123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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<HolegIndividual> {
- ArrayList<AbstractCpsObject> objectSpace;
- int maxObjects;
- int maxConnections;
- Random rng;
-
- public HolegFactory(ArrayList<AbstractCpsObject> 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<Integer> list = new ArrayList<Integer>();
-
- 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)));
- }
- }
- }
- }
|