Skip to content
Snippets Groups Projects
Commit b6e62b7c authored by IevgSvet's avatar IevgSvet
Browse files

Merge develop; Add Main, gradle run for Genetic solver

parent 3ac3a7e8
No related branches found
No related tags found
No related merge requests found
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
...@@ -7,6 +8,16 @@ repositories { ...@@ -7,6 +8,16 @@ repositories {
mavenCentral() mavenCentral()
} }
jar {
manifest {
attributes "Main-Class": 'de.tudresden.inf.st.mquat.solving.genetic.GeneticMain'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
dependencies { dependencies {
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0' compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
...@@ -22,3 +33,12 @@ dependencies { ...@@ -22,3 +33,12 @@ dependencies {
compile group: 'org.opt4j', name: 'opt4j-viewer', version: '3.1.4' compile group: 'org.opt4j', name: 'opt4j-viewer', version: '3.1.4'
compile group: 'org.opt4j', name: 'opt4j-benchmarks', version: '3.1.4' compile group: 'org.opt4j', name: 'opt4j-benchmarks', version: '3.1.4'
} }
run {
mainClassName = 'de.tudresden.inf.st.mquat.solving.genetic.GeneticMain'
standardInput = System.in
if (project.hasProperty("appArgs")) {
args Eval.me(appArgs)
}
}
\ No newline at end of file
package de.tudresden.inf.st.mquat.solving.genetic;
import de.tudresden.inf.st.mquat.generator.ScenarioDescription;
import de.tudresden.inf.st.mquat.generator.ScenarioGenerator;
import de.tudresden.inf.st.mquat.jastadd.model.MquatWriteSettings;
import de.tudresden.inf.st.mquat.jastadd.model.Root;
import de.tudresden.inf.st.mquat.solving.BenchmarkableSolver;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.tudresden.inf.st.mquat.jastadd.model.Solution;
import de.tudresden.inf.st.mquat.solving.SolvingException;
import java.io.BufferedWriter;
import java.nio.file.StandardOpenOption;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import static java.nio.file.Files.exists;
public class GeneticMain {
protected static final char SEPARATOR = ',';
protected static void writeHeader(BufferedWriter writer) throws IOException {
writer.append("name").append(SEPARATOR)
.append("Solved").append(SEPARATOR)
.append("Obj").append(SEPARATOR)
.append("Valid").append(SEPARATOR)
.append("TimeOut").append("\n");
}
private static void createDirIfNecessary(Path directory) throws IOException {
if (!exists(directory)) {
Files.createDirectories(directory);
}
}
public static void main(String[] args) throws Exception {
Logger logger = LogManager.getLogger(GeneticMain.class);
String scenario_name = "size_v2_q1_d1_r15";
int numTopLevelComponents = 1;
int avgNumImplSubComponents = 0;
int implSubComponentStdDerivation = 0;
int avgNumCompSubComponents = 2;
int compSubComponentStdDerivation = 0;
int componentDepth = 2; //from scenario - depth
int numImplementations = 2; //from scenario - variants
double excessComputeResourceRatio = 1.5; //from scenario - resources
int numRequests = 1; //from scenario - requests
int numCpus = 1;
long seed = 0; //settings - seed
long timeoutValue = 20; //from scenarios.json
String timeoutUnit = "SECONDS"; //from scenarios.json, possible values - "SECONDS", "MINUTES"
int generations = 200000;
int populationSize = 1000;
if (args.length == 2) {
System.out.println("============ Args: " + args[0] + " and " + args[1]);
generations = Integer.parseInt(args[0]);
populationSize = Integer.parseInt(args[1]);
System.out.println("============ generations = " + generations + ", populationSize = " + populationSize);
}
ScenarioGenerator generator = new ScenarioGenerator(new ScenarioDescription(numTopLevelComponents,
avgNumImplSubComponents, implSubComponentStdDerivation, avgNumCompSubComponents,
compSubComponentStdDerivation, componentDepth, numImplementations, excessComputeResourceRatio,
numRequests, numCpus, seed));
Root model = generator.generate();
logger.info(model.print(new MquatWriteSettings(" ")));
BenchmarkableSolver solver = new GeneticSolver(GeneticSolver.SelectorType.NSGA2, generations, populationSize);
solver.setTimeout(timeoutValue, TimeUnit.valueOf(timeoutUnit));
Path path_for_results = Paths.get("results/scenarios");
createDirIfNecessary(path_for_results);
Path results_file = Paths.get(path_for_results.toString() + "/" + scenario_name + ".csv");
BufferedWriter bw = null;
if (!Files.exists(results_file)) {
bw = Files.newBufferedWriter(results_file);
writeHeader(bw);
}
if (bw == null)
bw = Files.newBufferedWriter(results_file, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
StringBuilder sb = new StringBuilder("genetic_NSGA2").append(SEPARATOR); // name
Solution solution = null;
try {
solution = solver.solve(model);
if (solution == null) {
solution = Solution.emptySolutionOf(model);
} else {
solution.explain();
}
sb.append(solver.getLastSolvingTime()).append(SEPARATOR) // solution time
.append(solution.computeObjective()).append(SEPARATOR) // objective
.append(solution.isValid()); // valid
} catch (SolvingException e) {
logger.catching(e);
sb.append(-1).append(SEPARATOR) // solution time
.append(-1).append(SEPARATOR) // objective
.append(false); // valid
}
sb.append(SEPARATOR).append(solver.hadTimeout()) // timeout
.append("\n");
bw.append(sb.toString());
bw.close();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment