Commit 972705e3 authored by Johannes Mey's avatar Johannes Mey
Browse files

improve genetic solver. test now *can* complete successfully

parent e46f4b61
......@@ -10,6 +10,8 @@ import de.tudresden.inf.st.mquat.solving.genetic.opt4j.Opt4jModule;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.operator.copy.TreeCopyOperatorModule;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.operator.crossover.TreeCrossoverOperatorModule;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.operator.mutate.TreeMutateOperatorModule;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.operators.diversity.TreeDiversityOperator;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.operators.diversity.TreeDiversityOperatorModule;
import de.tudresden.inf.st.mquat.utils.StopWatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -17,6 +19,7 @@ import org.opt4j.core.Individual;
import org.opt4j.core.optimizer.Archive;
import org.opt4j.core.start.Opt4JTask;
import org.opt4j.optimizers.ea.EvolutionaryAlgorithmModule;
import org.opt4j.optimizers.ea.Nsga2Module;
import org.opt4j.viewer.ViewerModule;
import java.util.ArrayList;
......@@ -62,12 +65,12 @@ public class GeneticSolver implements BenchmarkableSolver {
Opt4jModule.setModel(model);
EvolutionaryAlgorithmModule ea = new EvolutionaryAlgorithmModule();
ea.setGenerations(1000);
ea.setGenerations(20000);
// set population size
ea.setAlpha(50);
ea.setAlpha(100);
Opt4jModule testModule = new Opt4jModule();
Opt4jModule mquatModule = new Opt4jModule();
TreeCrossoverOperatorModule crossover = new TreeCrossoverOperatorModule();
TreeMutateOperatorModule mutate = new TreeMutateOperatorModule();
......@@ -76,9 +79,13 @@ public class GeneticSolver implements BenchmarkableSolver {
ViewerModule viewer = new ViewerModule();
viewer.setCloseOnStop(false);
Nsga2Module nsga2 = new Nsga2Module();
TreeDiversityOperatorModule diversity = new TreeDiversityOperatorModule();
Opt4JTask task = new Opt4JTask(false);
task.init(ea, testModule, crossover, mutate, copy);
// task.init(ea, testModule, viewer, crossover, mutate, copy);
task.init(ea, nsga2, mquatModule, crossover, mutate, copy);
// task.init(diversity, ea, nsga2, mquatModule, viewer, crossover, mutate, copy);
try {
task.execute();
Archive archive = task.getInstance(Archive.class);
......
......@@ -10,21 +10,17 @@ public class Opt4jEvaluator implements Evaluator<Solution> {
@Override
public Objectives evaluate(Solution phenotype) {
Objectives objectives = new Objectives();
objectives.add("Validity Errors", Sign.MIN, phenotype.evaluateValidity() * 1000);
objectives.add("Software Validity Errors", Sign.MIN, phenotype.evaluateSoftwareValidity() * 1000);
objectives.add("Structural Errors", Sign.MIN, phenotype.evaluateSoftwareCompleteness() * 1000);
int validity = phenotype.evaluateValidity();
int softwareValidity = phenotype.evaluateSoftwareValidity();
objectives.add("Validity Errors", Sign.MIN, phenotype.evaluateValidity());
objectives.add("Software Validity Errors", Sign.MIN, phenotype.evaluateSoftwareValidity());
double objective = phenotype.computeObjective();
if (phenotype.evaluateValidity() != 0) {
objective *= 10;
}
if (phenotype.evaluateSoftwareValidity() != 0) {
objective *= 10;
}
if (phenotype.evaluateSoftwareCompleteness() != 0) {
objective *= 10;
}
objective += validity * 0.5 * objective;
objective += softwareValidity * 0.5 * objective;
objectives.add("Energy", Sign.MIN, objective);
......
package de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.operator.crossover;
import com.google.inject.Inject;
import de.tudresden.inf.st.mquat.jastadd.model.Implementation;
import de.tudresden.inf.st.mquat.jastadd.model.Instance;
import de.tudresden.inf.st.mquat.jastadd.model.Request;
import de.tudresden.inf.st.mquat.jastadd.model.Resource;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.genotypes.TreeGenotype;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.genotypes.TreeGenotypeNode;
import org.opt4j.core.common.random.Rand;
import org.opt4j.operators.Apply;
import org.opt4j.operators.crossover.Crossover;
import org.opt4j.operators.crossover.Pair;
import java.util.Random;
@Apply(TreeGenotype.class)
public class TreeCrossoverOperator implements Crossover<TreeGenotype<Request, Instance, Resource, Implementation>> {
private final Random random;
private static final double Min = 0.0d;
private static final double Max = 1.0d;
private static final double probability = 0.5;
@Inject
public TreeCrossoverOperator(Rand random) {
this.random = random;
}
@Override
public Pair<TreeGenotype<Request, Instance, Resource, Implementation>> crossover(
TreeGenotype<Request, Instance, Resource, Implementation> oldGenotype1,
......@@ -66,8 +78,11 @@ public class TreeCrossoverOperator implements Crossover<TreeGenotype<Request, In
}
} else {
// TODO figure out what to swap
boolean swapImpl = true;
boolean swapResource = true;
double randomValue = Min + (Max - Min) * this.random.nextDouble();
boolean swapImpl = this.random.nextBoolean();
boolean swapResource = this.random.nextBoolean();
if (swapImpl) {
newNode1.setImpl(oldNode2.getImpl());
......@@ -80,11 +95,25 @@ public class TreeCrossoverOperator implements Crossover<TreeGenotype<Request, In
for (Instance instance : oldNode2.getSubStructure().keySet()) {
newNode1.getSubStructure().put(instance, oldNode2.getSubStructure().get(instance).deepCopy());
}
} else {
newNode1.setImpl(oldNode1.getImpl());
newNode2.setImpl(oldNode2.getImpl());
// add swapped substructures
for (Instance instance : oldNode1.getSubStructure().keySet()) {
newNode1.getSubStructure().put(instance, oldNode1.getSubStructure().get(instance).deepCopy());
}
for (Instance instance : oldNode2.getSubStructure().keySet()) {
newNode2.getSubStructure().put(instance, oldNode2.getSubStructure().get(instance).deepCopy());
}
}
if (swapResource) {
newNode1.setResource(oldNode2.getResource());
newNode2.setResource(oldNode1.getResource());
} else {
newNode1.setResource(oldNode1.getResource());
newNode2.setResource(oldNode2.getResource());
}
}
......
......@@ -15,9 +15,9 @@ import java.util.Random;
@Apply(TreeGenotype.class)
public class TreeMutateOperator implements Mutate<TreeGenotype<Request, Instance, Resource, Implementation>> {
protected final Random random;
public static double Min = 0.0d;
public static double Max = 1.0d;
private final Random random;
private static final double Min = 0.0d;
private static final double Max = 1.0d;
@Inject
public TreeMutateOperator(Rand random) {
......@@ -27,6 +27,8 @@ public class TreeMutateOperator implements Mutate<TreeGenotype<Request, Instance
@Override
public void mutate(TreeGenotype<Request, Instance, Resource, Implementation> genotype, double p) {
p = 0.9;
for (Request e : genotype.getRequestMap()
.keySet()) {
......@@ -34,7 +36,7 @@ public class TreeMutateOperator implements Mutate<TreeGenotype<Request, Instance
double randomValue = Min + (Max - Min) * this.random.nextDouble();
if (true) {
if (randomValue < p) {
genotype.getRequestMap().put(e, mutateNode(e, node, p));
}
......@@ -44,32 +46,46 @@ public class TreeMutateOperator implements Mutate<TreeGenotype<Request, Instance
private TreeGenotypeNode<Request, Instance, Resource, Implementation> mutateNode(Request request,
TreeGenotypeNode<Request, Instance, Resource, Implementation> node, double p) {
double Min = 0;
double Max = 1;
double randomValue = Min + (Max - Min) * this.random.nextDouble();
if (randomValue < .5) {
Component comp = node.getImpl().containingComponent();
Implementation newImp = comp.getImplementation(this.random.nextInt(comp.getNumImplementation()));
double randomValue = Min + (Max - Min) * random.nextDouble();
// clear substructure requirements
if (randomValue < p/3.0) {
List<Resource> unassignedResources = node.getContainingGenotype().getResources();
// add newly freed resources
for (TreeGenotypeNode subNode : node.getSubStructure().values()) {
unassignedResources.addAll(subNode.containedResources());
if (random.nextDouble() > 0.7) {
// mutate the resources
unassignedResources.add(node.getResource());
// get a random unassigned resource
int resourceIndex = random.nextInt(unassignedResources.size());
node.setResource(unassignedResources.get(resourceIndex));
unassignedResources.remove(resourceIndex);
}
node.getSubStructure().clear();
if (random.nextDouble() > 0.4) {
// mutate the implementation
Component comp = node.getImpl().containingComponent();
Implementation newImp = comp.getImplementation(random.nextInt(comp.getNumImplementation()));
// clear substructure requirements
// add newly freed resources
for (TreeGenotypeNode subNode : node.getSubStructure().values()) {
unassignedResources.addAll(subNode.containedResources());
}
node.getSubStructure().clear();
node.setImpl(newImp);
node.setImpl(newImp);
for (ComponentRequirement cr : newImp.getComponentRequirementList()) {
for (Instance ci : cr.getInstanceList()) {
// create node
Assignment newAssignment = newImp.root().createRandomAssignment(false, request, cr.getComponentRef().getRef(), unassignedResources, new Random());
node.getSubStructure().put(ci, Opt4jCreator.createNode(newAssignment, node.getContainingGenotype()));
for (ComponentRequirement cr : newImp.getComponentRequirementList()) {
for (Instance ci : cr.getInstanceList()) {
// create node
Assignment newAssignment = newImp.root().createRandomAssignment(false, request, cr.getComponentRef().getRef(), unassignedResources, new Random());
node.getSubStructure().put(ci, Opt4jCreator.createNode(newAssignment, node.getContainingGenotype()));
}
}
}
} else {
// mutate the children
for (Instance instance : node.getSubStructure().keySet()) {
......
package de.tudresden.inf.st.mquat.solving.genetic.opt4j.operators.diversity;
public class TreeDiversityOperator {
import de.tudresden.inf.st.mquat.jastadd.model.Implementation;
import de.tudresden.inf.st.mquat.jastadd.model.Instance;
import de.tudresden.inf.st.mquat.jastadd.model.Request;
import de.tudresden.inf.st.mquat.jastadd.model.Resource;
import de.tudresden.inf.st.mquat.solving.genetic.opt4j.custom.genotypes.TreeGenotype;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opt4j.operators.diversity.Diversity;
public class TreeDiversityOperator implements Diversity<TreeGenotype<Request, Instance, Resource, Implementation>> {
private static final Logger logger = LogManager.getLogger(TreeDiversityOperator.class);
@Override
public double diversity(TreeGenotype<Request, Instance, Resource, Implementation> a, TreeGenotype<Request, Instance, Resource, Implementation> b) {
logger.debug("DIVERSITY");
System.out.println("DIVERSITY");
return 1;
}
}
package de.tudresden.inf.st.mquat.solving.genetic.opt4j.operators.diversity;
public class TreeDiversityOperatorModule {
import org.opt4j.operators.diversity.DiversityModule;
public class TreeDiversityOperatorModule extends DiversityModule {
@Override
protected void config() {
addOperator(TreeDiversityOperator.class);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment