123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<HolegIndividual> {
- ArrayList<AbstractCpsObject> objectSpace;
- public ArrayList<AbstractCpsObject> originObjects;
- public int maxObjects;
- public int maxConnections;
- Random rng;
- public boolean onlyNodes;
-
- public HolegFactory(ArrayList<AbstractCpsObject> objects, int maxObjects, int maxConnections){
- objectSpace = objects;
- objectSpace.add(null);
- this.maxObjects = maxObjects;
- this.maxConnections = maxConnections;
- rng = new Random();
- originObjects = new ArrayList<AbstractCpsObject>();
- 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<Integer> list = new ArrayList<Integer>();
-
- 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);
- }
- }
- }
- }
|