HolegFittnessScenario.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package algorithms.geneticAlgorithm.holegGA.Components;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Random;
  7. import classes.CpsEdge;
  8. import classes.HolonWildCard;
  9. import classes.Position;
  10. import ui.controller.Control;
  11. import ui.controller.SimulationManager;
  12. import ui.model.Model;
  13. import algorithms.geneticAlgorithm.Components.GAFittnessFunctionStrategy;
  14. import algorithms.geneticAlgorithm.holegGA.GAEdge;
  15. import algorithms.geneticAlgorithm.holegGA.ParameterArray;
  16. public class HolegFittnessScenario implements GAFittnessFunctionStrategy<HolegIndividual>{
  17. HolegFittnessFkt singleFkt;
  18. Control controller;
  19. Model model;
  20. SimulationManager simManager;
  21. ParameterArray params;
  22. int edgeBreakCount;
  23. double edgeLengthPoints;
  24. double wildcardPoints;
  25. double overprodPoints;
  26. int totalSteps = 100;
  27. int singleStep;
  28. int breakCounter = 0;
  29. HolegIndividual originalNetwork;
  30. ArrayList<Integer> edgeBreakingIndexes = new ArrayList<Integer>();
  31. double globalOverProd;
  32. Random rng = new Random();
  33. public HolegFittnessScenario(Control controller) {
  34. singleFkt = new HolegFittnessFkt(controller);
  35. model = singleFkt.getModel();
  36. }
  37. @Override
  38. public double calculateFittness(HolegIndividual candidate) {
  39. globalOverProd = 0;
  40. double fittness = 0;
  41. double noBreakFittness = calcFittnessWithoutBreak(candidate);
  42. fittness += noBreakFittness;
  43. candidate.addLogEntry("Fittness without break: " + noBreakFittness);
  44. double breakFittness = calcFittnessWithBreak(candidate);
  45. candidate.addLogEntry("Fittness with break: " + breakFittness);
  46. fittness += breakFittness;
  47. fittness = fittness /2;
  48. globalOverProd = globalOverProd / 2;
  49. globalOverProd = globalOverProd / totalSteps;
  50. for(Integer wildIdx : candidate.getWildcardIndexes()){
  51. if(!(candidate.indexToObjectMap.get(wildIdx) instanceof HolonWildCard)){
  52. fittness += wildcardPoints;
  53. }
  54. }
  55. double edgeLength = 0;
  56. for(GAEdge e : candidate.getAllEdges()){
  57. Position aPos = e.getA().getPosition();
  58. Position bPos = e.getB().getPosition();
  59. double xDiff = Math.abs(aPos.x - bPos.x);
  60. double yDiff = Math.abs(aPos.y - bPos.y);
  61. edgeLength += Math.sqrt(Math.pow(xDiff, 2) + Math.pow(yDiff, 2));
  62. }
  63. candidate.setEdgeLength(getRoundedValue(edgeLength));
  64. candidate.addLogEntry("Total Edge Length: " + getRoundedValue(edgeLength));
  65. fittness += (edgeLength/100)*edgeLengthPoints;
  66. candidate.addLogEntry("average Overproduction: " + getRoundedValue(globalOverProd));
  67. candidate.setFittness(getRoundedValue(fittness));
  68. candidate.addLogEntry("Fittness: " + getRoundedValue(fittness));
  69. return fittness;
  70. }
  71. public double calcFittnessWithoutBreak(HolegIndividual candidate){
  72. double fittness = 0;
  73. double avgOverProduction = 0;
  74. for(CpsEdge edge : candidate.brokenEdges){
  75. edge.setWorkingState(true);
  76. }
  77. candidate.brokenEdges.clear();
  78. for(int i = 0; i < totalSteps; i++){
  79. model.setCurIteration(singleStep * i);
  80. fittness += singleFkt.calculateFittness(candidate);
  81. avgOverProduction += singleFkt.getCurrentOverProduction();
  82. }
  83. globalOverProd += avgOverProduction;
  84. fittness += (avgOverProduction / 1000) * overprodPoints;
  85. fittness = (int)fittness;
  86. fittness = fittness/totalSteps;
  87. return fittness;
  88. }
  89. public double calcFittnessWithBreak(HolegIndividual candidate){
  90. double fittness = 0;
  91. double avgOverProduction = 0;
  92. for(CpsEdge edge : candidate.brokenEdges){
  93. edge.setWorkingState(true);
  94. }
  95. breakEdges(candidate);
  96. for(int i = 0; i < totalSteps; i++){
  97. model.setCurIteration(singleStep * i);
  98. fittness += singleFkt.calculateFittness(candidate);
  99. avgOverProduction += singleFkt.getCurrentOverProduction();
  100. }
  101. globalOverProd += avgOverProduction;
  102. fittness += (avgOverProduction / 1000) * overprodPoints;
  103. fittness = (int)fittness;
  104. fittness = fittness/totalSteps;
  105. return fittness;
  106. }
  107. private void breakEdges(HolegIndividual candidate) {
  108. candidate.brokenEdges.clear();
  109. int unbrokenEdges = 0;
  110. for(Integer idx : edgeBreakingIndexes){
  111. ArrayList<CpsEdge> healthyEdges = new ArrayList<CpsEdge>();
  112. for(CpsEdge edge : candidate.indexToObjectMap.get(idx).getConnections()){
  113. if(edge.isWorking()){
  114. healthyEdges.add(edge);
  115. }
  116. }
  117. Collections.shuffle(healthyEdges);
  118. if(healthyEdges.size() > 0){
  119. healthyEdges.get(0).setWorkingState(false);
  120. candidate.brokenEdges.add(healthyEdges.get(0));
  121. }else{
  122. unbrokenEdges++;
  123. }
  124. }
  125. if(unbrokenEdges > 0){
  126. ArrayList<CpsEdge> healthyEdges = new ArrayList<CpsEdge>();
  127. for(CpsEdge edge : candidate.getAllEdges()){
  128. if(edge.isWorking()){
  129. healthyEdges.add(edge);
  130. }
  131. }
  132. Collections.shuffle(healthyEdges);
  133. int bound;
  134. if(unbrokenEdges <= healthyEdges.size()){
  135. bound = unbrokenEdges;
  136. }else{
  137. bound = healthyEdges.size();
  138. }
  139. for(int i = 0; i < bound; i++){
  140. healthyEdges.get(i).setWorkingState(false);
  141. }
  142. }
  143. }
  144. public void setParameters(ParameterArray params){
  145. this.params = params;
  146. edgeBreakCount = (int)params.get(params.EDGE_BREAK_AMOUNT);
  147. edgeLengthPoints = (double)params.get(params.LENGTH_POINTS);
  148. wildcardPoints = (double)params.get(params.WILDCARD_USAGE);
  149. totalSteps = (int)params.get(params.SIM_ITERATIONS);
  150. overprodPoints = (double)params.get(params.OVERPRODUCTION);
  151. originalNetwork = (HolegIndividual)params.get(params.ORIGINAL_NETWORK);
  152. if(totalSteps < 100){
  153. singleStep = (int) Math.floor(100/totalSteps);
  154. }else{
  155. singleStep = 1;
  156. }
  157. singleFkt.setParameters(params);
  158. }
  159. /*
  160. * method is called before a new generation is calculated
  161. * setting the specific edge breaks here
  162. */
  163. public void initialize(){
  164. edgeBreakingIndexes.clear();
  165. ArrayList<Integer> objIndexes = new ArrayList<Integer>();
  166. objIndexes.addAll(originalNetwork.getIndexes());
  167. Collections.shuffle(objIndexes);
  168. //shuffeling leads to random selection of Indexes
  169. int bound;
  170. if(edgeBreakCount <= objIndexes.size()){
  171. bound = edgeBreakCount;
  172. }else{
  173. bound = objIndexes.size();
  174. }
  175. for(int i = 0; i < bound; i++){
  176. //edgeBreakingIndexes.add(objIndexes.get(i));
  177. edgeBreakingIndexes.add(objIndexes.get(rng.nextInt(objIndexes.size())));
  178. }
  179. }
  180. public double getRoundedValue(double value){
  181. BigDecimal bd = new BigDecimal(value);
  182. bd = bd.setScale(2, RoundingMode.HALF_UP);
  183. return bd.doubleValue();
  184. }
  185. }