Update javadoc.

This commit is contained in:
Mikael Capelle 2018-03-07 15:24:50 +01:00
parent bcdaf54a63
commit 1454e651dc
4 changed files with 118 additions and 41 deletions

View File

@ -4,13 +4,23 @@ import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
/**
* Base class for algorithm classes.
*
* @param <Observer> Observer type for the algorithm.
*/
public abstract class AbstractAlgorithm<Observer> { public abstract class AbstractAlgorithm<Observer> {
protected AbstractInputData data; // Input data for the algorithm
protected ArrayList<Observer> observers; protected final AbstractInputData data;
// List of observers for the algorithm
protected final ArrayList<Observer> observers;
/** /**
* @param data * Create a new algorithm with an empty list of observers.
*
* @param data Input data for the algorithm.
*/ */
protected AbstractAlgorithm(AbstractInputData data) { protected AbstractAlgorithm(AbstractInputData data) {
this.data = data; this.data = data;
@ -18,19 +28,20 @@ public abstract class AbstractAlgorithm<Observer> {
} }
/** /**
* @param data * Create a new algorithm with the given list of observers.
* @param observers *
* @param data Input data for the algorithm.
* @param observers Initial list of observers for the algorithm.
*/ */
protected AbstractAlgorithm(AbstractInputData data, ArrayList<Observer> observers) { protected AbstractAlgorithm(AbstractInputData data, ArrayList<Observer> observers) {
this.data = data; this.data = data;
this.observers = observers; this.observers = observers;
;
} }
/** /**
* Add an observer to this algorithm. * Add an observer to this algorithm.
* *
* @param observer * @param observer Observer to add to this algorithm.
*/ */
public void addObserver(Observer observer) { public void addObserver(Observer observer) {
observers.add(observer); observers.add(observer);
@ -44,16 +55,19 @@ public abstract class AbstractAlgorithm<Observer> {
} }
/** /**
* @return Instance corresponding to this algorithm. * @return Input for this algorithm.
*/ */
public AbstractInputData getInputData() { public AbstractInputData getInputData() {
return data; return data;
} }
/** /**
* Run the algorithm and update the current solution. * Run the algorithm and return the solution.
* *
* @return true if a feasible solution was found (even non-optimal). * This methods internally time the call to doRun() and update the result of the
* call with the computed solving time.
*
* @return The solution found by the algorithm (may not be a feasible solution).
*/ */
public AbstractSolution run() { public AbstractSolution run() {
Instant start = Instant.now(); Instant start = Instant.now();
@ -65,7 +79,8 @@ public abstract class AbstractAlgorithm<Observer> {
/** /**
* Abstract method that should be implemented by child class. * Abstract method that should be implemented by child class.
* *
* @return A solution, if one was found, or null. * @return The solution found, must not be null (use an infeasible or unknown
* status if necessary).
*/ */
protected abstract AbstractSolution doRun(); protected abstract AbstractSolution doRun();

View File

@ -3,20 +3,33 @@ package org.insa.algo;
import org.insa.graph.Arc; import org.insa.graph.Arc;
import org.insa.graph.Graph; import org.insa.graph.Graph;
/**
* Base class for algorithm input data classes. This class contains the basic
* data that are required by most graph algorithms, i.e. a graph, a mode (time /
* length) and a filter for the arc.
*
*/
public abstract class AbstractInputData { public abstract class AbstractInputData {
/**
* Mode for computing costs on the arc (time or length).
*
*/
public enum Mode { public enum Mode {
TIME, LENGTH TIME, LENGTH
} }
/** /**
* * Filtering inteface for arcs - This class can be used to indicate to an
* algorithm which arc can be used.
* *
*/ */
public interface ArcFilter { public interface ArcFilter {
/** /**
* @param arc * Check if the given arc can be used (is allowed).
*
* @param arc Arc to check.
* *
* @return true if the given arc is allowed. * @return true if the given arc is allowed.
*/ */
@ -25,18 +38,20 @@ public abstract class AbstractInputData {
} }
// Graph // Graph
protected Graph graph; private final Graph graph;
// Mode for the computation of the costs. // Mode for the computation of the costs.
private final AbstractInputData.Mode mode; private final Mode mode;
// Arc filter. // Arc filter.
private final AbstractInputData.ArcFilter arcFilter; private final ArcFilter arcFilter;
/** /**
* Create a new AbstractInputData instance for the given graph, mode and filter. * Create a new AbstractInputData instance for the given graph, mode and filter.
* *
* @param graph * @param graph
* @parma mode
* @param arcFilter
*/ */
protected AbstractInputData(Graph graph, Mode mode, ArcFilter arcFilter) { protected AbstractInputData(Graph graph, Mode mode, ArcFilter arcFilter) {
this.graph = graph; this.graph = graph;
@ -65,15 +80,9 @@ public abstract class AbstractInputData {
* mode (LENGHT), with no filtering on the arc. * mode (LENGHT), with no filtering on the arc.
* *
* @param graph * @param graph
* @param mode
*/ */
protected AbstractInputData(Graph graph) { protected AbstractInputData(Graph graph) {
this(graph, Mode.LENGTH, new AbstractInputData.ArcFilter() { this(graph, Mode.LENGTH);
@Override
public boolean isAllowed(Arc arc) {
return true;
}
});
} }
/** /**
@ -85,13 +94,21 @@ public abstract class AbstractInputData {
/** /**
* @return Mode of the algorithm (time or length). * @return Mode of the algorithm (time or length).
*
* @see Mode
*/ */
public Mode getMode() { public Mode getMode() {
return mode; return mode;
} }
/** /**
* Check if the given arc is allowed for the filter corresponding to this input.
*
* @param arc Arc to check.
*
* @return true if the given arc is allowed. * @return true if the given arc is allowed.
*
* @see ArcFilter
*/ */
public boolean isAllowed(Arc arc) { public boolean isAllowed(Arc arc) {
return this.arcFilter.isAllowed(arc); return this.arcFilter.isAllowed(arc);

View File

@ -2,6 +2,12 @@ package org.insa.algo;
import java.time.Duration; import java.time.Duration;
/**
* Base class for solution classes returned by the algorithm. This class
* contains the basic information that any solution should have: status of the
* solution (unknown, infeasible, etc.), solving time and the original input
* data.
*/
public abstract class AbstractSolution { public abstract class AbstractSolution {
/** /**
@ -13,13 +19,13 @@ public abstract class AbstractSolution {
}; };
// Status of the solution. // Status of the solution.
Status status; private final Status status;
// Solving time for the solution. // Solving time for the solution.
Duration solvingTime; private Duration solvingTime;
// Original input of the solution. // Original input of the solution.
AbstractInputData data; private final AbstractInputData data;
/** /**
* Create a new abstract solution with unknown status. * Create a new abstract solution with unknown status.

View File

@ -3,8 +3,23 @@ package org.insa.graph;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.EnumSet; import java.util.EnumSet;
/**
* Class containing access restrictions for roads/arcs.
*
* This class maps transport modes to their restriction and provide interface
* based on EnumSet to query restrictions.
*
* To each transport is associated at most one restriction per road (no
* restriction corresponds to {@link AccessRestriction#UNKNOWN} but a road can
* have different restrictions for different modes.
*
*/
public class AccessRestrictions { public class AccessRestrictions {
/**
* List of managed transport modes.
*
*/
public enum AccessMode { public enum AccessMode {
// Specific modes // Specific modes
@ -17,18 +32,28 @@ public class AccessRestrictions {
HEAVY_GOODS, HEAVY_GOODS,
PUBLIC_TRANSPORT; PUBLIC_TRANSPORT;
// All available modes /**
* EnumSet containing all the possible transport modes.
*/
public static final EnumSet<AccessMode> ALL = EnumSet.allOf(AccessMode.class); public static final EnumSet<AccessMode> ALL = EnumSet.allOf(AccessMode.class);
// Vehicle /**
* EnumSet containing all the vehicle transport modes.
*/
public static final EnumSet<AccessMode> VEHICLE = EnumSet.range(AccessMode.BICYCLE, public static final EnumSet<AccessMode> VEHICLE = EnumSet.range(AccessMode.BICYCLE,
AccessMode.PUBLIC_TRANSPORT); AccessMode.PUBLIC_TRANSPORT);
// Motor vehicle /**
* EnumSet containing all the motorized vehicle transport modes.
*/
public static final EnumSet<AccessMode> MOTOR_VEHICLE = EnumSet public static final EnumSet<AccessMode> MOTOR_VEHICLE = EnumSet
.range(AccessMode.SMALL_MOTORCYCLE, AccessMode.PUBLIC_TRANSPORT); .range(AccessMode.SMALL_MOTORCYCLE, AccessMode.PUBLIC_TRANSPORT);
} }
/**
* Possible restrictions for the roads/arcs.
*
*/
public enum AccessRestriction { public enum AccessRestriction {
ALLOWED, FORBIDDEN, PRIVATE, DESTINATION, DELIVERY, CUSTOMERS, FORESTRY, UNKNOWN; ALLOWED, FORBIDDEN, PRIVATE, DESTINATION, DELIVERY, CUSTOMERS, FORESTRY, UNKNOWN;
@ -44,7 +69,7 @@ public class AccessRestrictions {
private final EnumMap<AccessMode, AccessRestriction> restrictions; private final EnumMap<AccessMode, AccessRestriction> restrictions;
/** /**
* Create new access restrictions with unknown restrictions. * Create new AccessRestrictions instances with unknown restrictions.
*/ */
public AccessRestrictions() { public AccessRestrictions() {
this.restrictions = new EnumMap<>(AccessMode.class); this.restrictions = new EnumMap<>(AccessMode.class);
@ -54,16 +79,19 @@ public class AccessRestrictions {
} }
/** /**
* Create a new instance of access restrictions with the given restrictions. * Create a new AccessRestrictions instances with the given restrictions.
* *
* @param restrictions * @param restrictions Map of restrictions for this instance of
* AccessRestrictions.
*/ */
public AccessRestrictions(EnumMap<AccessMode, AccessRestriction> restrictions) { public AccessRestrictions(EnumMap<AccessMode, AccessRestriction> restrictions) {
this.restrictions = restrictions; this.restrictions = restrictions;
} }
/** /**
* @param mode * Retrieve the restriction corresponding to the given mode.
*
* @param mode Mode for which the restriction should be retrieved.
* *
* @return Restriction for the given mode. * @return Restriction for the given mode.
*/ */
@ -72,28 +100,39 @@ public class AccessRestrictions {
} }
/** /**
* @param mode * Check if the restriction associated with the given mode is one of the given
* @param restrictions * restrictions.
* *
* @return true if the given mode is allowed for any of the given restrictions. * @param mode Mode for which to check the restrictions.
* @param restrictions List of queried restrictions for the mode.
*
* @return true if the restriction of the given mode is one of the given
* restrictions.
*/ */
public boolean isAllowedForAny(AccessMode mode, EnumSet<AccessRestriction> restrictions) { public boolean isAllowedForAny(AccessMode mode, EnumSet<AccessRestriction> restrictions) {
return restrictions.contains(getRestrictionFor(mode)); return restrictions.contains(getRestrictionFor(mode));
} }
/** /**
* @param mode * Check if the restriction for the given mode corresponds to the given
* @param restriction * restriction.
* *
* @return true if the given mode is allowed for the given restriction. * @param mode Mode for which the restriction should be checked.
* @param restriction Restriction to check against.
*
* @return true if the restriction of the given mode corresponds to the given
* restriction.
*/ */
public boolean isAllowedFor(AccessMode mode, AccessRestriction restriction) { public boolean isAllowedFor(AccessMode mode, AccessRestriction restriction) {
return getRestrictionFor(mode).equals(restriction); return getRestrictionFor(mode).equals(restriction);
} }
/** /**
* @param modes * Check if the restriction associated to each given mode is one of the
* @param restrictions * restrictions. The restriction may not be the same for all modes.
*
* @param modes Modes for which restrictions should be checked.
* @param restrictions Set of wanted restrictions for the modes.
* *
* @return true if all the given modes are allowed for any of the given * @return true if all the given modes are allowed for any of the given
* restrictions. * restrictions.