Skip to content

Commit e7f9cf4

Browse files
ChristophBodensteinChristophBodenstein
authored andcommitted
speed up for cache handling (using hashmap for lookup)
1 parent 70933f6 commit e7f9cf4

File tree

3 files changed

+29
-62
lines changed

3 files changed

+29
-62
lines changed

TimeNETOptimizationEnvironment/src/toe/simulation/SimulationCache.java

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Collections;
2323
import java.util.Comparator;
24+
import java.util.HashMap;
2425
import javax.swing.JOptionPane;
2526
import toe.typedef.typeOfLogLevel;
2627

@@ -41,10 +42,12 @@ public class SimulationCache {
4142
private BigInteger tmpHash = BigInteger.ZERO;
4243
private SimulationType tmpSimulation;
4344
private ArrayList<MeasureType> myTmpList = new ArrayList();
45+
private HashMap<BigInteger, SimulationType> simulationHashmap;
4446

4547
public SimulationCache() {
4648
//this.MeasureList = new ArrayList<MeasureType>();
47-
this.simulationList = new ArrayList<SimulationType>();
49+
this.simulationList = new ArrayList<>();
50+
this.simulationHashmap = new HashMap<>();
4851
}
4952

5053
public boolean parseSimulationCacheFile(String filename, ArrayList<MeasureType> listOfMeasures, parameterTableModel myParameterTableModel, MainFrame myParentFrame) {
@@ -214,7 +217,8 @@ public boolean parseSimulationCacheFile(String filename, ArrayList<MeasureType>
214217
//this.MeasureList.add(tmpSimulation);
215218

216219
}
217-
getSimulationList().add(tmpSimulation);
220+
//getSimulationList().add(tmpSimulation);
221+
addSimulationToCache(tmpSimulation);
218222
}
219223

220224
//Do not reformat Parameter-Table
@@ -480,57 +484,38 @@ private parameter findParameterInListByName(String name, ArrayList<parameter> li
480484
*
481485
* @param parameterSetList List of Parameter-Arrays to be "simulated"
482486
* @param simulationCounter Counter of Simulations
487+
* @param listOfUnknownParametersets given by reference, this list will
488+
* contain all parametersets not found in cache
483489
* @return List of parsers with Simulation-Results, like in real simulation
484490
*/
485-
public ArrayList<SimulationType> getListOfCompletedSimulations(ArrayList<ArrayList<parameter>> parameterSetList, int simulationCounter) {
491+
public ArrayList<SimulationType> getListOfCompletedSimulations(ArrayList<ArrayList<parameter>> parameterSetList, int simulationCounter, ArrayList<ArrayList<parameter>> listOfUnknownParametersets) {
486492
setLocalSimulationCounter(simulationCounter);
487-
ArrayList<SimulationType> myParserList = new ArrayList<>();
488-
ArrayList<MeasureType> listOfMeasureWithGivenParameters;
493+
ArrayList<SimulationType> mySimulationList = new ArrayList<>();
489494
ArrayList<parameter> parameterSet;
490495

491-
support.log("Updating all simulation hash values.", typeOfLogLevel.INFO);
492-
for (int i = 0; i < this.getSimulationList().size(); i++) {
493-
this.getSimulationList().get(i).updateHashString();
494-
support.setStatusText("Updating Hashs: " + i * 100 / this.getSimulationList().size() + " %");
495-
if (support.isCancelEverything()) {
496-
return null;
497-
}
498-
}
499-
support.log("All hash values in cache up to date.", typeOfLogLevel.VERBOSE);
500-
501-
//for (ArrayList<parameter> parameterSet : parameterSetList) {
502496
for (int i = 0; i < parameterSetList.size(); i++) {
503497
parameterSet = parameterSetList.get(i);
504498

505499
support.spinInLabel();
506-
507500
support.setStatusText("Searching in cache: " + i * 100 / parameterSetList.size() + " %");
508-
//Get local simulation results
509-
listOfMeasureWithGivenParameters = this.getAllMeasuresWithParameterList(parameterSet);
510-
511-
//append if listSize is > 0
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-
}
501+
502+
//Get cached simulation results
503+
SimulationType foundSimulation = simulationHashmap.get(support.getHashStringForParameterList(parameterSet));
504+
if (foundSimulation != null) {
505+
mySimulationList.add(foundSimulation);
506+
simulationCounter++;
507+
} else {
508+
listOfUnknownParametersets.add(parameterSet);
525509
}
510+
526511
if (support.isCancelEverything()) {
527-
return myParserList;
512+
return mySimulationList;
528513
}
529514
}
530-
if (myParserList.isEmpty()) {
515+
if (mySimulationList.isEmpty()) {
531516
return null;
532517
}
533-
return myParserList;
518+
return mySimulationList;
534519
}
535520

536521
/**
@@ -588,7 +573,9 @@ public void addListOfSimulationsToCache(ArrayList<SimulationType> SimulationList
588573
* @param SimulationToAdd Simulation to be added
589574
*/
590575
public void addSimulationToCache(SimulationType SimulationToAdd) {
591-
this.getSimulationList().add(SimulationToAdd);
576+
SimulationToAdd.updateHashString();
577+
//this.getSimulationList().add(SimulationToAdd);
578+
simulationHashmap.put(SimulationToAdd.getHashValue(), SimulationToAdd);
592579
}
593580

594581
/**
@@ -621,6 +608,6 @@ public ArrayList<SimulationType> getSimulationList() {
621608
* @return Size of SimulationList
622609
*/
623610
public int getCacheSize() {
624-
return this.simulationList.size();
611+
return simulationList.size();
625612
}
626613
}

TimeNETOptimizationEnvironment/src/toe/simulation/SimulatorCached.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ public SimulatorCached() {
4242
*/
4343
@Override
4444
public void initSimulator(ArrayList<ArrayList<parameter>> listOfParameterSetsTMP, boolean log) {
45+
ArrayList<ArrayList<parameter>> listOfUnKnownParametersets = new ArrayList<>();
4546
if (mySimulationCache != null) {
46-
this.myListOfSimulations = mySimulationCache.getListOfCompletedSimulations(listOfParameterSetsTMP, support.getGlobalSimulationCounter());
47+
this.myListOfSimulations = mySimulationCache.getListOfCompletedSimulations(listOfParameterSetsTMP, support.getGlobalSimulationCounter(), listOfUnKnownParametersets);
4748
support.setGlobalSimulationCounter(support.getGlobalSimulationCounter() + myListOfSimulations.size());
4849
} else {
4950
support.log("No local Simulation file loaded. Simulation not possible.", typeOfLogLevel.ERROR);

TimeNETOptimizationEnvironment/src/toe/simulation/SimulatorCachedLocal.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void initSimulator(ArrayList< ArrayList<parameter>> listOfParameterSetsTM
6464
public void run() {
6565
if (mySimulationCache != null) {
6666
support.log("Will load available results from simulation cache.", typeOfLogLevel.INFO);
67-
this.myListOfCompletedSimulations = mySimulationCache.getListOfCompletedSimulations(listOfParameterSetsTMP, support.getGlobalSimulationCounter());
67+
this.myListOfCompletedSimulations = mySimulationCache.getListOfCompletedSimulations(listOfParameterSetsTMP, support.getGlobalSimulationCounter(), remainingParametersets);
6868
} else {
6969
support.log("No local Simulation file loaded. Will build my own cache from scratch.", typeOfLogLevel.INFO);
7070
this.mySimulationCache = support.getMySimulationCache();
@@ -83,27 +83,6 @@ public void run() {
8383

8484
if (this.myListOfCompletedSimulations.size() < listOfParameterSetsTMP.size()) {
8585
support.log("Some simulations were missing in cache. Will simulate them local/distributed.", typeOfLogLevel.INFO);
86-
87-
for (ArrayList<parameter> myParameterset : listOfParameterSetsTMP) {
88-
remainingParametersets.add(myParameterset);
89-
}
90-
91-
ArrayList<parameter> myParameterset;
92-
for (int i = 0; i < listOfParameterSetsTMP.size(); i++) {
93-
myParameterset = listOfParameterSetsTMP.get(i);
94-
support.spinInLabel();
95-
support.setStatusText("Build new List: " + i * 100 / listOfParameterSetsTMP.size() + " %");
96-
//for (ArrayList<parameter> myParameterset : listOfParameterSetsTMP) {
97-
for (SimulationType myListOfSimulationParser : this.myListOfCompletedSimulations) {
98-
if (this.mySimulationCache.compareParameterList(myListOfSimulationParser.getListOfParametersFittedToBaseParameterset(), myParameterset)) {
99-
remainingParametersets.remove(myParameterset);
100-
}
101-
if (support.isCancelEverything()) {
102-
return;
103-
}
104-
}
105-
}
106-
support.log("Size of Remaining ParameterList is " + remainingParametersets.size(), typeOfLogLevel.INFO);
10786
//Find simulations that are not already simulated
10887
support.log("Will simulate " + remainingParametersets.size() + " local/distributed.", typeOfLogLevel.INFO);
10988
if (support.isCancelEverything()) {

0 commit comments

Comments
 (0)