package algorithms.geneticAlgorithm.holegGA.Components; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import classes.AbstractCpsObject; import classes.CpsNode; import classes.HolonBattery; import classes.HolonObject; import classes.HolonSwitch; import algorithms.geneticAlgorithm.Components.GAIndividualFactory; public class HolegFactory implements GAIndividualFactory { ArrayList objectSpace; public ArrayList originObjects; public int maxObjects; public int maxConnections; Random rng; public boolean onlyNodes; public HolegFactory(ArrayList objects, int maxObjects, int maxConnections){ objectSpace = objects; objectSpace.add(null); this.maxObjects = maxObjects; this.maxConnections = maxConnections; rng = new Random(); originObjects = new ArrayList(); onlyNodes = false; } @Override public HolegIndividual createRandomIndividual() { HolegIndividual hI = new HolegIndividual(originObjects); if(maxObjects > 0){ createObjects(hI); } if(maxConnections > 0){ createEdges(hI); } return hI; } public void createObjects(HolegIndividual hI){ int objCount = rng.nextInt(maxObjects) + 1; for(int i = 0; i < objCount; i++){ AbstractCpsObject newObj = null; if(!onlyNodes){ AbstractCpsObject absObj = objectSpace.get(rng.nextInt(objectSpace.size())); if(absObj instanceof HolonObject){ newObj = new HolonObject(absObj); }else if(absObj instanceof HolonSwitch){ newObj = new HolonSwitch(absObj); }else if(absObj instanceof HolonBattery){ newObj = new HolonBattery(absObj); }else if(absObj == null){ newObj = new CpsNode("Node"); } }else{ newObj = new CpsNode("Node"); } //hI.holonObjects.add(newObj); hI.addObject(newObj); } } public void createEdges(HolegIndividual hI){ if(hI.getIndexes().size() > 1){ int edgeCount = rng.nextInt(maxConnections) + 1; ArrayList list = new ArrayList(); for (int i = 0; i < hI.getIndexes().size(); i++) { list.add(hI.getIndexes().get(i)); } for(int i = 0; i < edgeCount; i++){ Collections.shuffle(list); int aPos = list.get(0); int bPos = list.get(1); //hI.holonEdges.add(new GAEdge(aPos, bPos, hI.holonObjects.get(aPos), hI.holonObjects.get(bPos))); hI.addEdge(aPos, bPos); } } } }