Skip to content
Snippets Groups Projects
Commit 3c59a3ee authored by Johannes Mey's avatar Johannes Mey
Browse files

some fixes. test 5 still fails.

parent 94e9eaae
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@ aspect Checking {
syn int Solution.evaluateValidity() {
int faults = evaluateSoftwareCompletenessa();
int faults = evaluateSoftwareCompleteness();
Set<Request> requestSet = new HashSet<>();
Set<Resource> resourceSet = new HashSet<>();
......@@ -54,7 +54,7 @@ aspect Checking {
syn int Solution.evaluateSoftwareValidity() {
int faults = evaluateSoftwareCompletenessa();
int faults = evaluateSoftwareCompleteness();
// check assignments
Iterator<Assignment> assignmentIterator = this.assignmentIterator();
......@@ -92,7 +92,7 @@ aspect Checking {
return faults;
}
syn int Solution.evaluateSoftwareCompletenessa() {
syn int Solution.evaluateSoftwareCompleteness() {
int faults = 0;
......@@ -102,7 +102,7 @@ aspect Checking {
}
for (Assignment assignment : getAssignmentList()) {
faults += assignment.evaluateSoftwareCompletenessa();
faults += assignment.evaluateSoftwareCompleteness();
targets.remove(assignment.getImplementation().containingComponent());
}
......@@ -113,7 +113,7 @@ aspect Checking {
return faults + targets.size();
}
syn int Assignment.evaluateSoftwareCompletenessa() {
syn int Assignment.evaluateSoftwareCompleteness() {
int faults = 0;
if (getRequest() == null) {
......
package de.tudresden.inf.st.mquat.solving.genetic;
import de.tudresden.inf.st.mquat.generator.ScenarioDescription;
import de.tudresden.inf.st.mquat.jastadd.model.Root;
import de.tudresden.inf.st.mquat.jastadd.model.Solution;
import de.tudresden.inf.st.mquat.solving.BenchmarkableSolver;
import de.tudresden.inf.st.mquat.solving.Solver;
import de.tudresden.inf.st.mquat.solving.SolvingException;
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.utils.StopWatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.viewer.ViewerModule;
import java.util.ArrayList;
import java.util.List;
......@@ -39,14 +49,58 @@ public class GeneticSolver implements BenchmarkableSolver {
@Override
public Solution solve(Root model) throws SolvingException {
reset();
stopWatch = StopWatch.start();
List<Solution> solutions = new ArrayList<>();
if (model.getNumRequest() == 0) {
return Solution.emptySolutionOf(model);
}
stopWatch = StopWatch.start();
List<Solution> solutions = new ArrayList<>();
// do some solvin'
Opt4jModule.setModel(model);
EvolutionaryAlgorithmModule ea = new EvolutionaryAlgorithmModule();
ea.setGenerations(1000);
// set population size
ea.setAlpha(50);
Opt4jModule testModule = new Opt4jModule();
TreeCrossoverOperatorModule crossover = new TreeCrossoverOperatorModule();
TreeMutateOperatorModule mutate = new TreeMutateOperatorModule();
TreeCopyOperatorModule copy = new TreeCopyOperatorModule();
ViewerModule viewer = new ViewerModule();
viewer.setCloseOnStop(false);
Opt4JTask task = new Opt4JTask(false);
task.init(ea, testModule, crossover, mutate, copy);
// task.init(ea, testModule, viewer, crossover, mutate, copy);
try {
task.execute();
Archive archive = task.getInstance(Archive.class);
for (Individual individual : archive) {
// obtain the phenotype and objective, etc. of each individual
Solution solution = (Solution) individual.getPhenotype();
if (solution.isValid()) {
if (solutions.isEmpty() || solution.computeObjective() < solutions.get(solutions.size() - 1).computeObjective()) {
Solution clone = solution.deepCopy();
solutions.add(clone);
logger.info("found a better solution with an objective of {}.", solution.computeObjective());
}
} else {
logger.warn("Found an invalid solution with " + solution.evaluateValidity() + " errors.");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
task.close();
}
if (solutions.size() > 0) {
lastSolution = solutions.get(solutions.size() - 1);
......
......@@ -12,7 +12,7 @@ public class Opt4jEvaluator implements Evaluator<Solution> {
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.evaluateSoftwareCompletenessa() * 1000);
objectives.add("Structural Errors", Sign.MIN, phenotype.evaluateSoftwareCompleteness() * 1000);
double objective = phenotype.computeObjective();
......@@ -22,7 +22,7 @@ public class Opt4jEvaluator implements Evaluator<Solution> {
if (phenotype.evaluateSoftwareValidity() != 0) {
objective *= 10;
}
if (phenotype.evaluateSoftwareCompletenessa() != 0) {
if (phenotype.evaluateSoftwareCompleteness() != 0) {
objective *= 10;
}
......
package de.tudresden.inf.st.mquat.solving.genetic.opt4j;
import de.tudresden.inf.st.mquat.generator.ScenarioDescription;
import de.tudresden.inf.st.mquat.jastadd.model.Root;
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 org.opt4j.core.Individual;
import org.opt4j.core.optimizer.Archive;
import org.opt4j.core.problem.ProblemModule;
import org.opt4j.core.start.Opt4JTask;
import org.opt4j.optimizers.ea.EvolutionaryAlgorithmModule;
import org.opt4j.viewer.ViewerModule;
public class Opt4jModule extends ProblemModule {
......@@ -24,45 +15,6 @@ public class Opt4jModule extends ProblemModule {
Opt4jModule.model = model;
}
public static void main(String[] args) {
final ScenarioDescription SCENARIO_DESCRIPTION = new ScenarioDescription(1, 2, 0, 0, 0, 2, 3, 1.5, 1, 1, 0);
de.tudresden.inf.st.mquat.generator.ScenarioGenerator sc = new de.tudresden.inf.st.mquat.generator.ScenarioGenerator(
SCENARIO_DESCRIPTION);
Opt4jModule.setModel(sc.generate());
EvolutionaryAlgorithmModule ea = new EvolutionaryAlgorithmModule();
ea.setGenerations(100000);
// set population size
ea.setAlpha(100);
Opt4jModule testModule = new Opt4jModule();
TreeCrossoverOperatorModule crossover = new TreeCrossoverOperatorModule();
TreeMutateOperatorModule mutate = new TreeMutateOperatorModule();
TreeCopyOperatorModule copy = new TreeCopyOperatorModule();
// testModule.setFunction(JohannesModule.Function.dtlz);
ViewerModule viewer = new ViewerModule();
viewer.isCloseOnStop();
Opt4JTask task = new Opt4JTask(false);
task.init(ea, testModule, viewer, crossover, mutate, copy);
try {
task.execute();
Archive archive = task.getInstance(Archive.class);
for (Individual individual : archive) {
// obtain the phenotype and objective, etc. of each individual
}
} catch (Exception e) {
e.printStackTrace();
} finally {
task.close();
}
}
@Override
protected void configure() {
bindProblem(Opt4jCreator.class, Opt4jDecoder.class, Opt4jEvaluator.class);
......
......@@ -2,7 +2,7 @@ package de.tudresden.inf.st.mquat.solving;
import de.tudresden.inf.st.mquat.solving.genetic.GeneticSolver;
public class SimpleHandwrittenTest extends HandwrittenTestSuite {
public class GeneticHandwrittenTest extends HandwrittenTestSuite {
@Override
protected Solver getSolver() {
......
......@@ -11,20 +11,20 @@ import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class SimpleSolverTest {
public class GeneticSolverTest {
private static Logger logger;
@BeforeClass
public static void initLogger() {
logger = LogManager.getLogger(SimpleSolverTest.class);
logger = LogManager.getLogger(GeneticSolverTest.class);
}
/**
* tests the genetic solver with one very genetic use case
*/
@Test
public void testSimpleSolver() throws SolvingException {
public void testGeneticSolver() throws SolvingException {
int tlc = 1;
int iac = 2;
int isd = 0;
......
......@@ -14,6 +14,7 @@ import java.util.Iterator;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public abstract class HandwrittenTestSuite {
private static Logger logger;
......@@ -141,6 +142,7 @@ public abstract class HandwrittenTestSuite {
public void test_05() throws IOException, Parser.Exception, SolvingException {
Tuple<Root, Solution> modelAndSolution = loadAndSolve("test_05.txt");
assertValidSolution(modelAndSolution);
assertTrue(modelAndSolution.getSecondElement().isValid());
Assignment configA = assertAssignment(modelAndSolution, 0, "configA0", "r0");
Assignment configB = assertAssignment(modelAndSolution, 0, "configB0", "r1");
Assignment configC = assertAssignment(modelAndSolution, 0, "configC0", "r4");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment