12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package algorithms.geneticAlgorithm.holegGA;
- import java.util.ArrayList;
- import classes.AbstractCpsObject;
- import classes.Category;
- import classes.CpsEdge;
- import classes.Position;
- import ui.controller.Control;
- import ui.model.Model;
- import algorithms.geneticAlgorithm.Components.GAResultListener;
- public class GenAlgoHandler {
-
- ArrayList<GAResultListener<HolegIndividual>> listeners = new ArrayList<GAResultListener<HolegIndividual>>();
- HolegFactory factory;
- Control controller;
- Model model;
- int genCount;
-
- public GenAlgoHandler(Control controller, Model model){
- this.controller = controller;
- this.model = model;
- genCount = 1;
- ArrayList<AbstractCpsObject> objSpace = new ArrayList<AbstractCpsObject>();
- for(Category cat : model.getCategories()){
- for(AbstractCpsObject obj : cat.getObjects()){
- objSpace.add(obj);
- }
- }
- factory = new HolegFactory(objSpace, 5, 5);
- }
-
- public void createGeneration(){
- ArrayList<HolegIndividual> population = new ArrayList<HolegIndividual>();
- for(int i = 1; i <= 2; i++){
- population.add(factory.createRandomIndividual());
- }
- addPopulationListeners(genCount, population);
- genCount++;
-
- ArrayList<HolegIndividual> children = new ArrayList<HolegIndividual>();
- HolegCrossover hC = new HolegCrossover();
- //for(int j = 1; j <= 2; j++){
- children.addAll(hC.crossOver(population));
- //}
- addPopulationListeners(genCount, children);
- genCount++;
- }
-
- public void drawIndividual(HolegIndividual hI){
- model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
- model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
-
- int xBound = (int) Math.ceil(Math.sqrt(hI.holonObjects.size()));
- int row = -1;
-
- int x = 50;
- int y = 50;
- int dist = 200;
- boolean isX = true;
-
- for(int i = 0; i < hI.holonObjects.size(); i++){
- //for(AbstractCpsObject abs : hI.holonObjects){
- AbstractCpsObject abs = hI.holonObjects.get(i);
-
-
-
- if(i % xBound == 0){
- row++;
- }
-
- abs.setPosition(new Position(((i % xBound)* dist) + x , (row * dist) + y));
- controller.addObjectCanvas(abs);
- }
-
- for(int j = 0; j < hI.holonEdges.size(); j++){
- controller.addEdgeOnCanvas(hI.holonEdges.get(j));
- }
- }
-
- public void addListener(GAResultListener<HolegIndividual> listener){
- listeners.add(listener);
- }
-
- public void addPopulationListeners(int generation, ArrayList<HolegIndividual> population){
- for(GAResultListener<HolegIndividual> listener : listeners){
- listener.populationCreated(generation, population);
- }
- }
-
-
- }
|