From 02a09e93b70329d3da47139541e4515a31eed052 Mon Sep 17 00:00:00 2001 From: liujianxun-ict Date: Mon, 8 Oct 2018 17:36:44 +0800 Subject: [PATCH] Update DDSSampler.java --- .../bestConf/bestConf/sampler/DDSSampler.java | 273 ++++++++++++++++++ 1 file changed, 273 insertions(+) diff --git a/src/main/cn/ict/zyq/bestConf/bestConf/sampler/DDSSampler.java b/src/main/cn/ict/zyq/bestConf/bestConf/sampler/DDSSampler.java index 908376e..80efd6e 100644 --- a/src/main/cn/ict/zyq/bestConf/bestConf/sampler/DDSSampler.java +++ b/src/main/cn/ict/zyq/bestConf/bestConf/sampler/DDSSampler.java @@ -17,10 +17,283 @@ */ package cn.ict.zyq.bestConf.bestConf.sampler; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Properties; +import java.util.Random; + +import weka.core.Attribute; +import weka.core.DenseInstance; +import weka.core.Instances; +import weka.core.ProtectedProperties; + public class DDSSampler extends ConfigSampler{ + private static Random uniRand = new Random(System.nanoTime()); + + private int rounds = 1; + public DDSSampler(int totalRounds){ rounds = totalRounds; } + private static boolean inAlready(ArrayList[][] sets, ArrayList[] permNew){ + boolean noSame = true, notEqual = false; + for(int i=0;i[][] sets, long[] dists, int pos1, int pos2){ + ArrayList[] tempSet = sets[pos1]; + sets[pos1] = sets[pos2]; + sets[pos2] = tempSet; + + long tempVal = dists[pos1]; + dists[pos1] = dists[pos2]; + dists[pos2] = tempVal; + } + + long dists[] = null; + ArrayList[][] sets = null; + private int sampleSetToGet = 0; + public void setCurrentRound(int crntRound){ + if(sets!=null && crntRound atts, int sampleSetSize, boolean useMid){ + + ArrayList[] crntSetPerm; + //only initialize once + if(sets==null){ + //possible number of sample sets will not exceed $sampleSetSize to the power of 2 + int L = (int) Math.min(rounds, + atts.size()>2?Math.pow(sampleSetSize,atts.size()-1): + (atts.size()>1?sampleSetSize:1)); + + //initialization + dists = new long[L]; + sets = new ArrayList[L][]; + for(int i=0;i[] setPerm = generateOneSampleSet(sampleSetSize, atts.size()); + while(inAlready(sets,setPerm))//continue the samples set generation till different samples are obtained + setPerm = generateOneSampleSet(sampleSetSize, atts.size()); + sets[i] = setPerm; + + //compute the minimum distance minDist between any sample pair for each set + dists[i] = minDistForSet(setPerm); + //select the set with the maximum minDist + if(dists[i]>maxMinDist){ + posWithMaxMinDist = i; + maxMinDist = dists[i]; + } + } + //now let the first sample set be the one with the max mindist + positionSwitch(sets, dists, 0, posWithMaxMinDist); + } + crntSetPerm = sets[sampleSetToGet]; + + //generate and output the set with the maximum minDist as the result + + //first, divide the domain of each attribute into sampleSetSize equal subdomain + double[][] bounds = new double[atts.size()][sampleSetSize+1];//sampleSetSize+1 to include the lower and upper bounds + Iterator itr = atts.iterator(); + Attribute crntAttr; + boolean[] roundToInt = new boolean[atts.size()]; + for(int i=0;isampleSetSize) + roundToInt[i]=true; + } + + //second, generate the set according to setWithMaxMinDist + Instances data = new Instances("SamplesByLHS", atts, sampleSetSize); + for(int i=0;i[] generateOneSampleSet(int sampleSetSize, int attrNum){ + ArrayList[] setPerm = new ArrayList[attrNum];//sampleSetSize samples; each with atts.size() attributes + int crntRand; + //generate atts.size() permutations of sampleSetSize integers + // start from the second attribute, the first attribute always uses the natural order + for(int i=1;i(sampleSetSize); + + //randomly generate a permutation for sampleSetSize integers + for(int j=0;j(sampleSetSize); + for(int j=0;j[] setPerm){ + long mindist = Long.MAX_VALUE, dist; + int sampleSetSize = setPerm[0].size(); + int[] sampleA = new int[setPerm.length], sampleB = new int[setPerm.length]; + for(int i=0;idist?dist:mindist; + } + } + + return mindist; + } + + /** + * compute the Euclidean distance between two points in a multi-dim integer space + */ + private static long eucDistForPairs(int[] sampleA, int[] sampleB){ + long dist = 0; + for(int i=0;i atts = new ArrayList(); + + Properties p1 = new Properties(); + p1.setProperty("range", "[0,1]"); + ProtectedProperties prop1 = new ProtectedProperties(p1); + + Properties p2 = new Properties(); + p2.setProperty("range", "[321,1E9]"); + ProtectedProperties prop2 = new ProtectedProperties(p2); + + Properties p3 = new Properties(); + p3.setProperty("range", "[1,30]"); + ProtectedProperties prop3 = new ProtectedProperties(p3); + + ArrayList attVals = new ArrayList(); + for (int i = 0; i < 5; i++) + attVals.add("val" + (i+1)); + + atts.add(new Attribute("att1", prop1)); + atts.add(new Attribute("att2", prop2)); + atts.add(new Attribute("att3", prop3)); + //atts.add(new Attribute("att4", attVals)); + //Instances data = LHSInitializer.getMultiDimContinuous(atts, 10, false); + //Instances data = LHSInitializer.getMultiDim(atts, 10, false); + DDSSampler sampler = new DDSSampler(3); + + sampler.setCurrentRound(0); + Instances data = sampler.sampleMultiDimContinuous(atts, 2, false); + System.out.println(data); + + sampler.setCurrentRound(01); + data = sampler.sampleMultiDimContinuous(atts, 2, false); + System.out.println(data); + + sampler.setCurrentRound(2); + data = sampler.sampleMultiDimContinuous(atts, 2, false); + System.out.println(data); + } + } +