HolegFactory.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package algorithms.geneticAlgorithm.holegGA;
  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. int maxObjects;
  16. int maxConnections;
  17. Random rng;
  18. public HolegFactory(ArrayList<AbstractCpsObject> objects, int maxObjects, int maxConnections){
  19. objectSpace = objects;
  20. objectSpace.add(null);
  21. this.maxObjects = maxObjects;
  22. this.maxConnections = maxConnections;
  23. rng = new Random();
  24. }
  25. @Override
  26. public HolegIndividual createRandomIndividual() {
  27. HolegIndividual hI = new HolegIndividual();
  28. createObjects(hI);
  29. createEdges(hI);
  30. return hI;
  31. }
  32. public void createObjects(HolegIndividual hI){
  33. int objCount = rng.nextInt(maxObjects) + 1;
  34. for(int i = 0; i < objCount; i++){
  35. AbstractCpsObject absObj = objectSpace.get(rng.nextInt(objectSpace.size()));
  36. AbstractCpsObject newObj = null;
  37. if(absObj instanceof HolonObject){
  38. newObj = new HolonObject(absObj);
  39. }else if(absObj instanceof HolonSwitch){
  40. newObj = new HolonSwitch(absObj);
  41. }else if(absObj instanceof HolonBattery){
  42. newObj = new HolonBattery(absObj);
  43. }else if(absObj == null){
  44. newObj = new CpsNode("Node");
  45. }
  46. hI.holonObjects.add(newObj);
  47. }
  48. }
  49. public void createEdges(HolegIndividual hI){
  50. if(hI.holonObjects.size() > 1){
  51. int edgeCount = rng.nextInt(maxConnections) + 1;
  52. ArrayList<Integer> list = new ArrayList<Integer>();
  53. for (int i = 0; i < hI.holonObjects.size(); i++) {
  54. list.add(new Integer(i));
  55. }
  56. for(int i = 0; i < edgeCount; i++){
  57. Collections.shuffle(list);
  58. int aPos = list.get(0);
  59. int bPos = list.get(1);
  60. hI.holonEdges.add(new GAEdge(aPos, bPos, hI.holonObjects.get(aPos), hI.holonObjects.get(bPos)));
  61. }
  62. }
  63. }
  64. }