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

Merge remote-tracking branch 'origin/k-means' into ai

parents 302588a3 35cc0038
No related branches found
No related tags found
No related merge requests found
...@@ -73,6 +73,24 @@ To test your solver with the name `fancy-solver` along with the reference implem ...@@ -73,6 +73,24 @@ To test your solver with the name `fancy-solver` along with the reference implem
The value `total` is used to constrain the total number of models to be generated. Set this to `null` (the default) to generate all value for the defined parameter ranges. The value `total` is used to constrain the total number of models to be generated. Set this to `null` (the default) to generate all value for the defined parameter ranges.
Refer to [`de.tudresden.inf.st.mquat.generator.ScenarioDescription`](https://git-st.inf.tu-dresden.de/stgroup/ttc18/blob/master/jastadd-mquat-base/src/main/java/de/tudresden/inf/st/mquat/generator/ScenarioDescription.java) for a description of the possible parameters. Refer to [`de.tudresden.inf.st.mquat.generator.ScenarioDescription`](https://git-st.inf.tu-dresden.de/stgroup/ttc18/blob/master/jastadd-mquat-base/src/main/java/de/tudresden/inf/st/mquat/generator/ScenarioDescription.java) for a description of the possible parameters.
## Checking a solution using Java methods
The check, if a `Solution` object is valid, either call [`solution.valid()`](https://git-st.inf.tu-dresden.de/stgroup/ttc18/blob/master/jastadd-mquat-base/src/main/jastadd/solution/Checking.jrag#L3) returning a boolean, if the solution is valid, or call [`solution.explain`](https://git-st.inf.tu-dresden.de/stgroup/ttc18/blob/master/jastadd-mquat-base/src/main/jastadd/solution/Checking.jadd#L2) to print out if the solution is valid and violations, if there are any.
You may also look into the [code used for external tools](https://git-st.inf.tu-dresden.de/stgroup/ttc18/blob/master/jastadd-mquat-base/src/main/java/de/tudresden/inf/st/mquat/MainCheck.java) to see a minimal working example.
### Checking a solution for external tools
If you can not use Java method, we provided a Gradle task `checkSolution` expecting two paths to files: the problem description and the solution.
Alternatively, you can use the wrapper shell-script `checkSolution.sh` with the same parameters (problem description and its solution).
In both cases, the output will be the following:
- The read solution pretty-printed (to verify correct parsing)
- Whether the solution is valid, and the violations in the solution, if any.
- The objective value of the solution.
Thus, the check will only check for validity and not for optimality as this would require solving the problem again.
## Notes and Troubleshooting ## Notes and Troubleshooting
- Please use the gradle wrapper script, as different version of Gradle might not work with the setup - Please use the gradle wrapper script, as different version of Gradle might not work with the setup
......
...@@ -3,7 +3,7 @@ aspect Checking { ...@@ -3,7 +3,7 @@ aspect Checking {
// TODO fix for multiresource // TODO fix for multiresource
Set<Request> requestSet = new HashSet<>(); Set<Request> requestSet = new HashSet<>();
Map<Resource, Assignment> resourceSet = new HashMap<>(); Map<Resource, Assignment> resourceSet = new HashMap<>();
logger.info(this.toString()); // logger.info(this.toString());
// check assignments // check assignments
for (Assignment assignment : allAssignments()) { for (Assignment assignment : allAssignments()) {
if (!assignment.isValid()) { if (!assignment.isValid()) {
......
...@@ -4,6 +4,7 @@ import beaver.Parser; ...@@ -4,6 +4,7 @@ import beaver.Parser;
import de.tudresden.inf.st.mquat.jastadd.model.*; import de.tudresden.inf.st.mquat.jastadd.model.*;
import de.tudresden.inf.st.mquat.jastadd.scanner.MquatScanner; import de.tudresden.inf.st.mquat.jastadd.scanner.MquatScanner;
import de.tudresden.inf.st.mquat.utils.ParserUtils; import de.tudresden.inf.st.mquat.utils.ParserUtils;
import org.apache.logging.log4j.LogManager;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
...@@ -38,8 +39,8 @@ public class MainCheck { ...@@ -38,8 +39,8 @@ public class MainCheck {
System.out.println(out); System.out.println(out);
boolean isValid = solution.isValid(); boolean isValid = solution.isValid();
double objectiveValue = solution.computeObjective(); double objectiveValue = solution.computeObjective();
System.out.println("Solution valid: " + Boolean.toString(isValid)); solution.explain();
System.out.println("Objective value: " + objectiveValue); LogManager.getLogger(MainCheck.class).info("Objective value: " + objectiveValue);
} }
private static void printUsage() { private static void printUsage() {
......
...@@ -30,7 +30,7 @@ task benchmarkCustom(type: JavaExec, dependsOn: assemble) { ...@@ -30,7 +30,7 @@ task benchmarkCustom(type: JavaExec, dependsOn: assemble) {
classpath = sourceSets.test.runtimeClasspath classpath = sourceSets.test.runtimeClasspath
main = 'de.tudresden.inf.st.mquat.benchmark.CustomBenchmarkMain' main = 'de.tudresden.inf.st.mquat.benchmark.CustomBenchmarkMain'
systemProperty "java.library.path", project.glpkPath systemProperty "java.library.path", project.glpkPath
jvmArgs("-XX:+UnlockCommercialFeatures", "-XX:+FlightRecorder", "-XX:StartFlightRecording=settings=profile", "-XX:FlightRecorderOptions=defaultrecording=true,dumponexit=true,dumponexitpath=results/fr", "-Xverify:none") // jvmArgs("-XX:+UnlockCommercialFeatures", "-XX:+FlightRecorder", "-XX:StartFlightRecording=settings=profile", "-XX:FlightRecorderOptions=defaultrecording=true,dumponexit=true,dumponexitpath=results/fr", "-Xverify:none")
} }
task benchmarkFull(type: JavaExec, dependsOn: assemble) { task benchmarkFull(type: JavaExec, dependsOn: assemble) {
......
This diff is collapsed.
...@@ -13,6 +13,7 @@ dependencies { ...@@ -13,6 +13,7 @@ dependencies {
compile group: 'org.gnu.glpk', name: 'glpk-java', version: '1.11.0' compile group: 'org.gnu.glpk', name: 'glpk-java', version: '1.11.0'
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'
compile 'nz.ac.waikato.cms.weka:weka-stable:3.8.3'
compile project(':jastadd-mquat-base') compile project(':jastadd-mquat-base')
compile project(':jastadd-mquat-solver') compile project(':jastadd-mquat-solver')
testCompile project(path: ':jastadd-mquat-solver', configuration: 'testArtifacts') testCompile project(path: ':jastadd-mquat-solver', configuration: 'testArtifacts')
......
package de.tudresden.inf.st.mquat.solving.clustering;
import de.tudresden.inf.st.mquat.jastadd.model.Resource;
import weka.core.DenseInstance;
import weka.core.Instance;
/***
* An entity unifying WEKA instances and Jastadd resources
*/
public class HardwareComponent {
private Resource resource;
private Instance instance;
public HardwareComponent(Resource resource, int attributes_number) {
this.resource = resource;
this.instance = new DenseInstance(attributes_number);
}
public Resource getResource() {
return resource;
}
public void setResource(Resource resource) {
this.resource = resource;
}
public Instance getInstance() {
return instance;
}
public void setInstance(Instance instance) {
this.instance = instance;
}
}
package de.tudresden.inf.st.mquat.solving.clustering;
import de.tudresden.inf.st.mquat.jastadd.model.Resource;
import de.tudresden.inf.st.mquat.jastadd.model.Root;
import weka.clusterers.*;
import weka.core.Attribute;
import weka.core.Instances;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/***
* Wrapper for WEKA SimpleKMeans. Clustering based on:
* CPU frequency
* RAM total
* Disk total
* Network throughput
*
* @author Dmytro Pukhkaiev
* @version 0.1
*/
public class KMeans{
// dataset
private Instances resources;
// number of clusters
private int k;
// list of resources
private ArrayList<HardwareComponent> hardwareComponentList = new ArrayList<>();
public KMeans(Root model) {
// create attributes for instances
Attribute cpu = new Attribute("cpu");
Attribute ram = new Attribute("ram");
Attribute disk = new Attribute("disk");
Attribute network = new Attribute("network");
ArrayList<Attribute> attributeList = new ArrayList<Attribute>();
attributeList.add(cpu);
attributeList.add(ram);
attributeList.add(disk);
attributeList.add(network);
// create a dataset
resources = new Instances("resources", attributeList, 0);
//instantiate items in the dataset
for (Resource resource : model.getHardwareModel().getResourceList()) {
if (resource.getType().getName().getName().equals("ComputeNode")) {
HardwareComponent hardwareComponent = new HardwareComponent(resource, resource.getNumSubResource());
hardwareComponent.getInstance().setDataset(resources);
for (Resource sub : resource.getSubResourceList()) {
String typeName = sub.getType().getName().getName();
switch (typeName) {
case "CPU":
hardwareComponent.getInstance().setValue(cpu, sub.getCurrentValueByPropertyName("frequency"));
break;
case "RAM":
hardwareComponent.getInstance().setValue(ram, sub.getCurrentValueByPropertyName("total"));
break;
case "DISK":
hardwareComponent.getInstance().setValue(disk, sub.getCurrentValueByPropertyName("total"));
break;
case "NETWORK":
hardwareComponent.getInstance().setValue(network, sub.getCurrentValueByPropertyName("throughput"));
break;
default:
throw new RuntimeException("Unable to transform unknown resource of type" + typeName);
}
}
hardwareComponentList.add(hardwareComponent);
}
}
for (HardwareComponent hwc : hardwareComponentList)
resources.add(hwc.getInstance());
k = (int) (0.2 * model.getHardwareModel().getResourceList().asJavaCollection().size());
}
public Map<Integer, ArrayList<Resource>> buildClusters(){
SimpleKMeans algorithm = new SimpleKMeans();
Map<Integer, ArrayList<Resource>> Clusters = new HashMap<>();
for (int i = 0; i < k; i ++)
Clusters.put(i, new ArrayList<>());
try {
algorithm.setNumClusters(k);
algorithm.setPreserveInstancesOrder(true);
algorithm.buildClusterer(resources);
int[] assignments = algorithm.getAssignments();
for (int i = 0; i < assignments.length; i++){
Clusters.get(assignments[i]).add(hardwareComponentList.get(i).getResource());
}
} catch (Exception e) {
e.printStackTrace();
}
return Clusters;
}
}
...@@ -2,12 +2,10 @@ package de.tudresden.inf.st.mquat.solving.ilp; ...@@ -2,12 +2,10 @@ package de.tudresden.inf.st.mquat.solving.ilp;
import de.tudresden.inf.st.mquat.generator.ScenarioDescription; import de.tudresden.inf.st.mquat.generator.ScenarioDescription;
import de.tudresden.inf.st.mquat.generator.ScenarioGenerator; import de.tudresden.inf.st.mquat.generator.ScenarioGenerator;
import de.tudresden.inf.st.mquat.jastadd.model.ILP; import de.tudresden.inf.st.mquat.jastadd.model.*;
import de.tudresden.inf.st.mquat.jastadd.model.MquatWriteSettings;
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.Solver; import de.tudresden.inf.st.mquat.solving.Solver;
import de.tudresden.inf.st.mquat.solving.SolvingException; import de.tudresden.inf.st.mquat.solving.SolvingException;
import de.tudresden.inf.st.mquat.solving.clustering.KMeans;
import de.tudresden.inf.st.mquat.utils.StopWatch; import de.tudresden.inf.st.mquat.utils.StopWatch;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
...@@ -16,6 +14,8 @@ import org.gnu.glpk.GLPK; ...@@ -16,6 +14,8 @@ import org.gnu.glpk.GLPK;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -90,6 +90,8 @@ public class ILPMain { ...@@ -90,6 +90,8 @@ public class ILPMain {
System.out.println(version); System.out.println(version);
ScenarioGenerator gen = new ScenarioGenerator(new ScenarioDescription(1, 2, 0, 0, 0, 2, 2, 2.5, 3, 1, 0)); ScenarioGenerator gen = new ScenarioGenerator(new ScenarioDescription(1, 2, 0, 0, 0, 2, 2, 2.5, 3, 1, 0));
Root model = gen.generate(); Root model = gen.generate();
KMeans kMeans = new KMeans(model);
Map<Integer, ArrayList<Resource>> clusters = kMeans.buildClusters();
Solver external = new GLPKSolver().setDeleteFilesOnExit(false); Solver external = new GLPKSolver().setDeleteFilesOnExit(false);
Solution solution = external.solve(model); Solution solution = external.solve(model);
logger.info(model.print(new MquatWriteSettings(" "))); logger.info(model.print(new MquatWriteSettings(" ")));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment