package algorithms.geneticAlgorithm.holegGA; 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; int maxObjects; int maxConnections; Random rng; public HolegFactory(ArrayList objects, int maxObjects, int maxConnections){ objectSpace = objects; objectSpace.add(null); this.maxObjects = maxObjects; this.maxConnections = maxConnections; rng = new Random(); } @Override public HolegIndividual createRandomIndividual() { HolegIndividual hI = new HolegIndividual(); createObjects(hI); createEdges(hI); return hI; } public void createObjects(HolegIndividual hI){ int objCount = rng.nextInt(maxObjects) + 1; for(int i = 0; i < objCount; i++){ AbstractCpsObject absObj = objectSpace.get(rng.nextInt(objectSpace.size())); AbstractCpsObject newObj = null; 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"); } hI.holonObjects.add(newObj); } } public void createEdges(HolegIndividual hI){ if(hI.holonObjects.size() > 1){ int edgeCount = rng.nextInt(maxConnections) + 1; ArrayList list = new ArrayList(); for (int i = 0; i < hI.holonObjects.size(); i++) { list.add(new Integer(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))); } } } }