Commit d5d08866 authored by Daniel Lehmberg's avatar Daniel Lehmberg
Browse files

WIP

parent ad3d3b81
Pipeline #114938 failed with stages
in 85 minutes and 40 seconds
......@@ -3,6 +3,7 @@ package org.vadere.simulator.models.osm;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.models.StepSizeAdjuster;
import org.vadere.simulator.models.osm.optimization.OptimizationMetric;
import org.vadere.simulator.models.potential.combinedPotentials.CombinedPotentialStrategy;
import org.vadere.simulator.models.potential.combinedPotentials.ICombinedPotentialStrategy;
import org.vadere.simulator.models.potential.combinedPotentials.TargetAttractionStrategy;
......@@ -26,11 +27,7 @@ import org.vadere.util.geometry.shapes.IPoint;
import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.*;
public class PedestrianOSM extends Pedestrian {
......@@ -172,7 +169,7 @@ public class PedestrianOSM extends Pedestrian {
*
* @return the free flow step size
*/
private double getFreeFlowStepSize() {
public double getFreeFlowStepSize() {
/*if (attributesOSM.isDynamicStepLength()) {
double step = attributesOSM.getStepLengthIntercept()
+ attributesOSM.getStepLengthSlopeSpeed()
......@@ -357,6 +354,12 @@ public class PedestrianOSM extends Pedestrian {
return minStepLength;
}
public ArrayList<OptimizationMetric> getOptimizationMetricElements(){
var values = this.stepCircleOptimizer.getCurrentMetricValues();
this.stepCircleOptimizer.resetHashMap(); // resets
return values;
}
@Override
public PedestrianOSM clone() {
throw new RuntimeException("clone is not supported for PedestrianOSM; it seems hard to implement.");
......
......@@ -16,17 +16,13 @@ public class OptimizationMetric {
private VPoint foundPoint;
private double foundFuncValue;
public OptimizationMetric(){
public OptimizationMetric(final VPoint optimalPoint, double optimalFuncValue){
}
public OptimizationMetric(final VPoint optimalPoint, double optimalFuncValue, VPoint foundPoint,
double foundFuncValue){
this.optimalPoint = optimalPoint;
this.optimalFuncValue = optimalFuncValue;
this.foundPoint = foundPoint;
this.foundFuncValue = foundFuncValue;
this.foundPoint = null; // Can only be set afterwards optimal point
this.foundFuncValue = -1;
}
public VPoint getOptimalPoint() {
......@@ -44,4 +40,12 @@ public class OptimizationMetric {
public double getFoundFuncValue() {
return foundFuncValue;
}
public void setFoundPoint(VPoint foundPoint) {
this.foundPoint = foundPoint;
}
public void setFoundFuncValue(double foundFuncValue) {
this.foundFuncValue = foundFuncValue;
}
}
package org.vadere.simulator.models.osm.optimization;
import java.awt.Shape;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import org.vadere.simulator.models.osm.PedestrianOSM;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.data.SortedList;
import org.vadere.util.geometry.GeometryUtils;
import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.logging.Logger;
/**
* The Interface StepCircleOptimizer.
......@@ -11,22 +21,81 @@ import org.vadere.util.geometry.shapes.VPoint;
*/
public abstract class StepCircleOptimizer {
private OptimizationMetric currentMetricValues;
private ArrayList<OptimizationMetric> currentMetricValues;
protected StepCircleOptimizer(){
// TODO: read if the metric should be computed
// TODO: read if the metric should be computed from a config file, see issue #243
boolean computeMetric = true;
if(computeMetric){
this.currentMetricValues = new OptimizationMetric();
this.currentMetricValues = new ArrayList<>();
}else{
this.currentMetricValues = null;
}
}
/** Returns the reachable position with the minimal potential. */
abstract VPoint getNextPosition(PedestrianOSM pedestrian, Shape reachableArea);
public abstract VPoint getNextPosition(PedestrianOSM pedestrian, Shape reachableArea);
public abstract StepCircleOptimizer clone();
/** The following functions are to compute the "true" optimal value via brute force. This allows to check the
* quality of a optimization algorithm.
*/
private OptimizationMetric bruteForceOptimalValue(PedestrianOSM pedestrian){
var reachableArea = new VCircle( pedestrian.getFreeFlowStepSize() );
var potentialEvaluationFunction = new PotentialEvaluationFunction(pedestrian);
var bruteForceMethod = new StepCircleOptimizerDiscrete(0, new Random());
VPoint optimalPoint = bruteForceMethod.getNextPosition(pedestrian, getReachablePositions(reachableArea),
reachableArea.getRadius());
double optimalFuncValue; // -1 = invalid number
try{
optimalFuncValue = potentialEvaluationFunction.getValue(optimalPoint);
}catch (Exception e) {
Logger.getLogger(StepCircleOptimizerDiscrete.class).error("Potential evaluation threw error. Setting value " +
"to invalid (-1)");
optimalFuncValue = -1;
}
return new OptimizationMetric(optimalPoint, optimalFuncValue);
}
//abstract StepCircleOptimizer clone();
protected void setBruteForceSolution(PedestrianOSM pedestrian){
this.currentMetricValues.add(bruteForceOptimalValue(pedestrian)); // adds at the end of the list
}
protected void setFoundSolution(VPoint foundSolution, double funcValue){
// Always add at the last, currently this is not very secure bc. there is no checking if it overwrites other
// values
OptimizationMetric metric = this.currentMetricValues.get(this.currentMetricValues.size()-1);
metric.setFoundPoint(foundSolution);
metric.setFoundFuncValue(funcValue);
}
public ArrayList<OptimizationMetric> getCurrentMetricValues(){
return this.currentMetricValues;
}
public void resetHashMap(){
this.currentMetricValues = new ArrayList<>();
}
private static List<VPoint> getReachablePositions(VCircle reachableArea){
// TODO: numberPointsOfLargestCircle and numberOfCircles are parameters with a trade off between runtime and
// precision of brute force solution
return GeometryUtils.getDiscDiscretizationPoints(
null,
false,
reachableArea,
20,
10000,
0,
2 * Math.PI);
}
}
......@@ -21,7 +21,7 @@ import java.util.Random;
*/
public class StepCircleOptimizerDiscrete extends StepCircleOptimizer {
private final double movementThreshold;
private final double movementThreshold; // the next position must improve at least by this amount
private final Random random;
private final static Logger log = Logger.getLogger(StepCircleOptimizerDiscrete.class);
......@@ -33,10 +33,15 @@ public class StepCircleOptimizerDiscrete extends StepCircleOptimizer {
@Override
public VPoint getNextPosition(@NotNull final PedestrianOSM pedestrian, @NotNull final Shape reachableArea) {
assert reachableArea instanceof VCircle;
double stepSize = ((VCircle) reachableArea).getRadius();
double stepSize = ((VCircle) reachableArea).getRadius();
List<VPoint> positions = getReachablePositions(pedestrian, (VCircle) reachableArea, random);
return getNextPosition(pedestrian, positions, stepSize);
}
public VPoint getNextPosition(@NotNull final PedestrianOSM pedestrian, List<VPoint> positions, double stepSize){
PotentialEvaluationFunction potentialEvaluationFunction = new PotentialEvaluationFunction(pedestrian);
potentialEvaluationFunction.setStepSize(stepSize);
......@@ -52,7 +57,7 @@ public class StepCircleOptimizerDiscrete extends StepCircleOptimizer {
if (tmpPotential < potential
|| (Math.abs(tmpPotential - potential) <= 0.0001 && random
.nextBoolean())) {
.nextBoolean())) {
potential = tmpPotential;
nextPos = tmpPos;
}
......@@ -69,12 +74,12 @@ public class StepCircleOptimizerDiscrete extends StepCircleOptimizer {
return nextPos;
}
public StepCircleOptimizer clone() {
return new StepCircleOptimizerDiscrete(movementThreshold, random);
}
public static List<VPoint> getReachablePositions(@NotNull final PedestrianOSM pedestrian, @NotNull VCircle reachableArea, @NotNull final Random random) {
public static List<VPoint> getReachablePositions(@NotNull final PedestrianOSM pedestrian,
@NotNull VCircle reachableArea, @NotNull final Random random) {
final AttributesOSM attributesOSM = pedestrian.getAttributesOSM();
int numberOfCircles = attributesOSM.getNumberOfCircles();
......
......@@ -23,7 +23,7 @@ import java.util.LinkedList;
* Modellierungsseminar 2014/15
*
*/
public class StairStepOptimizer implements StepCircleOptimizer {
public class StairStepOptimizer extends StepCircleOptimizer {
private Stairs stairs;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment