EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.dsexplore.opt4j.operator]

COVERAGE SUMMARY FOR SOURCE FILE [MutateDesignDecisionGenotype.java]

nameclass, %method, %block, %line, %
MutateDesignDecisionGenotype.java0%   (0/1)0%   (0/8)0%   (0/312)0%   (0/69)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MutateDesignDecisionGenotype0%   (0/1)0%   (0/8)0%   (0/312)0%   (0/69)
MutateDesignDecisionGenotype (Rand, MutateInteger, MutateDouble, MutationRate... 0%   (0/1)0%   (0/15)0%   (0/6)
mutate (DesignDecisionGenotype): void 0%   (0/1)0%   (0/65)0%   (0/12)
mutateContinous (ContinousRangeChoice): void 0%   (0/1)0%   (0/43)0%   (0/10)
mutateDiscrete (DiscreteRangeChoice): void 0%   (0/1)0%   (0/62)0%   (0/12)
mutateEnum (ClassChoice): void 0%   (0/1)0%   (0/38)0%   (0/9)
mutateInteger (int, int, int): int 0%   (0/1)0%   (0/23)0%   (0/5)
mutateSchedulingPolicyChoice (SchedulingPolicyChoice): void 0%   (0/1)0%   (0/44)0%   (0/11)
randomlySelectNewIndex (List, int): int 0%   (0/1)0%   (0/22)0%   (0/4)

1package de.uka.ipd.sdq.dsexplore.opt4j.operator;
2 
3 
4 
5import java.util.List;
6 
7import org.opt4j.common.random.Rand;
8import org.opt4j.genotype.DoubleGenotype;
9import org.opt4j.genotype.IntegerGenotype;
10import org.opt4j.operator.mutate.Mutate;
11import org.opt4j.operator.mutate.MutateDouble;
12import org.opt4j.operator.mutate.MutateInteger;
13import org.opt4j.operator.mutate.MutationRate;
14 
15import com.google.inject.Inject;
16 
17import de.uka.ipd.sdq.dsexplore.exception.ChoiceOutOfBoundsException;
18import de.uka.ipd.sdq.dsexplore.exception.InvalidChoiceForDegreeException;
19import de.uka.ipd.sdq.dsexplore.opt4j.genotype.DesignDecisionGenotype;
20import de.uka.ipd.sdq.pcm.designdecision.Choice;
21import de.uka.ipd.sdq.pcm.designdecision.ContinousRangeChoice;
22import de.uka.ipd.sdq.pcm.designdecision.ContinuousRangeDegree;
23import de.uka.ipd.sdq.pcm.designdecision.DegreeOfFreedomInstance;
24import de.uka.ipd.sdq.pcm.designdecision.DiscreteRangeChoice;
25import de.uka.ipd.sdq.pcm.designdecision.DiscreteRangeDegree;
26import de.uka.ipd.sdq.pcm.designdecision.ClassChoice;
27import de.uka.ipd.sdq.pcm.designdecision.ClassDegree;
28import de.uka.ipd.sdq.pcm.designdecision.OrderedIntegerDegree;
29import de.uka.ipd.sdq.pcm.designdecision.SchedulingPolicyChoice;
30import de.uka.ipd.sdq.pcm.designdecision.SchedulingPolicyDegree;
31import de.uka.ipd.sdq.pcm.resourceenvironment.SchedulingPolicy;
32 
33/**
34 * Mutator for {@link DesignDecisionGenotype}s. Calls the bound {@link MutateInteger} or {@link MutateDouble}, depending on 
35 * the type of {@link DegreeOfFreedomInstance} of each gene. 
36 * 
37 * @author martens
38 *
39 */
40public class MutateDesignDecisionGenotype implements Mutate<DesignDecisionGenotype> {
41        
42        private MutateInteger mutateInteger;
43        private MutateDouble mutateDouble;
44        private MutationRate mutationRate;
45        private Rand random;
46        
47 
48        @Inject
49        public MutateDesignDecisionGenotype(Rand random, MutateInteger mutateInteger, MutateDouble mutateDouble, MutationRate rate){
50                 this.mutateDouble = mutateDouble;
51                 this.mutateInteger = mutateInteger;
52                 this.mutationRate = rate;
53                 this.random = random;
54                 
55        }
56 
57        /**
58         * Mutates the passed {@link DesignDecisionGenotype} by calling a matching {@link Mutate}
59         * for each gene.
60         */
61        @Override
62        public void mutate(DesignDecisionGenotype genotype) {
63                for (Choice choice : genotype) {
64                        if (random.nextDouble() < mutationRate.get()) {
65                                if (choice instanceof DiscreteRangeChoice){
66                                        mutateDiscrete((DiscreteRangeChoice)choice);
67                                } else if (choice instanceof ClassChoice){
68                                        mutateEnum((ClassChoice)choice);
69                                } else if (choice instanceof ContinousRangeChoice){
70                                        mutateContinous((ContinousRangeChoice)choice);
71                                } else if (choice instanceof SchedulingPolicyChoice){
72                                        mutateSchedulingPolicyChoice((SchedulingPolicyChoice)choice);
73                                } else {
74                                        throw new UnsupportedOperationException("Choice type "+choice+" not supported.");
75                                }
76                        }
77                }
78        }
79        
80        private void mutateSchedulingPolicyChoice(SchedulingPolicyChoice choice) {
81                DegreeOfFreedomInstance degree = choice.getDegreeOfFreedomInstance();
82                if (degree instanceof SchedulingPolicyDegree){
83                        SchedulingPolicyDegree schedDegree = (SchedulingPolicyDegree)degree;
84                        List<SchedulingPolicy> domain = schedDegree.getDomainOfAllowedSchedulingPolicies();
85                        
86                        int oldIndex = domain.indexOf(choice.getChosenValue());
87                        if (oldIndex == -1){
88                                throw new ChoiceOutOfBoundsException(choice, "Error when mutating individual, old choice was invalid");
89                        }
90                        int newIndex = randomlySelectNewIndex(domain, oldIndex);
91                        
92                        choice.setChosenValue(domain.get(newIndex));
93                        
94                } else {
95                        throw new InvalidChoiceForDegreeException(choice);
96                }
97                
98        }
99 
100        @SuppressWarnings("unchecked")
101        private int randomlySelectNewIndex(List domain,
102                        int oldIndex) {
103                //use mutateInteger as that one already realises a uniform distribution among the possible values with a certain rate. 
104                int newIndex = mutateInteger(oldIndex, 0, domain.size()-1);
105                
106                //choose new value from the (size - 1) (all except old) values. 
107                //randomly choose an integer from [0,size-2]
108                //int newIndex = this.random.nextInt(enumDegree.getClassDesignOptions().size() -1);
109                //if new index is larger or equal than the old value's index, shift it by one, 
110                //to achieve a uniform distribution and not choose the old value again. 
111                //if (oldIndex <= newIndex){
112                //        newIndex ++;
113                //}
114 
115                if (newIndex < 0 || newIndex >= domain.size()){
116                        throw new RuntimeException("Error when mutating integer index value: Value is out of bounds!");
117                }
118                return newIndex;
119        }
120 
121        /**
122         * Calls {@link MutateDouble} for the given {@link Choice}.
123         * @param choice
124         */
125        private void mutateContinous(ContinousRangeChoice choice) {
126                DegreeOfFreedomInstance degree = choice.getDegreeOfFreedomInstance();
127                if (degree instanceof ContinuousRangeDegree){
128                        ContinuousRangeDegree contDegree = (ContinuousRangeDegree)degree;
129                        DoubleGenotype doubleList = new DoubleGenotype(contDegree.getFrom(),contDegree.getTo());
130                        doubleList.add(choice.getChosenValue());
131                        this.mutateDouble.mutate(doubleList);
132                        double newValue = doubleList.get(0);
133                        choice.setChosenValue(newValue);
134                } else throw new InvalidChoiceForDegreeException(choice);
135        }
136 
137        /**
138         * Mutates an enumeration design decision (i.e. without order) by randomly choosing a 
139         * new (!= old) value from the design decision options.
140         * 
141         * @param choice
142         */
143        private void mutateEnum(ClassChoice choice) {
144                DegreeOfFreedomInstance degree = choice.getDegreeOfFreedomInstance();
145                if (degree instanceof ClassDegree){
146                        ClassDegree enumDegree = (ClassDegree) degree;
147                        int oldIndex = enumDegree.getClassDesignOptions().indexOf(choice.getChosenValue());
148                        if (oldIndex == -1){
149                                throw new ChoiceOutOfBoundsException(choice, "Error when mutating individual, old choice was invalid");
150                        }
151                        //use mutateInteger as that one already realises a uniform distribution among the possible values with a certain rate. 
152                        int newIndex = randomlySelectNewIndex(enumDegree.getClassDesignOptions(), oldIndex);
153                        
154                        choice.setChosenValue(enumDegree.getClassDesignOptions().get(newIndex));
155                }
156        }
157 
158        /**
159         * Calls {@link MutateInteger} for the given {@link Choice}. 
160         * @param choice
161         */
162        private void mutateDiscrete(DiscreteRangeChoice choice) {
163                DegreeOfFreedomInstance degree = choice.getDegreeOfFreedomInstance();
164                if (degree instanceof DiscreteRangeDegree) {
165                        DiscreteRangeDegree discDegree = (DiscreteRangeDegree) degree;
166                        int newValue = mutateInteger(choice.getChosenValue(), discDegree.getFrom(), discDegree.getTo());
167                        choice.setChosenValue(newValue);
168                } else if (degree instanceof OrderedIntegerDegree){
169                        OrderedIntegerDegree orderedIntegerDegree = (OrderedIntegerDegree) degree;
170                        int currentIndex = orderedIntegerDegree.getListOfIntegers().indexOf(new Integer(choice.getChosenValue()));
171                        int randomIndex = mutateInteger(currentIndex, 0,orderedIntegerDegree.getListOfIntegers().size()-1);
172                        choice.setChosenValue(orderedIntegerDegree.getListOfIntegers().get(randomIndex));
173                } else throw new InvalidChoiceForDegreeException(choice);
174                
175        }
176 
177        private int mutateInteger(int oldValue, int lowerBound, int upperBound) {
178                IntegerGenotype integerGenotype = new IntegerGenotype(lowerBound, upperBound);
179                integerGenotype.add(oldValue);
180                this.mutateInteger.mutate(integerGenotype);
181                int newValue = integerGenotype.get(0);
182                return newValue;
183        }
184 
185}

[all classes][de.uka.ipd.sdq.dsexplore.opt4j.operator]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov