Skip to content

Commit a070eca

Browse files
ChristophBodensteinChristophBodenstein
authored andcommitted
improved simulation-search in cache
1 parent 268def2 commit a070eca

File tree

3 files changed

+55
-53
lines changed

3 files changed

+55
-53
lines changed

TimeNETOptimizationEnvironment/src/toe/datamodel/SimulationType.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package toe.datamodel;
99

10+
import java.math.BigInteger;
1011
import toe.support;
1112
import java.util.ArrayList;
1213
import toe.typedef.typeOfLogLevel;
@@ -27,7 +28,7 @@ public class SimulationType {
2728
private String xmlFileName = "";
2829
private boolean isFromCache = false;//is true, if from cache and false if logfile is parsed
2930
private boolean isFromDistributedSimulation = false;//Is False, if local simulated, true if simulated via Web
30-
private String hashString="";//Hash String to identify this Simulation based on the parameterset
31+
private BigInteger hashValue = BigInteger.ONE;//Hash Value to identify this Simulation based on the parameterset
3132

3233
/**
3334
* the default constructor for parser-objects
@@ -343,15 +344,15 @@ public void setIsFromDistributedSimulation(boolean isFromDistributedSimulation)
343344
/**
344345
* @return the hashString
345346
*/
346-
public String getHashString() {
347-
return hashString;
347+
public BigInteger getHashValue() {
348+
return hashValue;
348349
}
349350

350351
/**
351352
* Update the HashString of current parameterlist
352353
*/
353354
public void updateHashString() {
354-
this.hashString = support.getHashStringForParameterList(parameterList);
355+
this.hashValue = support.getHashStringForParameterList(parameterList);
355356
//support.log(this.hashString+" = HashString", typeOfLogLevel.VERBOSE);
356357
}
357358
}

TimeNETOptimizationEnvironment/src/toe/simulation/SimulationCache.java

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import java.io.BufferedReader;
1818
import java.io.File;
1919
import java.io.FileReader;
20-
import java.io.IOException;
20+
import java.math.BigInteger;
2121
import java.util.ArrayList;
2222
import java.util.Collections;
2323
import java.util.Comparator;
@@ -37,7 +37,10 @@ public class SimulationCache {
3737
//ArrayList<MeasureType> MeasureList;
3838
private ArrayList<SimulationType> simulationList;
3939
private int localSimulationCounter = 0;
40-
private String tmpHash = "";
40+
//Variables for search in simulationCache
41+
private BigInteger tmpHash = BigInteger.ZERO;
42+
private SimulationType tmpSimulation;
43+
private ArrayList<MeasureType> myTmpList = new ArrayList();
4144

4245
public SimulationCache() {
4346
//this.MeasureList = new ArrayList<MeasureType>();
@@ -299,21 +302,17 @@ public boolean checkIfAllParameterMatchTable(parameterTableModel myTableModel) {
299302
* It will just compare the hash values of the sorted parameterLists!
300303
*
301304
* @param parameterList given Set of Parameters to by virtually simulated
302-
* @return ArrayList of MeasureTypes
305+
* @return ArrayList of MeasureTypes, null if not found
303306
*/
304307
public ArrayList<MeasureType> getAllMeasuresWithParameterList(ArrayList<parameter> parameterList) {
305-
SimulationType tmpSimulation;
306-
ArrayList<MeasureType> myTmpList = new ArrayList();
308+
myTmpList = null;
307309
tmpHash = support.getHashStringForParameterList(parameterList);
308-
//support.log(tmpHash + " = Needle.", typeOfLogLevel.VERBOSE);
309310

310-
//support.log("Size of All Measures from File: "+MeasureList.size());
311-
//Go through all Measures, find the one with the same parameterlist
311+
//Go through all Simulations, find the one with the same parameterlist
312312
for (int i = 0; i < this.getSimulationList().size(); i++) {
313313
tmpSimulation = this.getSimulationList().get(i);
314-
//if (compareParameterList(parameterList, tmpSimulation.getListOfParameters())) {
315-
if (tmpHash.equals(tmpSimulation.getHashString())) {
316-
myTmpList = tmpSimulation.getMeasures(); //assuming cache-file did not have experiments with same Parameterset
314+
if (tmpHash.equals(tmpSimulation.getHashValue())) {
315+
myTmpList = tmpSimulation.getMeasures();
317316
}
318317
}
319318
return myTmpList;
@@ -352,10 +351,12 @@ public int compare(Double[] a, Double[] b) {
352351
//}
353352
//indexOfZeroDistance should contain the index of the Distance >=0
354353
ArrayList<MeasureType> listOfMeasureWithGivenParameters = this.getAllMeasuresWithParameterList(this.getSimulationList().get(distArrayList.get(indexOfZeroDistance)[1].intValue()).getListOfParameters());
355-
if (listOfMeasureWithGivenParameters.size() > 0) {
354+
if (listOfMeasureWithGivenParameters != null) {
355+
if (listOfMeasureWithGivenParameters.size() > 0) {
356356

357-
this.setLocalSimulationCounter(this.getLocalSimulationCounter() + 1);
358-
return (this.getParserFromListOfMeasures(listOfMeasureWithGivenParameters));
357+
this.setLocalSimulationCounter(this.getLocalSimulationCounter() + 1);
358+
return (this.getSimulationFromListOfMeasures(listOfMeasureWithGivenParameters));
359+
}
359360
}
360361
return null;
361362
}
@@ -483,7 +484,9 @@ private parameter findParameterInListByName(String name, ArrayList<parameter> li
483484
*/
484485
public ArrayList<SimulationType> getListOfCompletedSimulationParsers(ArrayList<ArrayList<parameter>> parameterSetList, int simulationCounter) {
485486
setLocalSimulationCounter(simulationCounter);
486-
ArrayList<SimulationType> myParserList = new ArrayList<SimulationType>();
487+
ArrayList<SimulationType> myParserList = new ArrayList<>();
488+
ArrayList<MeasureType> listOfMeasureWithGivenParameters;
489+
ArrayList<parameter> parameterSet;
487490

488491
support.log("Updating all simulation hash values.", typeOfLogLevel.INFO);
489492
for (int i = 0; i < this.getSimulationList().size(); i++) {
@@ -497,30 +500,28 @@ public ArrayList<SimulationType> getListOfCompletedSimulationParsers(ArrayList<A
497500

498501
//for (ArrayList<parameter> parameterSet : parameterSetList) {
499502
for (int i = 0; i < parameterSetList.size(); i++) {
500-
ArrayList<parameter> parameterSet = parameterSetList.get(i);
503+
parameterSet = parameterSetList.get(i);
501504

502-
//Create Arraylist from array of parameters
503-
/*ArrayList<parameter> tmpParameterList = new ArrayList<parameter>();
504-
for (parameter myParameter : parameterSet) {
505-
tmpParameterList.add(myParameter);
506-
}*/
507505
support.spinInLabel();
508506

509507
support.setStatusText("Searching in cache: " + i * 100 / parameterSetList.size() + " %");
510508
//Get local simulation results
511-
ArrayList<MeasureType> listOfMeasureWithGivenParameters = this.getAllMeasuresWithParameterList(parameterSet);
512-
support.log("Size of ParameterList: " + parameterSet.size() + " results in " + listOfMeasureWithGivenParameters.size() + " Measurements.", typeOfLogLevel.VERBOSE);
509+
listOfMeasureWithGivenParameters = this.getAllMeasuresWithParameterList(parameterSet);
510+
513511
//append if listSize is > 0
514-
if (listOfMeasureWithGivenParameters.size() > 0) {
515-
/*SimulationType tmpParser=new SimulationType();
516-
tmpParser.setMeasures(listOfMeasureWithGivenParameters);
517-
tmpParser.setSimulationTime(listOfMeasureWithGivenParameters.get(i).getSimulationTime());
518-
tmpParser.setCPUTime(support.getInt(listOfMeasureWithGivenParameters.get(i).getCPUTime()));
519-
*/
520-
SimulationType tmpParser = this.getParserFromListOfMeasures(listOfMeasureWithGivenParameters);
521-
tmpParser.setListOfParameters(parameterSet);
522-
myParserList.add(tmpParser);
523-
simulationCounter++;
512+
if (listOfMeasureWithGivenParameters != null) {
513+
if (listOfMeasureWithGivenParameters.size() > 0) {
514+
/*
515+
SimulationType tmpParser=new SimulationType();
516+
tmpParser.setMeasures(listOfMeasureWithGivenParameters);
517+
tmpParser.setSimulationTime(listOfMeasureWithGivenParameters.get(i).getSimulationTime());
518+
tmpParser.setCPUTime(support.getInt(listOfMeasureWithGivenParameters.get(i).getCPUTime()));
519+
*/
520+
SimulationType tmpParser = this.getSimulationFromListOfMeasures(listOfMeasureWithGivenParameters);
521+
tmpParser.setListOfParameters(parameterSet);
522+
myParserList.add(tmpParser);
523+
simulationCounter++;
524+
}
524525
}
525526
if (support.isCancelEverything()) {
526527
return myParserList;
@@ -539,16 +540,16 @@ public ArrayList<SimulationType> getListOfCompletedSimulationParsers(ArrayList<A
539540
* SimulationType
540541
* @return one SimulationType-object
541542
*/
542-
private SimulationType getParserFromListOfMeasures(ArrayList<MeasureType> mList) {
543+
private SimulationType getSimulationFromListOfMeasures(ArrayList<MeasureType> mList) {
543544
if (mList.size() > 0) {
544-
SimulationType tmpParser = new SimulationType();
545+
SimulationType tmpSimulation = new SimulationType();
545546
for (int i = 0; i < mList.size(); i++) {
546-
tmpParser.setMeasures(mList);
547-
//tmpParser.setSimulationTime(mList.get(i).getSimulationTime());
548-
//tmpParser.setCPUTime(support.getInt(mList.get(i).getCPUTime()));
547+
tmpSimulation.setMeasures(mList);
548+
//tmpSimulation.setSimulationTime(mList.get(i).getSimulationTime());
549+
//tmpSimulation.setCPUTime(support.getInt(mList.get(i).getCPUTime()));
549550
}
550-
tmpParser.setIsFromCache(true);
551-
return tmpParser;
551+
tmpSimulation.setIsFromCache(true);
552+
return tmpSimulation;
552553
}
553554
return null;
554555
}

TimeNETOptimizationEnvironment/src/toe/support.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ public class support {
183183

184184
private static ArrayList<MeasureType> Measures;
185185

186+
private static String tmpHashString = "";
187+
186188
/**
187189
* @return the myOptimizerPreferences a Reference to the Preferences-Frame
188190
*/
@@ -1981,8 +1983,8 @@ public static String getDefaultPathToR() {
19811983
* @return hopefully unique string to be used as primary key
19821984
* @see https://dzone.com/articles/get-md5-hash-few-lines-java
19831985
*/
1984-
public static String getHashStringForParameterList(ArrayList<parameter> parameterList) {
1985-
String returnValue = "";
1986+
public static BigInteger getHashStringForParameterList(ArrayList<parameter> parameterList) {
1987+
tmpHashString = "";
19861988
try {
19871989

19881990
Collections.sort(parameterList, new Comparator<parameter>() {
@@ -1993,18 +1995,16 @@ public int compare(parameter o1, parameter o2) {
19931995
});
19941996

19951997
for (int i = 0; i < parameterList.size(); i++) {
1996-
returnValue += parameterList.get(i).getStringValue();
1998+
tmpHashString += parameterList.get(i).getStringValue();
19971999
}
19982000

1999-
byte[] bytesOfMessage = returnValue.getBytes("UTF-8");
2000-
20012001
MessageDigest md = MessageDigest.getInstance("MD5");
2002-
md.update(returnValue.getBytes(), 0, returnValue.length());
2003-
return (new BigInteger(1, md.digest()).toString(16));
2002+
md.update(tmpHashString.getBytes(), 0, tmpHashString.length());
2003+
return (new BigInteger(1, md.digest()));
20042004

20052005
} catch (Exception ex) {
2006-
support.log("Could not create MD5-hash for " + returnValue, typeOfLogLevel.ERROR);
2006+
support.log("Could not create MD5-hash for " + tmpHashString, typeOfLogLevel.ERROR);
20072007
}
2008-
return returnValue;
2008+
return (BigInteger.ZERO);
20092009
}
20102010
}

0 commit comments

Comments
 (0)