Skip to content
Snippets Groups Projects
Commit 8376ae35 authored by Albert Zuendorf's avatar Albert Zuendorf
Browse files

first steps

parent 54bdf4b5
No related branches found
No related tags found
No related merge requests found
glpkPath = /usr/local/lib/jni glpkPath = C:/ProgrammeAlbert/glpk-4.65/w64
build/
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
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 project(':jastadd-mquat-base')
compile project(':jastadd-mquat-solver')
compile "org.eclipse.emf:org.eclipse.emf.ecore:2.9.0-v20130528-0742"
compile "org.eclipse.emf:org.eclipse.emf.common:2.9.0-v20130528-0742"
// compile "org.eclipse.emf:org.eclipse.emf.ecore.xmi:2.9.0-v20130528-0742"
compile files("./libs/libEMFER.jar")
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile project(path: ':jastadd-mquat-solver', configuration: 'testArtifacts')
}
group 'de.tudresden.inf.st'
version '1.0.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
File added
Hello World
\ No newline at end of file
package uniks;
import de.tudresden.inf.st.mquat.jastadd.model.*;
import de.tudresden.inf.st.mquat.solving.BenchmarkableSolver;
import de.tudresden.inf.st.mquat.solving.Solver;
import de.tudresden.inf.st.mquat.solving.SolverUtils;
import de.tudresden.inf.st.mquat.solving.SolvingException;
import de.tudresden.inf.st.mquat.utils.StopWatch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import uniks.ttc18.ECompMapping;
import uniks.ttc18.ESolution;
import uniks.ttc18.Ttc18Factory;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class EMFeRSolver implements BenchmarkableSolver {
private static final Logger logger = LogManager.getLogger(EMFeRSolver.class);
private Solution lastSolution;
private long lastSolvingTime;
private int solutionCounter;
private StopWatch stopWatch;
private long maxSolvingTime;
private boolean timedOut;
public EMFeRSolver() {
this(Long.MAX_VALUE);
}
public EMFeRSolver(long maxSolvingTime) {
this.maxSolvingTime = maxSolvingTime;
reset();
}
private static void assignResource(Assignment assignment, Resource resource) {
Implementation impl = assignment.getImplementation();
ResourceMapping mapping = new ResourceMapping(impl.getResourceRequirement().getInstance(0), resource, new de.tudresden.inf.st.mquat.jastadd.model.List<>());
SolverUtils.populateResourceMapping(mapping, impl.getResourceRequirement(), resource);
assignment.setResourceMapping(mapping);
}
private int checkAssignment(Solution solution, List<Solution> solutions, List<Assignment> assignments, List<Set<Resource>> possibleResources, int index, Stack<Resource> usedResources) {
int checkCounter = 0;
Assignment assignment = assignments.get(index);
for (Resource resource : possibleResources.get(index)) {
if (stopWatch.time(TimeUnit.MILLISECONDS) > maxSolvingTime) {
return checkCounter;
}
if (usedResources.contains(resource)) continue;
assignResource(assignment, resource);
usedResources.push(resource);
checkCounter++;
if (index == assignments.size() - 1) {
if (solution.isValid()) {
solutionCounter++;
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 {
checkCounter += checkAssignment(solution, solutions, assignments, possibleResources, index + 1, usedResources);
}
usedResources.pop();
}
return checkCounter;
}
@Override
public Solution solve(Root model) throws SolvingException {
reset();
if (model.getNumRequest() == 0) {
return Solution.emptySolutionOf(model);
}
ESolution eSolution = Ttc18Factory.eINSTANCE.createESolution();
EMFeRTrafos emFeRTrafos = new EMFeRTrafos(model);
emFeRTrafos.createTopLevelMappings(eSolution);
for (ECompMapping compMapping : eSolution.getCompMappings())
{
emFeRTrafos.createAssignments(eSolution, compMapping);
}
int numAssignments = 0;
int numSoftwareSolutions = 0;
int numTotalSoftwareSolutions = 0;
stopWatch = StopWatch.start();
List<Solution> solutions = new ArrayList<>();
// iterate all possible assignments
// Note, that this only considers assignments of one configuration to each hardware component
Solution currentSolution = Solution.createSoftwareSolution(model);
Solution emferSolution = emFeRTrafos.transform(eSolution);
// currentSolution.trace().process(new LoggerProcessor());
de.tudresden.inf.st.mquat.jastadd.model.List<Resource> resources = model.getHardwareModel().getResources();
boolean hasNextSoftwareAssignment;
do {
numTotalSoftwareSolutions++;
if (currentSolution.isSoftwareValid()) {
numSoftwareSolutions++;
List<Assignment> assignments = currentSolution.allAssignments();
// initialize the lists of possible assignments
List<Set<Resource>> possibleResources = new ArrayList<>(assignments.size());
for (Assignment assignment : assignments) {
Set<Resource> resourceList = new HashSet<>();
for (Resource resource : resources) {
assignResource(assignment, resource);
if (assignment.isValid()) {
resourceList.add(resource);
}
}
possibleResources.add(resourceList);
}
numAssignments += checkAssignment(currentSolution, solutions, assignments, possibleResources, 0, new Stack<>());
}
if (stopWatch.time(TimeUnit.MILLISECONDS) > maxSolvingTime) {
this.timedOut = true;
logger.warn("Timeout! Solving terminated!");
break;
}
hasNextSoftwareAssignment = currentSolution.nextSoftwareAssignment();
} while (hasNextSoftwareAssignment);
logger.info("Number of total software solutions: {}", numTotalSoftwareSolutions);
logger.info("Number of iterated software solutions: {}", numSoftwareSolutions);
logger.info("Number of iterated solutions: {}", numAssignments);
logger.info("Number of correct solutions: {}", solutionCounter);
if (solutions.size() > 0) {
lastSolution = solutions.get(solutions.size() - 1);
} else {
lastSolution = Solution.emptySolutionOf(model);
logger.warn("Found no solution!");
}
lastSolvingTime = stopWatch.time(TimeUnit.MILLISECONDS);
return lastSolution;
}
private void reset() {
this.lastSolution = null;
this.solutionCounter = 0;
this.lastSolvingTime = 0;
this.timedOut = false;
}
@Override
public String getName() {
return "simple";
}
@Override
public long getLastSolvingTime() {
return lastSolvingTime;
}
@Override
public double getLastObjective() {
if (lastSolution != null) {
return lastSolution.computeObjective();
} else {
// TODO throw exception or do something reasonable
return 0d;
}
}
@Override
public Solver setTimeout(long timeoutValue, TimeUnit timeoutUnit) {
this.maxSolvingTime = timeoutUnit.toMillis(timeoutValue);
return this;
}
@Override
public boolean hadTimeout() {
return this.timedOut;
}
}
package uniks;
import de.tudresden.inf.st.mquat.jastadd.model.*;
import uniks.ttc18.EAssignment;
import uniks.ttc18.ECompMapping;
import uniks.ttc18.ESolution;
import uniks.ttc18.Ttc18Factory;
import java.io.IOException;
import java.nio.file.*;
public class EMFeRTrafos
{
private Root model;
public EMFeRTrafos(Root model)
{
this.model = model;
}
public void createTopLevelMappings(ESolution eSolution)
{
org.eclipse.emf.common.util.EList<ECompMapping> compMappings = eSolution.getCompMappings();
if ( ! compMappings.isEmpty())
{
return;
}
for ( Request request : model.getRequests())
{
String requName = request.getName().toString();
ComponentRef target = request.getTarget();
Name targetName = target.getName();
ECompMapping eCompMapping = Ttc18Factory.eINSTANCE.createECompMapping();
eCompMapping.setRequName(requName);
eCompMapping.setCompName(targetName.toString());
compMappings.add(eCompMapping);
}
}
public void createAssignments(ESolution eSolution, ECompMapping compMapping)
{
String compName = compMapping.getCompName();
Component comp = findComp(model, compName);
Implementation implementation = comp.getImplementation(0);
EAssignment eAssignment = Ttc18Factory.eINSTANCE.createEAssignment();
String implName = implementation.getName().toString();
eAssignment.setImplName(implName);
compMapping.setAssignment(eAssignment);
for (ComponentRequirement componentRequirement : implementation.getComponentRequirements())
{
String requCompName = componentRequirement.getComponentRef().getRef().getName().toString();
ECompMapping subMapping = Ttc18Factory.eINSTANCE.createECompMapping();
subMapping.setRequName(implName);
subMapping.setCompName(requCompName);
eAssignment.getCompMappings().add(subMapping);
createAssignments(eSolution, subMapping);
}
}
private Component findComp(Root model, String compName)
{
for (Component comp : model.getSoftwareModel().getComponents())
{
if (comp.getName().toString().equals(compName))
{
return comp;
}
}
return null;
}
public Solution transform(ESolution eSolution)
{
Solution result = new Solution();
result.setModel(model);
// top level
for (ECompMapping eCompMap : eSolution.getCompMappings())
{
Assignment assignment = new Assignment();
assignment.setRequest(findRequest(eCompMap.getRequName()));
assignment.setTopLevel(true);
result.addAssignment(assignment);
}
return result;
}
private Request findRequest(String requName)
{
for (Request r : model.getRequests())
{
if (r.getName().toString().equals(requName))
{
return r;
}
}
return null;
}
}
package de.tudresden.inf.st.mquat.solving;
import uniks.EMFeRSolver;
public class EMFeRHandwrittenTest extends HandwrittenTestSuite {
@Override
protected Solver getSolver() {
return new EMFeRSolver(10000);
}
}
package de.tudresden.inf.st.mquat.solving;
import de.tudresden.inf.st.mquat.generator.ScenarioDescription;
import de.tudresden.inf.st.mquat.generator.ScenarioGenerator;
import de.tudresden.inf.st.mquat.jastadd.model.Root;
import de.tudresden.inf.st.mquat.jastadd.model.Solution;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import uniks.EMFeRSolver;
import java.io.IOException;
import java.nio.file.*;
public class EMFeRSolverTest {
private static Logger logger;
@BeforeClass
public static void initLogger() {
logger = LogManager.getLogger(EMFeRSolverTest.class);
}
/**
* tests the simple solver with one very simple use case
*/
@Test
public void testSimpleSolver() throws SolvingException {
int tlc = 1;
int iac = 2;
int isd = 0;
int cac = 0;
int csd = 0;
int dep = 2;
int imp = 1;
int mod = 3;
double res = 1.5d;
int nfp = 0;
int req = 1;
int cpu = 1;
int seed = 0;
ScenarioGenerator generator = new ScenarioGenerator(new ScenarioDescription(tlc, iac, isd, cac, csd, dep, imp, res, req, cpu, seed));
Root model = generator.generate();
EMFeRSolver solver = new EMFeRSolver(20000);
Solution solution = solver.solve(model);
Assert.assertNotNull(solution);
logger.info("the best solution is {} and has an objective of {}.", (solution.isValid() ? "valid" : "invalid"), solution.computeObjective());
}
@Test
public void testListener()
{
Path path = Paths.get(".");
try
{
WatchService watcher = FileSystems.getDefault().newWatchService();
path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
Files.write(Paths.get("./message.txt"), "Hello World".getBytes());
WatchKey event = watcher.take();
for (WatchEvent we : event.pollEvents())
{
System.out.println("" + we.kind() + " " + we.context());
String context = we.context().toString();
String content = new String(Files.readAllBytes(Paths.get(context)));
System.out.println(content);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
...@@ -5,4 +5,6 @@ include ':jastadd-mquat-benchmark' ...@@ -5,4 +5,6 @@ include ':jastadd-mquat-benchmark'
include ':jastadd-mquat-solver' include ':jastadd-mquat-solver'
include ':jastadd-mquat-solver-ilp' include ':jastadd-mquat-solver-ilp'
include ':jastadd-mquat-solver-simple' include ':jastadd-mquat-solver-simple'
include ':jastadd-mquat-solver-emfer'
include 'jastadd-mquat-solver-emfer'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment