| 1 | package de.uka.ipd.sdq.dsexplore.opt4j.representation; |
| 2 | |
| 3 | import org.opt4j.operator.Apply; |
| 4 | import org.opt4j.operator.neighbor.Neighbor; |
| 5 | import org.opt4j.operator.normalize.NormalizeDouble; |
| 6 | import org.opt4j.common.random.Rand; |
| 7 | |
| 8 | import com.google.inject.Singleton; |
| 9 | |
| 10 | import de.uka.ipd.sdq.dsexplore.opt4j.genotype.DesignDecisionGenotype; |
| 11 | |
| 12 | @Singleton |
| 13 | @Apply(DesignDecisionGenotype.class) |
| 14 | public class DSENeighbor implements Neighbor<DesignDecisionGenotype> { |
| 15 | |
| 16 | public DSENeighbor(NormalizeDouble normalize, Rand random) { |
| 17 | System.out.print("using DSENeighbor"); |
| 18 | } |
| 19 | |
| 20 | @Override |
| 21 | public void neighbor(DesignDecisionGenotype genotype) { |
| 22 | |
| 23 | |
| 24 | /*DesignDecisionGenotype vector = (DesignDecisionGenotype) genotype; |
| 25 | |
| 26 | int size = vector.size(); |
| 27 | |
| 28 | int i = random.nextInt(size); |
| 29 | |
| 30 | |
| 31 | |
| 32 | if (Opt4JStarter.getProblem().getBounds().isInteger(i)){ |
| 33 | int currentValue = vector.get(i).intValue(); |
| 34 | |
| 35 | //TODO: handle enums right |
| 36 | |
| 37 | if (Opt4JStarter.getProblem().getBounds().isEnum(i)){ |
| 38 | //handle enum: all are neighbors. Lower bound is 0, but maybe I change it later, so this is fool proof. |
| 39 | int choice = random.nextInt((int)(Opt4JStarter.getProblem().getBounds().getUpperBound(i) - Opt4JStarter.getProblem().getBounds().getLowerBound(i))); |
| 40 | //shift one up if the value is larger than the current choice, as the current is no neighbor of itself and the upper bound cannot be produced by nextInt |
| 41 | if (choice > currentValue){ |
| 42 | choice ++; |
| 43 | } |
| 44 | vector.set(i, new Double(choice+ Opt4JStarter.getProblem().getBounds().getLowerBound(i))); |
| 45 | |
| 46 | } |
| 47 | //handle ordered integer |
| 48 | else if (Opt4JStarter.getProblem().getBounds().getLowerBound(i) == currentValue){ |
| 49 | vector.set(i, new Double(currentValue + 1)); |
| 50 | } else if (Opt4JStarter.getProblem().getBounds().getUpperBound(i) == currentValue){ |
| 51 | vector.set(i, new Double(currentValue - 1)); |
| 52 | } else { |
| 53 | //decide whether going up or down |
| 54 | boolean add = (random.nextInt(2) != 0); |
| 55 | if (add){ |
| 56 | vector.set(i, new Double(currentValue + 1)); |
| 57 | } else { |
| 58 | vector.set(i, new Double(currentValue - 1)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | } else { |
| 64 | //if double value, use the normal way |
| 65 | //copied from superclass NeighborDouble |
| 66 | double value = vector.get(i) + random.nextDouble() * 0.1 - 0.05; |
| 67 | vector.set(i, value); |
| 68 | |
| 69 | normalize.normalize(vector); |
| 70 | } |
| 71 | */ |
| 72 | throw new UnsupportedOperationException("DSE Neighbour operator has not been implemented yet."); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | } |