Skip to content

Commit

Permalink
Update ConfigSampler.java
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianxun-ict authored Oct 8, 2018
1 parent 2b5026a commit c17aa81
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions src/main/cn/ict/zyq/bestConf/bestConf/sampler/ConfigSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,179 @@
package cn.ict.zyq.bestConf.bestConf.sampler;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TreeSet;

import cn.ict.zyq.bestConf.bestConf.BestConf;

import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.ProtectedProperties;

public abstract class ConfigSampler {

private static String PerformanceAttName = "performance";
private static int scaleDownChoice = 1;

////////////////////////////////////////////////////////////////////////////////////////////

abstract Instances sampleMultiDimContinuous(ArrayList<Attribute> atts, int sampleSetSize, boolean useMid);

public Instances getMultiDimContinuous(ArrayList<Attribute> atts, int sampleSetSize, boolean useMid, BestConf bestconf){
Instances retval = sampleMultiDimContinuous(atts, sampleSetSize, useMid), temp;
while(retval.size()<sampleSetSize){
temp = sampleMultiDimContinuous(atts, sampleSetSize, useMid);
retval.addAll(temp);
}

//make sure the set size is equal to the setting
while(retval.size()>sampleSetSize)
retval.remove(retval.size()-1);

return retval;
}

////////////////////////////////////////////////////////////////////////////////////////////

public static void setScaleDownChoice(int val){
scaleDownChoice = val;
}

public static ArrayList<Attribute> scaleDownDetour(Instances previousSet, Instance center){
switch(scaleDownChoice){
case 0:
return scaleDownMindists(previousSet,center);
default:
return scaleDownNeighbordists(previousSet,center);
}
}

// 1. find the nearest neighbor in each dimension; 2. update the sampling range
private static ArrayList<Attribute> scaleDownNeighbordists(Instances previousSet, Instance center){
ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
int attNum = center.numAttributes();

int pos = -1;
if(previousSet.attribute(PerformanceAttName)!=null)
pos = previousSet.attribute(PerformanceAttName).index();

//traverse each dimension
Enumeration<Instance> enu;
double[] minDists = new double[2];
double val;
for(int i=0;i<attNum;i++){
if(i==pos)
continue;

enu = previousSet.enumerateInstances();
minDists[0] = 1-Double.MAX_VALUE;
minDists[1] = Double.MAX_VALUE;

while(enu.hasMoreElements()){
Instance ins = enu.nextElement();
if(!ins.equals(center)){
val = ins.value(i)-center.value(i);
if(val<0)
minDists[0] = Math.max((double)((int)((ins.value(i)-center.value(i))*1000))/1000.0, minDists[0]);
else
minDists[1] = Math.min((double)((int)((ins.value(i)-center.value(i))*1000))/1000.0, minDists[1]);
}
}

//now we set the range
Properties p1 = new Properties();
double upper = center.value(i)+minDists[1], lower=center.value(i)+minDists[0];

TreeSet<Double> detourSet = new TreeSet<Double>();
detourSet.add(upper);
detourSet.add(lower);
detourSet.add(previousSet.attribute(i).getUpperNumericBound());
detourSet.add(previousSet.attribute(i).getLowerNumericBound());
switch(detourSet.size()){
case 1:
upper=lower=detourSet.first();
break;
case 2:
upper = detourSet.last();
lower = detourSet.first();
break;
case 3:
upper=lower=detourSet.higher(detourSet.first());
break;
default://case 4:
upper=detourSet.lower(detourSet.last());
lower=detourSet.higher(detourSet.first());
break;
}

p1.setProperty("range", "["+String.valueOf(lower)+","+String.valueOf(upper)+"]");
ProtectedProperties prop1 = new ProtectedProperties(p1);

localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
}

return localAtts;
}

// 1. find the nearest neighbor in each dimension; 2. update the sampling range
private static ArrayList<Attribute> scaleDownMindists(Instances previousSet, Instance center){
ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
int attNum = center.numAttributes();

int pos = previousSet.attribute(PerformanceAttName).index();

//traverse each dimension
Enumeration<Instance> enu;
double minDis;
for(int i=0;i<attNum;i++){
if(i==pos)
continue;

enu = previousSet.enumerateInstances();
minDis = Double.MAX_VALUE;

while(enu.hasMoreElements()){
Instance ins = enu.nextElement();
if(!ins.equals(center))
minDis = Math.min((double)((int)(Math.abs(ins.value(i)-center.value(i))*1000))/1000.0, minDis);
}

//now we set the range
Properties p1 = new Properties();
double upper = center.value(i)+minDis, lower=center.value(i)-minDis;

TreeSet<Double> detourSet = new TreeSet<Double>();
detourSet.add(upper);
detourSet.add(lower);
detourSet.add(previousSet.attribute(i).getUpperNumericBound());
detourSet.add(previousSet.attribute(i).getLowerNumericBound());
switch(detourSet.size()){
case 1:
upper=lower=detourSet.first();
break;
case 2:
upper = detourSet.last();
lower = detourSet.first();
break;
case 3:
upper=lower=detourSet.higher(detourSet.first());
break;
default://case 4:
upper=detourSet.lower(detourSet.last());
lower=detourSet.higher(detourSet.first());
break;
}

p1.setProperty("range", "["+String.valueOf(lower)+","+String.valueOf(upper)+"]");
ProtectedProperties prop1 = new ProtectedProperties(p1);

localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
}

return localAtts;
}
}


0 comments on commit c17aa81

Please sign in to comment.