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

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

parents 4d827485 59877908
No related branches found
No related tags found
No related merge requests found
package de.tudresden.inf.st.mquat.solving.clustering;
import java.util.Map;
public abstract class AbstractNormalizer implements Normalizer {
Map<String, Double> minValue;
Map<String, Double> maxValue;
public AbstractNormalizer(Map<String, Double> minValue, Map<String, Double> maxValue){
this.minValue = minValue;
this.maxValue = maxValue;
}
}
...@@ -4,6 +4,9 @@ import de.tudresden.inf.st.mquat.jastadd.model.Resource; ...@@ -4,6 +4,9 @@ import de.tudresden.inf.st.mquat.jastadd.model.Resource;
import weka.core.DenseInstance; import weka.core.DenseInstance;
import weka.core.Instance; import weka.core.Instance;
import java.util.HashMap;
import java.util.Map;
/*** /***
* An entity unifying WEKA instances and Jastadd resources * An entity unifying WEKA instances and Jastadd resources
*/ */
...@@ -11,10 +14,15 @@ public class HardwareComponent { ...@@ -11,10 +14,15 @@ public class HardwareComponent {
private Resource resource; private Resource resource;
private Instance instance; private Instance instance;
//TODO: do it with Jastadd
private Map<String, Double> value;
private Map<String, Double> normalizedValue;
public HardwareComponent(Resource resource, int attributes_number) { HardwareComponent(Resource resource, int attributes_number) {
this.resource = resource; this.resource = resource;
this.instance = new DenseInstance(attributes_number); this.instance = new DenseInstance(attributes_number);
value = new HashMap<>();
normalizedValue = new HashMap<>();
} }
public Resource getResource() { public Resource getResource() {
...@@ -32,4 +40,20 @@ public class HardwareComponent { ...@@ -32,4 +40,20 @@ public class HardwareComponent {
public void setInstance(Instance instance) { public void setInstance(Instance instance) {
this.instance = instance; this.instance = instance;
} }
public Map<String, Double> getValue() {
return value;
}
public void setValue(Map<String, Double> value) {
this.value = value;
}
public Map<String, Double> getNormalizedValue() {
return normalizedValue;
}
public void setNormalizedValue(Map<String, Double> normalizedValue) {
this.normalizedValue = normalizedValue;
}
} }
...@@ -26,26 +26,24 @@ import java.util.Map; ...@@ -26,26 +26,24 @@ import java.util.Map;
*/ */
public class KMeans { public class KMeans {
private static final Logger logger = LogManager.getLogger(KMeans.class); private static final Logger logger = LogManager.getLogger(KMeans.class);
// dataset // dataset
private Instances resources; private Instances resources;
// the model // the model
private Root model; private Root model;
// list of resources // list of resources
private ArrayList<HardwareComponent> hardwareComponentList = new ArrayList<>(); private ArrayList<HardwareComponent> hardwareComponentList = new ArrayList<>();
/***
* Prepare clustering algorithm, extract WEKA instances from Jastadd model
* @param model Jastadd model
*/
public KMeans(Root model) { public KMeans(Root model) {
this.model = model; this.model = model;
// create attributes for instances // create attributes for instances
Attribute cpu = new Attribute("cpu"); Attribute cpu = new Attribute("cpu");
Attribute ram = new Attribute("ram"); Attribute ram = new Attribute("ram");
Attribute disk = new Attribute("disk"); Attribute disk = new Attribute("disk");
Attribute network = new Attribute("network"); Attribute network = new Attribute("network");
ArrayList<Attribute> attributeList = new ArrayList<>(); ArrayList<Attribute> attributeList = new ArrayList<>();
// we expect one top-level resource type and consider at most four sub-resources of the following names // we expect one top-level resource type and consider at most four sub-resources of the following names
...@@ -78,19 +76,23 @@ public class KMeans { ...@@ -78,19 +76,23 @@ public class KMeans {
String typeName = sub.getType().getName().getName(); String typeName = sub.getType().getName().getName();
switch (typeName) { switch (typeName) {
case "CPU": case "CPU":
hardwareComponent.getInstance().setValue(cpu, sub.getCurrentValueByPropertyName("frequency")); hardwareComponent.getValue().put("CPU", sub.getCurrentValueByPropertyName("frequency"));
//hardwareComponent.getInstance().setValue(cpu, sub.getCurrentValueByPropertyName("frequency"));
break; break;
case "RAM": case "RAM":
hardwareComponent.getInstance().setValue(ram, sub.getCurrentValueByPropertyName("total")); hardwareComponent.getValue().put("RAM", sub.getCurrentValueByPropertyName("total"));
//hardwareComponent.getInstance().setValue(ram, sub.getCurrentValueByPropertyName("total"));
break; break;
case "DISK": case "DISK":
hardwareComponent.getInstance().setValue(disk, sub.getCurrentValueByPropertyName("total")); hardwareComponent.getValue().put("DISK", sub.getCurrentValueByPropertyName("total"));
//hardwareComponent.getInstance().setValue(disk, sub.getCurrentValueByPropertyName("total"));
break; break;
case "NETWORK": case "NETWORK":
hardwareComponent.getInstance().setValue(network, sub.getCurrentValueByPropertyName("throughput")); hardwareComponent.getValue().put("NETWORK", sub.getCurrentValueByPropertyName("throughput"));
//hardwareComponent.getInstance().setValue(network, sub.getCurrentValueByPropertyName("throughput"));
break; break;
default: default:
throw new EvalutationException("Unable to transform unknown resource of type" + typeName); throw new RuntimeException("Unable to transform unknown resource of type" + typeName);
} }
} }
...@@ -98,9 +100,48 @@ public class KMeans { ...@@ -98,9 +100,48 @@ public class KMeans {
} }
} }
for (HardwareComponent hwc : hardwareComponentList) Map<String, Double> minValue = new HashMap<>();
resources.add(hwc.getInstance()); Map<String, Double> maxValue = new HashMap<>();
for (HardwareComponent hwc : hardwareComponentList) {
for (String key : hwc.getValue().keySet()) {
if (!minValue.containsKey(key))
minValue.put(key, hwc.getValue().get(key));
else if (minValue.get(key) > hwc.getValue().get(key))
minValue.put(key, hwc.getValue().get(key));
if (!maxValue.containsKey(key))
maxValue.put(key, hwc.getValue().get(key));
else if (maxValue.get(key) < hwc.getValue().get(key))
maxValue.put(key, hwc.getValue().get(key));
}
}
LinearNormalizer linearNormalizer = new LinearNormalizer(minValue, maxValue);
for (HardwareComponent hwc : hardwareComponentList) {
hwc.setNormalizedValue(linearNormalizer.normalize(hwc));
for (String key : hwc.getNormalizedValue().keySet()) {
switch (key) {
case "CPU":
hwc.getInstance().setValue(cpu, hwc.getNormalizedValue().get(key));
break;
case "RAM":
hwc.getInstance().setValue(ram, hwc.getNormalizedValue().get(key));
break;
case "DISK":
hwc.getInstance().setValue(disk, hwc.getNormalizedValue().get(key));
break;
case "NETWORK":
hwc.getInstance().setValue(network, hwc.getNormalizedValue().get(key));
break;
default:
throw new RuntimeException("Unable to transform unknown resource of type" + key);
}
}
resources.add(hwc.getInstance());
}
} }
public Map<Integer, ArrayList<Resource>> buildClusters(int numClusters) { public Map<Integer, ArrayList<Resource>> buildClusters(int numClusters) {
......
package de.tudresden.inf.st.mquat.solving.clustering;
import de.tudresden.inf.st.mquat.jastadd.model.Resource;
import java.util.HashMap;
import java.util.Map;
public class LinearNormalizer extends AbstractNormalizer implements Normalizer {
public LinearNormalizer(Map<String, Double> minValue, Map<String, Double> maxValue)
{
super(minValue, maxValue);
}
@Override
public Map<String, Double> normalize(HardwareComponent hardwareComponent) {
Map<String, Double> normalizedHWC = new HashMap<>();
for (String key : hardwareComponent.getValue().keySet()){
double normalized =
(hardwareComponent.getValue().get(key) - minValue.get(key))/(maxValue.get(key) - minValue.get(key));
normalizedHWC.put(key, normalized);
}
return normalizedHWC;
}
}
package de.tudresden.inf.st.mquat.solving.clustering;
import java.util.Map;
public interface Normalizer {
Map<String, Double> normalize(HardwareComponent resource);
}
package de.tudresden.inf.st.mquat.solving.clustering;
import java.util.HashMap;
import java.util.Map;
public class StudentNormalizer implements Normalizer {
@Override
public Map<String, Double> normalize(HardwareComponent hardwareComponent) {
return new HashMap<>();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment