[dev] Fetch changes from public folder.

This commit is contained in:
Mikael CAPELLE
2024-11-21 10:08:08 +01:00
parent b8c86f3645
commit 443539a6ba
17 changed files with 257 additions and 299 deletions

View File

@@ -74,7 +74,10 @@ public abstract class AbstractInputData {
* {@link GraphStatistics#NO_MAXIMUM_SPEED} if none is set.
*/
public int getMaximumSpeed() {
return this.arcInspector.getMaximumSpeed();
final int inspectorMaxSpeed = this.arcInspector.getMaximumSpeed();
return inspectorMaxSpeed == GraphStatistics.NO_MAXIMUM_SPEED
? getGraph().getGraphInformation().getMaximumSpeed()
: inspectorMaxSpeed;
}
/**

View File

@@ -14,14 +14,14 @@ import org.insa.graphs.algorithm.shortestpath.ShortestPathAlgorithm;
import org.insa.graphs.algorithm.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
/**
* Factory class used to register and retrieve algorithms based on their common
* ancestor and name.
*
* Factory class used to register and retrieve algorithms based on their common ancestor
* and name.
*/
public class AlgorithmFactory {
// Map between algorithm names and class.
private final static Map<Class<? extends AbstractAlgorithm<?>>, Map<String, Class<? extends AbstractAlgorithm<?>>>> ALGORITHMS = new IdentityHashMap<>();
private final static Map<Class<? extends AbstractAlgorithm<?>>, Map<String, Class<? extends AbstractAlgorithm<?>>>> ALGORITHMS =
new IdentityHashMap<>();
static {
// Register weakly-connected components algorithm:
@@ -29,8 +29,10 @@ public class AlgorithmFactory {
WeaklyConnectedComponentsAlgorithm.class);
// Register shortest path algorithm:
registerAlgorithm(ShortestPathAlgorithm.class, "Bellman-Ford", BellmanFordAlgorithm.class);
registerAlgorithm(ShortestPathAlgorithm.class, "Dijkstra", DijkstraAlgorithm.class);
registerAlgorithm(ShortestPathAlgorithm.class, "Bellman-Ford",
BellmanFordAlgorithm.class);
registerAlgorithm(ShortestPathAlgorithm.class, "Dijkstra",
DijkstraAlgorithm.class);
registerAlgorithm(ShortestPathAlgorithm.class, "A*", AStarAlgorithm.class);
// Register your algorithms here:
@@ -39,17 +41,18 @@ public class AlgorithmFactory {
}
/**
* Register the given algorithm class with the given name as a child class of
* the given base algorithm.
*
* Register the given algorithm class with the given name as a child class of the
* given base algorithm.
*
* @param baseAlgorithm Base algorithm class that corresponds to the newly
* registered algorithm class (e.g., generic algorithm
* class for the problem).
* @param name Name for the registered algorithm class.
* @param algoClass Algorithm class to register.
* registered algorithm class (e.g., generic algorithm class for the
* problem).
* @param name Name for the registered algorithm class.
* @param algoClass Algorithm class to register.
*/
public static void registerAlgorithm(Class<? extends AbstractAlgorithm<?>> baseAlgorithm,
String name, Class<? extends AbstractAlgorithm<?>> algoClass) {
public static void registerAlgorithm(
Class<? extends AbstractAlgorithm<?>> baseAlgorithm, String name,
Class<? extends AbstractAlgorithm<?>> algoClass) {
if (!ALGORITHMS.containsKey(baseAlgorithm)) {
ALGORITHMS.put(baseAlgorithm, new LinkedHashMap<>());
}
@@ -58,18 +61,16 @@ public class AlgorithmFactory {
/**
* Create an instance of the given algorithm class using the given input data.
* Assuming algorithm correspond to a class "Algorithm", this function returns
* an object equivalent to `new Algorithm(data)`.
*
* Assuming algorithm correspond to a class "Algorithm", this function returns an
* object equivalent to `new Algorithm(data)`.
*
* @param algorithm Class of the algorithm to create.
* @param data Input data for the algorithm.
*
* @param data Input data for the algorithm.
* @return A new instance of the given algorithm class using the given data.
*
* @throws Exception if something wrong happens when constructing the object,
* i.e. the given input data does not correspond to the given
* algorithm and/or no constructor that takes a single
* parameter of type (data.getClass()) exists.
* @throws Exception if something wrong happens when constructing the object, i.e.
* the given input data does not correspond to the given algorithm and/or no
* constructor that takes a single parameter of type (data.getClass())
* exists.
*/
public static AbstractAlgorithm<?> createAlgorithm(
Class<? extends AbstractAlgorithm<?>> algorithm, AbstractInputData data)
@@ -79,11 +80,12 @@ public class AlgorithmFactory {
// Within this set, find the constructor that can be called with "data" (only).
AbstractAlgorithm<?> constructed = null;
for (Constructor<?> c: constructors) {
for (Constructor<?> c : constructors) {
Class<?>[] params = c.getParameterTypes();
if (params.length == 1 && params[0].isAssignableFrom(data.getClass())) {
c.setAccessible(true);
constructed = (AbstractAlgorithm<?>) c.newInstance(new Object[] { data });
constructed =
(AbstractAlgorithm<?>) c.newInstance(new Object[] { data });
break;
}
}
@@ -91,15 +93,12 @@ public class AlgorithmFactory {
}
/**
* Return the algorithm class corresponding to the given base algorithm class
* and name. The algorithm must have been previously registered using
* registerAlgorithm.
*
* Return the algorithm class corresponding to the given base algorithm class and
* name. The algorithm must have been previously registered using registerAlgorithm.
*
* @param baseAlgorithm Base algorithm class for the algorithm to retrieve.
* @param name Name of the algorithm to retrieve.
*
* @param name Name of the algorithm to retrieve.
* @return Class corresponding to the given name.
*
* @see #registerAlgorithm
*/
public static Class<? extends AbstractAlgorithm<?>> getAlgorithmClass(
@@ -108,14 +107,12 @@ public class AlgorithmFactory {
}
/**
* Return the list of names corresponding to the registered algorithm classes
* for the given base algorithm class.
*
* Return the list of names corresponding to the registered algorithm classes for
* the given base algorithm class.
*
* @param baseAlgorithm Base algorithm class for the algorithm class names to
* retrieve.
*
* retrieve.
* @return Names of the currently registered algorithms.
*
* @see #registerAlgorithm
*/
public static Set<String> getAlgorithmNames(

View File

@@ -6,7 +6,7 @@ import org.insa.graphs.model.GraphStatistics;
/**
* This class can be used to indicate to an algorithm which arcs can be used and the
* costs of the usable arcs..
* costs of the usable arcs.
*/
public interface ArcInspector {

View File

@@ -1,6 +1,6 @@
package org.insa.graphs.algorithm;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
@@ -12,169 +12,113 @@ import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
public class ArcInspectorFactory {
private static class NoFilterByLengthArcInspector implements ArcInspector {
@Override
public boolean isAllowed(Arc arc) {
return true;
}
@Override
public double getCost(Arc arc) {
return arc.getLength();
}
@Override
public int getMaximumSpeed() {
return GraphStatistics.NO_MAXIMUM_SPEED;
}
@Override
public Mode getMode() {
return Mode.LENGTH;
}
@Override
public String toString() {
return "Shortest path, all roads allowed";
}
};
private static class OnlyCarsByLengthArcInspector
extends NoFilterByLengthArcInspector {
@Override
public boolean isAllowed(Arc arc) {
return arc.getRoadInformation().getAccessRestrictions().isAllowedForAny(
AccessMode.MOTORCAR,
EnumSet.complementOf(EnumSet.of(AccessRestriction.FORBIDDEN,
AccessRestriction.PRIVATE)));
}
@Override
public String toString() {
return "Shortest path, only roads open for cars";
}
};
private static class OnlyCarsByTimeArcInspector
extends NoFilterByLengthArcInspector {
@Override
public double getCost(Arc arc) {
return arc.getMinimumTravelTime();
}
@Override
public Mode getMode() {
return Mode.TIME;
}
@Override
public String toString() {
return "Fastest path, all roads allowed";
}
};
private static class OnlyPedestrianByTime implements ArcInspector {
static final int maxPedestrianSpeed = 5;
@Override
public boolean isAllowed(Arc arc) {
return arc.getRoadInformation().getAccessRestrictions().isAllowedForAny(
AccessMode.FOOT,
EnumSet.complementOf(EnumSet.of(AccessRestriction.FORBIDDEN,
AccessRestriction.PRIVATE)));
}
@Override
public double getCost(Arc arc) {
return arc.getTravelTime(Math.min(maxPedestrianSpeed,
arc.getRoadInformation().getMaximumSpeed()));
}
@Override
public String toString() {
return "Fastest path for pedestrian";
}
@Override
public int getMaximumSpeed() {
return 5;
}
@Override
public Mode getMode() {
return Mode.TIME;
}
};
/**
* @return List of all arc filters in this factory.
*/
public static List<ArcInspector> getAllFilters() {
List<ArcInspector> filters = new ArrayList<>();
// Common filters:
// No filter (all arcs allowed):
filters.add(new ArcInspector() {
@Override
public boolean isAllowed(Arc arc) {
return true;
}
@Override
public double getCost(Arc arc) {
return arc.getLength();
}
@Override
public int getMaximumSpeed() {
return GraphStatistics.NO_MAXIMUM_SPEED;
}
@Override
public Mode getMode() {
return Mode.LENGTH;
}
@Override
public String toString() {
return "Shortest path, all roads allowed";
}
});
// Only road allowed for cars and length:
filters.add(new ArcInspector() {
@Override
public boolean isAllowed(Arc arc) {
return arc.getRoadInformation().getAccessRestrictions().isAllowedForAny(
AccessMode.MOTORCAR,
EnumSet.complementOf(EnumSet.of(AccessRestriction.FORBIDDEN,
AccessRestriction.PRIVATE)));
}
@Override
public double getCost(Arc arc) {
return arc.getLength();
}
@Override
public int getMaximumSpeed() {
return GraphStatistics.NO_MAXIMUM_SPEED;
}
@Override
public Mode getMode() {
return Mode.LENGTH;
}
@Override
public String toString() {
return "Shortest path, only roads open for cars";
}
});
// Only road allowed for cars and time:
filters.add(new ArcInspector() {
@Override
public boolean isAllowed(Arc arc) {
return true;
}
@Override
public double getCost(Arc arc) {
return arc.getMinimumTravelTime();
}
@Override
public int getMaximumSpeed() {
return GraphStatistics.NO_MAXIMUM_SPEED;
}
@Override
public Mode getMode() {
return Mode.TIME;
}
@Override
public String toString() {
return "Fastest path, all roads allowed";
}
});
filters.add(new ArcInspector() {
@Override
public boolean isAllowed(Arc arc) {
return arc.getRoadInformation().getAccessRestrictions().isAllowedForAny(
AccessMode.MOTORCAR,
EnumSet.complementOf(EnumSet.of(AccessRestriction.FORBIDDEN,
AccessRestriction.PRIVATE)));
}
@Override
public double getCost(Arc arc) {
return arc.getMinimumTravelTime();
}
@Override
public int getMaximumSpeed() {
return GraphStatistics.NO_MAXIMUM_SPEED;
}
@Override
public Mode getMode() {
return Mode.TIME;
}
@Override
public String toString() {
return "Fastest path, only roads open for cars";
}
});
// Non-private roads for pedestrian and bicycle:
filters.add(new ArcInspector() {
@Override
public boolean isAllowed(Arc arc) {
return arc.getRoadInformation().getAccessRestrictions().isAllowedForAny(
AccessMode.FOOT,
EnumSet.complementOf(EnumSet.of(AccessRestriction.FORBIDDEN,
AccessRestriction.PRIVATE)));
}
@Override
public double getCost(Arc arc) {
return arc.getTravelTime(Math.min(getMaximumSpeed(),
arc.getRoadInformation().getMaximumSpeed()));
}
@Override
public String toString() {
return "Fastest path for pedestrian";
}
@Override
public int getMaximumSpeed() {
return 5;
}
@Override
public Mode getMode() {
return Mode.TIME;
}
});
// Add your own filters here (do not forget to implement toString()
// to get an understandable output!):
return filters;
return Arrays.asList(new NoFilterByLengthArcInspector(),
new OnlyCarsByLengthArcInspector(), new OnlyCarsByTimeArcInspector(),
new OnlyPedestrianByTime());
}
}

View File

@@ -8,9 +8,17 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm {
@Override
protected ShortestPathSolution doRun() {
// retrieve data from the input problem (getInputData() is inherited from the
// parent class ShortestPathAlgorithm)
final ShortestPathData data = getInputData();
// variable that will contain the solution of the shortest path problem
ShortestPathSolution solution = null;
// TODO:
// TODO: implement the Dijkstra algorithm
// when the algorithm terminates, return the solution that has been found
return solution;
}

View File

@@ -155,8 +155,8 @@ public class BinaryHeapFormatter {
* Creates a multi-lines string representing a sorted view of the given binary heap.
*
* @param heap The binary heap to display.
* @param maxElement Maximum number of elements to display. or {@code -1} to display
* all the elements.
* @param max_elements Maximum number of elements to display. or {@code -1} to
* display all the elements.
* @return a string containing a sorted view the given binary heap.
*/
public static <E extends Comparable<E>> String toStringSorted(BinaryHeap<E> heap,