HolegFactory.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package algorithms.geneticAlgorithm.holegGA.Components;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Random;
  7. import classes.AbstractCpsObject;
  8. import classes.CpsNode;
  9. import classes.HolonBattery;
  10. import classes.HolonObject;
  11. import classes.HolonSwitch;
  12. import algorithms.geneticAlgorithm.Components.GAIndividualFactory;
  13. public class HolegFactory implements GAIndividualFactory<HolegIndividual> {
  14. ArrayList<AbstractCpsObject> objectSpace;
  15. public ArrayList<AbstractCpsObject> originObjects;
  16. public int maxObjects;
  17. public int maxConnections;
  18. Random rng;
  19. public boolean onlyNodes;
  20. public HolegFactory(ArrayList<AbstractCpsObject> objects, int maxObjects, int maxConnections){
  21. objectSpace = objects;
  22. objectSpace.add(null);
  23. this.maxObjects = maxObjects;
  24. this.maxConnections = maxConnections;
  25. rng = new Random();
  26. originObjects = new ArrayList<AbstractCpsObject>();
  27. onlyNodes = false;
  28. }
  29. @Override
  30. public HolegIndividual createRandomIndividual() {
  31. HolegIndividual hI = new HolegIndividual(originObjects);
  32. if(maxObjects > 0){
  33. createObjects(hI);
  34. }
  35. if(maxConnections > 0){
  36. createEdges(hI);
  37. }
  38. return hI;
  39. }
  40. public void createObjects(HolegIndividual hI){
  41. int objCount = rng.nextInt(maxObjects) + 1;
  42. for(int i = 0; i < objCount; i++){
  43. AbstractCpsObject newObj = null;
  44. if(!onlyNodes){
  45. AbstractCpsObject absObj = objectSpace.get(rng.nextInt(objectSpace.size()));
  46. if(absObj instanceof HolonObject){
  47. newObj = new HolonObject(absObj);
  48. }else if(absObj instanceof HolonSwitch){
  49. newObj = new HolonSwitch(absObj);
  50. }else if(absObj instanceof HolonBattery){
  51. newObj = new HolonBattery(absObj);
  52. }else if(absObj == null){
  53. newObj = new CpsNode("Node");
  54. }
  55. }else{
  56. newObj = new CpsNode("Node");
  57. }
  58. //hI.holonObjects.add(newObj);
  59. hI.addObject(newObj);
  60. }
  61. }
  62. public void createEdges(HolegIndividual hI){
  63. if(hI.getIndexes().size() > 1){
  64. int edgeCount = rng.nextInt(maxConnections) + 1;
  65. ArrayList<Integer> list = new ArrayList<Integer>();
  66. for (int i = 0; i < hI.getIndexes().size(); i++) {
  67. list.add(hI.getIndexes().get(i));
  68. }
  69. for(int i = 0; i < edgeCount; i++){
  70. Collections.shuffle(list);
  71. int aPos = list.get(0);
  72. int bPos = list.get(1);
  73. //hI.holonEdges.add(new GAEdge(aPos, bPos, hI.holonObjects.get(aPos), hI.holonObjects.get(bPos)));
  74. hI.addEdge(aPos, bPos);
  75. }
  76. }
  77. }
  78. }