Skip to content

Commit

Permalink
code optimization, handled code style warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Bodenstein authored and Christoph Bodenstein committed May 20, 2015
1 parent 70b1a24 commit b962b34
Showing 1 changed file with 34 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class OptimizerHill implements Runnable, Optimizer {

private double sizeOfNeighborhood;
private final double sizeOfNeighborhood;
private static final OptimizerPreferences myPreferences = support.getOptimizerPreferences();

private int simulationCounter = 0;
Expand All @@ -40,8 +40,8 @@ public class OptimizerHill implements Runnable, Optimizer {
String pathToTimeNet = "";
MainFrame parent = null;
JTabbedPane MeasureFormPane;
ArrayList<MeasureType> listOfMeasures = new ArrayList<MeasureType>();//Get List of all measures from MainFrame //Empty here
ArrayList<SimulationType> historyOfParsers = new ArrayList<SimulationType>();//History of all simulation runs
ArrayList<MeasureType> listOfMeasures = new ArrayList<>();//Get List of all measures from MainFrame //Empty here
ArrayList<SimulationType> historyOfParsers = new ArrayList<>();//History of all simulation runs
ArrayList<parameter> parameterBase;//Base set of parameters, start/end-value, stepping, etc.
SimulationCache mySimulationCache = new SimulationCache();
boolean optimized = false;//False until Optimization is ended
Expand Down Expand Up @@ -72,6 +72,7 @@ public OptimizerHill() {
* support-class and starts optimization
*
*/
@Override
public void initOptimizer() {
this.pathToTimeNet = support.getPathToTimeNet();// pathToTimeNetTMP;
this.MeasureFormPane = support.getMeasureFormPane();//MeasureFormPaneTMP;
Expand All @@ -92,6 +93,7 @@ public void initOptimizer() {
/**
* Main Routine for Thread. The Optimization runs here
*/
@Override
public void run() {
ArrayList<parameter> lastParameterset;
ArrayList<ArrayList<parameter>> newParameterset;
Expand All @@ -116,7 +118,7 @@ public void run() {
support.spinInLabel();

newParameterset = getNextParametersetAsArrayList(lastParameterset);
listOfCompletedSimulations = null;
//listOfCompletedSimulations = null;
//If result is already in cache, then count up corresponding counter
//Else start simulation

Expand All @@ -128,8 +130,8 @@ public void run() {
listOfCompletedSimulations = support.shrinkArrayListToFirstMember(listOfCompletedSimulations);

//set all results to "cached", for statistics
for (int i = 0; i < listOfCompletedSimulations.size(); i++) {
listOfCompletedSimulations.get(i).setIsFromCache(true);
for (SimulationType listOfCompletedSimulation : listOfCompletedSimulations) {
listOfCompletedSimulation.setIsFromCache(true);
}

//If last parameterset is double, then count up eject-counter for LastInCache
Expand All @@ -150,8 +152,8 @@ public void run() {
listOfCompletedSimulations = support.shrinkArrayListToFirstMember(listOfCompletedSimulations);

//Fit all resulting Simulation-Parameterlists
for (int i1 = 0; i1 < listOfCompletedSimulations.size(); i1++) {
listOfCompletedSimulations.get(i1).setListOfParameters(listOfCompletedSimulations.get(i1).getListOfParametersFittedToBaseParameterset());
for (SimulationType listOfCompletedSimulation : listOfCompletedSimulations) {
listOfCompletedSimulation.setListOfParameters(listOfCompletedSimulation.getListOfParametersFittedToBaseParameterset());
}

//Add all Results to Cache
Expand Down Expand Up @@ -251,8 +253,7 @@ private ArrayList<parameter> getFirstParameterset() {
support.log("Taking Min-Values as Start for every Parameter.");
//Calculate first parameterset, set every parameter to start-value
//For this choosing strategy, the first element must be minimum
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
p.setValue(p.getStartValue());
}
Expand All @@ -261,8 +262,7 @@ private ArrayList<parameter> getFirstParameterset() {
case middle:
support.log("Taking Middle-Values as Start for every Parameter.");
//Calulate first parameterset, the mean value of all parameters, with respect to stepping
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
double distance = p.getEndValue() - p.getStartValue();
distance = Math.round(0.5 * distance / p.getStepping()) * p.getStepping() + p.getStartValue();
Expand All @@ -274,8 +274,7 @@ private ArrayList<parameter> getFirstParameterset() {
support.log("Taking Max-Values as Start for every Parameter.");
//Calculate first parameterset, set every parameter to end-value
//For this choosing strategy, the first element must be minimum
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
p.setValue(p.getEndValue());
}
Expand All @@ -284,8 +283,7 @@ private ArrayList<parameter> getFirstParameterset() {
case random:
support.log("Taking Random-Values as Start for every Parameter.");
//Calulate first parameterset, the random value of all parameters, with respect to stepping
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
double distance = p.getEndValue() - p.getStartValue();
double rnd = Math.random();
Expand All @@ -297,8 +295,7 @@ private ArrayList<parameter> getFirstParameterset() {
case preset:
//Nothing to to, Value is already set to the preferred start-Value
//But let`s make sure, set it to middle if something is wrong
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
if ((p.getValue() < p.getStartValue()) || (p.getValue() > p.getEndValue())) {
double distance = p.getEndValue() - p.getStartValue();
Expand Down Expand Up @@ -354,7 +351,7 @@ protected ArrayList<parameter> getNextParameterset(ArrayList<parameter> actualPa
support.log("Number of Parameters in List: " + lastParameterList.size());

//For every Parameter check if it is iteratable and if it was changed last time
int i = 0;
int i;
numberOfLastParameter = -1;//Number of last parameter that was changed(in an Array of changable parameters)
for (i = 0; i < lastParameterList.size(); i++) {
if (lastParameterList.get(i).isIteratableAndIntern()) {
Expand Down Expand Up @@ -438,9 +435,8 @@ protected ArrayList<parameter> getNextParameterset(ArrayList<parameter> actualPa
incResult = support.getParameterByName(newParameterset, nameOfParameterToBeChanged).incDecValue(this.directionOfOptimization);
break;

case StepForwardBackRandom://Step back and forward randomly based on stepping
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
case StepForwardBackRandom: //Step back and forward randomly based on stepping
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
double nextValue = 0.0;
if (Math.random() >= 0.5) {
Expand All @@ -452,13 +448,11 @@ protected ArrayList<parameter> getNextParameterset(ArrayList<parameter> actualPa
}
}
break;
case RandomStepInNeighborhood://Calculate neighborhood and choose next value randomly
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
case RandomStepInNeighborhood: //Calculate neighborhood and choose next value randomly
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
double nextValue = 0.0;
double stepCount = (p.getEndValue() - p.getStartValue()) / p.getStepping();
nextValue = p.getStepping() * Math.round(Math.random() * stepCount * this.sizeOfNeighborhood / 100);
double nextValue = p.getStepping() * Math.round(Math.random() * stepCount * this.sizeOfNeighborhood / 100);
if (Math.random() >= 0.5) {
nextValue = Math.min(p.getValue() + nextValue, p.getEndValue());
} else {
Expand All @@ -469,25 +463,21 @@ protected ArrayList<parameter> getNextParameterset(ArrayList<parameter> actualPa
}
incResult = true;
break;
case RandomStepInDesignspace://Choose Value randomly out of complete designspace
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
case RandomStepInDesignspace: //Choose Value randomly out of complete designspace
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
double nextValue = 0.0;
double stepCount = (p.getEndValue() - p.getStartValue()) / p.getStepping();
nextValue = p.getStartValue() + Math.round(Math.random() * stepCount);
double nextValue = p.getStartValue() + Math.round(Math.random() * stepCount);
p.setValue(nextValue);
}
}
incResult = true;
break;
case RandomSteplessInNeighborhood: //Calculate neighborhood and choose next value randomly, Ignore Stepping!
for (int i = 0; i < newParameterset.size(); i++) {
parameter p = newParameterset.get(i);
for (parameter p : newParameterset) {
if (p.isIteratableAndIntern()) {
double nextValue = 0.0;
double range = (p.getEndValue() - p.getStartValue());
nextValue = Math.round(Math.random() * range * this.sizeOfNeighborhood / 100);
double nextValue = Math.round(Math.random() * range * this.sizeOfNeighborhood / 100);
if (Math.random() >= 0.5) {
nextValue = Math.min(p.getValue() + nextValue, p.getEndValue());
} else {
Expand Down Expand Up @@ -532,7 +522,7 @@ protected ArrayList<parameter> getNextParameterset(ArrayList<parameter> actualPa
* @return ArrayList of Parametersets
*/
private ArrayList< ArrayList<parameter>> getNextParametersetAsArrayList(ArrayList<parameter> actualParameterset) {
ArrayList< ArrayList<parameter>> myParametersetList = new ArrayList< ArrayList<parameter>>();
ArrayList< ArrayList<parameter>> myParametersetList = new ArrayList<>();
myParametersetList.add(getNextParameterset(actualParameterset));
return myParametersetList;
}
Expand All @@ -541,7 +531,7 @@ private ArrayList< ArrayList<parameter>> getNextParametersetAsArrayList(ArrayLis
* Wrapper, returns ArrayList of ArrayList of Parameters
*/
private ArrayList< ArrayList<parameter>> getParametersetAsArrayList(ArrayList<parameter> actualParameterset) {
ArrayList< ArrayList<parameter>> myParametersetList = new ArrayList< ArrayList<parameter>>();
ArrayList< ArrayList<parameter>> myParametersetList = new ArrayList<>();
myParametersetList.add(actualParameterset);
return myParametersetList;
}
Expand All @@ -556,9 +546,9 @@ private ArrayList< ArrayList<parameter>> getParametersetAsArrayList(ArrayList<pa
*/
protected double getActualDistance(SimulationType p) {
double distance = 0;
for (int measureCount = 0; measureCount < listOfMeasures.size(); measureCount++) {
MeasureType activeMeasure = p.getMeasureByName(listOfMeasures.get(measureCount).getMeasureName());
MeasureType activeMeasureFromInterface = listOfMeasures.get(measureCount);//Contains Optimization targets
for (MeasureType listOfMeasure : listOfMeasures) {
MeasureType activeMeasure = p.getMeasureByName(listOfMeasure.getMeasureName());
MeasureType activeMeasureFromInterface = listOfMeasure; //Contains Optimization targets
activeMeasure.setTargetValue(activeMeasureFromInterface.getTargetValue(), activeMeasureFromInterface.getTargetTypeOf());
if (activeMeasure.getTargetTypeOf().equals(typedef.typeOfTarget.value)) {
distance = activeMeasure.getDistanceFromTarget();
Expand Down Expand Up @@ -595,6 +585,7 @@ public void setSimulationCounter(int simulationCounter) {
*
* @return null if optimization not yet ended, else Optimum
*/
@Override
public SimulationType getOptimum() {
if (this.optimized) {
support.log("Its optimized, so returning best solution.");
Expand All @@ -611,6 +602,7 @@ public SimulationType getOptimum() {
*
* @param name Name (path) of logfile
*/
@Override
public void setLogFileName(String name) {
this.logFileName = name;
}
Expand All @@ -620,6 +612,7 @@ public void setLogFileName(String name) {
*
* @return name of logfile
*/
@Override
public String getLogFileName() {
return this.logFileName;
}
Expand Down

0 comments on commit b962b34

Please sign in to comment.