Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 22b744f2 authored by HA Van Phu's avatar HA Van Phu
Browse files

change the way to penalize criterion

parent 0b108199
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,16 @@ package fr.irisa.cairn.gecos.typeexploration.dse;
import static java.util.stream.Collectors.toList;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
......@@ -29,6 +34,16 @@ public class TabuExploration extends AbstractExplorationAlgorithm {
protected boolean directionUp = true;
protected ISolution optimalSol;
Map<Symbol, List<String>> trace = new HashMap<Symbol, List<String>>();
List<String> upTrace = new ArrayList<String>();
List<String> orderTrace = new ArrayList<String>();
List<Symbol> consideredSymbols = new ArrayList<Symbol>();
Map<Symbol, List<String>> traceCost = new HashMap<Symbol, List<String>>();
Map<Symbol, List<String>> traceAcc = new HashMap<Symbol, List<String>>();
Map<Symbol, List<String>> traceCostDegr = new HashMap<Symbol, List<String>>();
Map<Symbol, List<String>> traceAccImpr = new HashMap<Symbol, List<String>>();
Map<Symbol, List<String>> traceCriteria = new HashMap<Symbol, List<String>>();
@Override
protected List<String> initializeNotesKeys() {
List<String> list = new ArrayList<String>();
......@@ -91,19 +106,85 @@ public class TabuExploration extends AbstractExplorationAlgorithm {
// throw new RuntimeException(e);
}
// explorer.setAllWToMax();
// ISolution solution_1 = explorer.createCurrentSolution();
//
// try {
// evaluateAccuracy(solution_1);
// evaluateCost(solution_1);
// display(solution_1);
// log(solution_1, false);
// } catch (AccuracyEvaluationException | CostEvaluationException e) {
// //!!! aelmouss:
// //
// // Assuming the code compiles correctly with the selected underlying types library (ac_fixed, ct-Float ..)
// // This could happen if, for instance, the solution has very low precision so it results in underflow
// // that leads to segmentation faults due to divide by zero for example.
// // So this failure could indicate that the solution is not valid!
// //
// // To recover from this, I let the exploration start and during criterion computation I consider
// // the solution to be the worst in accuracy and in cost.
//
// logger.warning("Initial solution is not valid: cause :" + e.getMessage());
//// throw new RuntimeException(e);
// }
//
//
// System.out.println("min: acc: " + getAccuracy(solution).get() + "cost: " + getCost(solution).get() );
// System.out.println("max: acc: " + getAccuracy(solution_1).get() + "cost: " + getCost(solution_1).get() );
//
// return solution;
// Initialize the Trace Table
upTrace.addAll(Arrays.asList("Up", "", directionUp ? "1" : "0"));
orderTrace.addAll(Arrays.asList("Order", "", "0"));
for(int i=0; i<analyzer.getSymbolsWithoutConstraints().size(); i++) {
Symbol s = analyzer.getSymbolsWithoutConstraints().get(i);
if(explorer.getMaxWIdx(s) != 0) {
trace.put(s, new ArrayList<String>(Arrays.asList(s.getName(), "BW")));
trace.get(s).add(Integer.toString(explorer.getCurrentW(s)));
traceAcc.put(s, new ArrayList<String>(Arrays.asList(s.getName(), "ACC")));
traceAcc.get(s).add(Double.toString(getAccuracy(explorer.getLastCreatedSolution()).get()));
traceCost.put(s, new ArrayList<String>(Arrays.asList(s.getName(), "COST")));
traceCost.get(s).add(Double.toString(getCost(explorer.getLastCreatedSolution()).get()));
traceAccImpr.put(s, new ArrayList<String>(Arrays.asList(s.getName(), "ACC Impr")));
traceAccImpr.get(s).add("0");
traceCostDegr.put(s, new ArrayList<String>(Arrays.asList(s.getName(), "COST Degr")));
traceCostDegr.get(s).add("0");
traceCriteria.put(s, new ArrayList<String>(Arrays.asList(s.getName(), "Criteria")));
traceCriteria.get(s).add("0");
consideredSymbols.add(s);
}
}
//Initial solution should be counted as a separate iteration
nbIterations.incrementAndGet();
List<Symbol> finished = new ArrayList<>();
int itr = 0;
while(finished.size() < analyzer.getSymbolsWithoutConstraints().size()) {
logger.fine("Looking for the best next solution...");
WordLengthsExplorer next = findBestNext(analyzer, explorer, finished);
itr++;
if(next == explorer)
break;
if(next == null)
break;
// Update table
upTrace.add(directionUp ? "1" : "0");
orderTrace.add(Integer.toString(itr));
for(int k=0; k<consideredSymbols.size(); k++) {
Symbol s = consideredSymbols.get(k);
if(s == next.getLastModifiedSymbol())
trace.get(consideredSymbols.get(k)).add(Integer.toString(next.getCurrentW(s)));
else
trace.get(consideredSymbols.get(k)).add("");
}
explorer = next;
solution = explorer.getLastCreatedSolution();
display(solution);
......@@ -140,6 +221,54 @@ public class TabuExploration extends AbstractExplorationAlgorithm {
logger.info("Finished exploration.");
display(optimalSol);
// Write table to CSV file
Components comp = Components.getInstance();
Path logsDir = Components.getInstance().getOutputLoactions().getLogsDir();
String str1 = String.join(";", upTrace);
String str2 = String.join(";", orderTrace);
String str3;
String str4;
String str5;
String str6;
String str7;
String str8;
String traceDir = logsDir.resolve("trace.csv").toString();
try (PrintWriter out = new PrintWriter(traceDir)) {
out.println(str1);
out.println(str2);
for(int k=0; k<consideredSymbols.size(); k++) {
Symbol s = consideredSymbols.get(k);
str3 = String.join(";", trace.get(s));
out.println(str3);
}
}
catch (Exception e) {
// TODO: handle exception
}
String traceValueDir = logsDir.resolve("traceValue.csv").toString();
try (PrintWriter out = new PrintWriter(traceValueDir)) {
out.println(str1);
out.println(str2);
for(int k=0; k<consideredSymbols.size(); k++) {
Symbol s = consideredSymbols.get(k);
str4 = String.join(";", traceAcc.get(s));
str5 = String.join(";", traceCost.get(s));
str6 = String.join(";", traceAccImpr.get(s));
str7 = String.join(";", traceCostDegr.get(s));
str8 = String.join(";", traceCriteria.get(s));
out.println(str4);
out.println(str5);
out.println(str6);
out.println(str7);
out.println(str8);
out.println("");
}
}
catch (Exception e) {
// TODO: handle exception
}
return optimalSol;
}
......@@ -272,37 +401,64 @@ public class TabuExploration extends AbstractExplorationAlgorithm {
//FIXME these values are currently arbitrary
{
//max must be obtained before setting min or range, since it is computed from min and range
// Phu 21-11-2019
final double accMax = accuracyNormalizer.getMax();
final double costMax = costNormalizer.getMax();
accuracyNormalizer.setRange(Math.max(accuracyMetric.getNormalizationRangeLB(), accuracyNormalizer.getRange()));
accuracyNormalizer.setMin(accMax-accuracyNormalizer.getRange());
costNormalizer.setRange(Math.max(costMetric.getNormalizationRangeLB(), costNormalizer.getRange()));
costNormalizer.setMin(costMax-costNormalizer.getRange());
// And of comment 21-11-2019
}
Double normalizedCurrentAccuray = getAccuracy(currentSolution).map(accuracyNormalizer::normalize).orElse(null);
Double normalizedCurrentCost = getCost(currentSolution).map(costNormalizer::normalize).orElse(null);
int nextIdx = traceAcc.get(consideredSymbols.get(0)).size();
for(int k=0; k<consideredSymbols.size(); k++) {
Symbol s = consideredSymbols.get(k);
traceAcc.get(s).add("");
traceAccImpr.get(s).add("");
traceCost.get(s).add("");
traceCostDegr.get(s).add("");
traceCriteria.get(s).add("");
}
List<Double> normalizedAccuracyValues = accuracyNormalizer.normalize(accuracyValues);
List<Double> normalizedCostValues = costNormalizer.normalize(costValues);
List<Double> deltaAccList = new ArrayList<>();
List<Double> deltaCostList = new ArrayList<>();
List<Double> criteria = new LinkedList<>();
for (int i = 0; i < nextSolutionCandidates.size(); i++) {
Integer lastModifiedWL = lastModifiedWLs.get(i);
Double normalizedAccuracyValue = normalizedAccuracyValues.get(i);
Double normalizedCostValue = normalizedCostValues.get(i);
Double ratio = (1 + computeImprovement(normalizedCurrentAccuray, normalizedAccuracyValue, accuracyMetric.isHigherBetter())) /
(1 + computeDegradation(normalizedCurrentCost, normalizedCostValue, costMetric.isHigherBetter()));
// Double ratio = (1 + computeImprovement(normalizedCurrentAccuray, normalizedAccuracyValue, accuracyMetric.isHigherBetter())) /
// (1 + computeDegradation(normalizedCurrentCost, normalizedCostValue, costMetric.isHigherBetter()));
double deltaAcc = 1 + computeImprovement(normalizedCurrentAccuray, normalizedAccuracyValue, accuracyMetric.isHigherBetter());
double deltaCost = 1 + computeDegradation(normalizedCurrentCost, normalizedCostValue, costMetric.isHigherBetter());
deltaAccList.add(deltaAcc);
deltaCostList.add(deltaCost);
Double ratio = deltaAcc/deltaCost;
//FIXME bias strength is currently not well thought out
// Phu 21-11-2019
Double WLbias = Math.pow(lastModifiedWL.doubleValue(), 1.2) / (350); //TODO try 250
// End of comment 21-11-2019
final Double criterion;
if (directionUp)
criterion = ratio - WLbias;
else
criterion = ratio + WLbias;
// Phu 21-11-2019
// if (directionUp)
// criterion = ratio - WLbias;
// else
// criterion = ratio + WLbias;
// criterion = ratio;
// End of modification 21-11-2019
criterion = ratio - WLbias;
WordLengthsExplorer candidate = nextSolutionCandidates.get(i);
logger.info(String.format("TabuSearchMetric (%s; WL:%d, Acc:%f, Cost:%f, NormAcc:%f, NormCost:%f): %f -> %f",
......@@ -316,6 +472,15 @@ public class TabuExploration extends AbstractExplorationAlgorithm {
nextSolutionCandidates.get(i).currentSolution.setNote("selected", "F");
}
for (int k=0; k<nextSolutionCandidates.size(); k++) {
WordLengthsExplorer Ws = nextSolutionCandidates.get(k);
traceAcc.get(Ws.getLastModifiedSymbol()).set(nextIdx, Double.toString(getAccuracy(Ws.getLastCreatedSolution()).get()));
traceCost.get(Ws.getLastModifiedSymbol()).set(nextIdx, Double.toString(getCost(Ws.getLastCreatedSolution()).get()));
traceAccImpr.get(Ws.getLastModifiedSymbol()).set(nextIdx, Double.toString(deltaAccList.get(k)));
traceCostDegr.get(Ws.getLastModifiedSymbol()).set(nextIdx, Double.toString(deltaCostList.get(k)));
traceCriteria.get(Ws.getLastModifiedSymbol()).set(nextIdx, Double.toString(criteria.get(k)));
}
final WordLengthsExplorer nextBest;
if(directionUp) {
nextBest = criteria.stream()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment