| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.uka.ipd.sdq.dsexplore.opt4j.representation; |
| 5 | |
| 6 | import org.opt4j.genotype.Bounds; |
| 7 | |
| 8 | import de.uka.ipd.sdq.pcm.designdecision.ContinuousRangeDegree; |
| 9 | import de.uka.ipd.sdq.pcm.designdecision.DecisionSpace; |
| 10 | import de.uka.ipd.sdq.pcm.designdecision.DegreeOfFreedomInstance; |
| 11 | import de.uka.ipd.sdq.pcm.designdecision.DiscreteRangeDegree; |
| 12 | import de.uka.ipd.sdq.pcm.designdecision.ClassDegree; |
| 13 | |
| 14 | @Deprecated |
| 15 | class DimensionBounds implements Bounds<Double> { |
| 16 | |
| 17 | |
| 18 | protected DecisionSpace problem; |
| 19 | |
| 20 | public DimensionBounds(DecisionSpace problem){ |
| 21 | this.problem = problem; |
| 22 | |
| 23 | } |
| 24 | |
| 25 | @Override |
| 26 | public Double getLowerBound(int index) { |
| 27 | if (DiscreteRangeDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index))){ |
| 28 | return this.getLowerIntBound(index); |
| 29 | } else if (ContinuousRangeDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index)) ){ |
| 30 | return this.getLowerDoubleBound(index); |
| 31 | } else if (ClassDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index))){ |
| 32 | return this.getLowerEnumerationBound(index); |
| 33 | } else |
| 34 | throw new RuntimeException("Design decision not supported: "+this.problem.getDegreesOfFreedom().get(index).getClass().getName()); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public Double getUpperBound(int index) { |
| 39 | if (DiscreteRangeDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index))){ |
| 40 | return this.getUpperIntBound(index); |
| 41 | } else if (ContinuousRangeDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index)) ){ |
| 42 | return this.getUpperDoubleBound(index); |
| 43 | } else if (ClassDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index))){ |
| 44 | return this.getUpperEnumerationBound(index); |
| 45 | } else |
| 46 | throw new RuntimeException("Design decision not supported: "+this.problem.getDegreesOfFreedom().get(index).getClass().getName()); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | private Double getLowerEnumerationBound(int index) { |
| 51 | //Lower bound for enumeration types is always 0. |
| 52 | return 0.0; |
| 53 | } |
| 54 | |
| 55 | private Double getUpperEnumerationBound(int index) { |
| 56 | int upperEnumerationBound = 0; |
| 57 | DegreeOfFreedomInstance degree = this.problem.getDegreesOfFreedom().get(index); |
| 58 | if (ClassDegree.class.isInstance(degree)){ |
| 59 | upperEnumerationBound = ((ClassDegree)degree).getClassDesignOptions().size()-1; |
| 60 | } else { |
| 61 | throw new RuntimeException("Domain of design decision not supported: "+this.problem.getDegreesOfFreedom().get(index).getClass().getName()); |
| 62 | } |
| 63 | |
| 64 | return new Double(upperEnumerationBound); |
| 65 | } |
| 66 | |
| 67 | private Double getLowerDoubleBound(int index) { |
| 68 | ContinuousRangeDegree range = (ContinuousRangeDegree)this.problem.getDegreesOfFreedom().get(index); |
| 69 | return range.isLowerBoundIncluded() ? range.getFrom() : range.getFrom() + Double.MIN_VALUE; |
| 70 | } |
| 71 | |
| 72 | private Double getUpperDoubleBound(int index) { |
| 73 | ContinuousRangeDegree range = (ContinuousRangeDegree)this.problem.getDegreesOfFreedom().get(index); |
| 74 | return range.isUpperBoundIncluded() ? range.getTo() : range.getTo() - Double.MIN_VALUE; |
| 75 | } |
| 76 | |
| 77 | private Double getLowerIntBound(int index) { |
| 78 | DiscreteRangeDegree range = (DiscreteRangeDegree)this.problem.getDegreesOfFreedom().get(index); |
| 79 | return Double.valueOf(range.isLowerBoundIncluded() ? range.getFrom() : range.getFrom() + 1); |
| 80 | } |
| 81 | |
| 82 | private Double getUpperIntBound(int index) { |
| 83 | DiscreteRangeDegree range = (DiscreteRangeDegree)this.problem.getDegreesOfFreedom().get(index); |
| 84 | return Double.valueOf(range.isUpperBoundIncluded() ? range.getTo() : range.getTo() - 1); |
| 85 | } |
| 86 | |
| 87 | public int numberOfDimensions() { |
| 88 | //here, the sizes of the arrays have to be the same! |
| 89 | return this.problem.getDegreesOfFreedom().size(); |
| 90 | } |
| 91 | |
| 92 | public boolean isInteger(int index){ |
| 93 | return ( |
| 94 | ClassDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index)) |
| 95 | || DiscreteRangeDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index))); |
| 96 | } |
| 97 | |
| 98 | public boolean isEnum(int index){ |
| 99 | return ( |
| 100 | ClassDegree.class.isInstance(this.problem.getDegreesOfFreedom().get(index)) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Checks whether a single gene is within the bounds specified for the given index |
| 106 | * @param doubleGene |
| 107 | * @param index |
| 108 | * @return |
| 109 | */ |
| 110 | public boolean isValidGene(Double doubleGene, int index) { |
| 111 | if (doubleGene.isNaN() || doubleGene.isInfinite() |
| 112 | || doubleGene < this.getLowerBound(index) |
| 113 | || doubleGene > this.getUpperBound(index)) |
| 114 | return false; |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | } |