Skip to content
Snippets Groups Projects
Commit b1fb2631 authored by Nikolaus Huber's avatar Nikolaus Huber
Browse files

evaluation interface introduced

git-svn-id: https://se1.informatik.uni-wuerzburg.de/usvn/svn/code/code/DMM/trunk@11541 9e42b895-fcda-4063-8a3b-11be15eb1bbd
parent f56ef395
No related branches found
No related tags found
No related merge requests found
package edu.kit.ipd.descartes.adaptation.evaluation;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
/**
* Evaluates a tactic.
*
* @author nhuber
*
*/
public interface IEvaluator {
/**
* Evaluates the impact of a {@link WeightedTactic}.
*
* @param wt The weighted tactic to evaluate
*/
public void evaluate(WeightedTactic wt);
}
package edu.kit.ipd.descartes.adaptation.evaluation;
import edu.kit.ipd.descartes.adaptation.evaluation.weightingfunction.WeightedSumCalculator;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightingFunction;
/**
* Evaluates a given {@link WeightedTactic} using the weighted tactic's {@link WeightingFunction}.
*
* @author nhuber
*
*/
public class WeightingFunctionEvaluator implements IEvaluator {
private WeightedSumCalculator calculator = null;
public WeightingFunctionEvaluator() {
calculator = new WeightedSumCalculator();
}
/*
* (non-Javadoc)
*
* @see edu.kit.ipd.descartes.adaptation.evaluation.IEvaluator#evaluate()
*/
@Override
public void evaluate(WeightedTactic tactic) {
WeightingFunction f = tactic.getParentStrategy().getWeightingFunction();
calculator.setWeightingFunction(f);
calculator.updateWeight(tactic);
}
}
package edu.kit.ipd.descartes.adaptation.weightingfunction;
import edu.kit.ipd.descartes.mm.adaptation.Tactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
public interface IWeightingFunctionCalculator {
/**
* Calculates the new weight of the given {@link Tactic}.
*
* @param impact
* @return
*/
double calculateWeight(WeightedTactic weightedTactic);
/**
* Calculates and sets the new weight of the given {@link WeightedTactic}.
*
* @param impact
* @return
*/
void updateWeight(WeightedTactic weightedTactic);
}
package edu.kit.ipd.descartes.adaptation.weightingfunction;
import java.util.Random;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
public class RandomWeightCalculator implements IWeightingFunctionCalculator {
@Override
public double calculateWeight(WeightedTactic weightedTactic) {
Random r = new Random(System.currentTimeMillis());
return r.nextDouble();
}
@Override
public void updateWeight(WeightedTactic weightedTactic) {
weightedTactic.setCurrentWeight(calculateWeight(weightedTactic));
}
}
package edu.kit.ipd.descartes.adaptation.weightingfunction;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import edu.kit.ipd.descartes.adaptation.model.dmm.util.WeightingFunctionHelper;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightingFunction;
import edu.kit.ipd.descartes.perfdatarepo.Impact;
import edu.kit.ipd.descartes.perfdatarepo.MetricValue;
import edu.kit.ipd.descartes.perfdatarepo.Result;
public class WeightedSumCalculator implements IWeightingFunctionCalculator {
private Logger logger = Logger.getLogger(WeightedSumCalculator.class);
private WeightingFunction weightingFunction = null;
public WeightedSumCalculator(WeightingFunction weightingFunction) {
this.weightingFunction = weightingFunction;
}
@Override
public double calculateWeight(WeightedTactic weightedtactic) {
double newWeight = 0.0;
Impact impact = weightedtactic.getLastImpact();
Result before = impact.getBefore();
Result after = impact.getAfter();
EList<MetricValue> afterMetricValues = after.getMetricValues();
for (MetricValue afterMetricValue : afterMetricValues) {
// calculated delta=(after-before)
double afterValue = afterMetricValue.getValue();
double beforeValue = WeightingFunctionHelper
.getValueForMetricType(afterMetricValue.getMetricType(), before);
double delta = afterValue - beforeValue;
// get the corresponding weight
double weightOfMetric = WeightingFunctionHelper.getWeightForMetricType(weightingFunction,
afterMetricValue.getMetricType());
if (Double.isNaN(weightOfMetric))
logger.error("No weight found for metric type: " + afterMetricValue.getMetricType().getName());
else {
// add it
newWeight += weightOfMetric * delta;
logger.debug("Delta for metric type " + afterMetricValue.getMetricType().getName() + " was: " + delta
+ " and weight is " + weightOfMetric);
}
}
return newWeight;
}
@Override
public void updateWeight(WeightedTactic weightedTactic) {
double newWeight = calculateWeight(weightedTactic);
logger.debug("Setting new weight of " + weightedTactic.getName() + " to " + newWeight);
weightedTactic.setCurrentWeight(newWeight);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment