- java.lang.Object
-
- org.jgrapht.nio.tsplib.TSPLIBImporter<V,E>
-
- Type Parameters:
V- the graph vertex typeE- the graph edge type
- All Implemented Interfaces:
GraphImporter<V,E>
public class TSPLIBImporter<V,E> extends java.lang.Object implements GraphImporter<V,E>
Importer for files in the TSPLIB95 format.This importer reads the nodes of a Symmetric travelling salesman problem instance from a file and creates a
complete graphand provides further data from the file and about the imported graph.This implementation does not cover the full TSPLIB95 standard and only implements the subset of capabilities required at the time of creation. All keywords of The specification part in chapter 1.1 of the TSPLIB95 standard are considered. Their values can be obtained from the corresponding getters of the
TSPLIBImporter.Specification. But only the following- EDGE_WEIGHT_TYPE
values are supported for a NODE_DATA_SECTION:- EUC_2D
- EUC_3D
- MAX_2D
- MAX_3D
- MAN_2D
- MAN_3D
- CEIL2D
- GEO
- ATT
The following data sections of The data part in chapter 1.2 of the TSPLIB95 standard are supported:
- NODE_COORD_SECTION
- TOUR_SECTION
It was attempted to make the structure of this implementation generic so further keywords from the specification part or other data sections can be considered if required by broaden this class. Currently this implementation only reads Symmetric travelling salesman problems with a NODE_COORD_SECTION and on of the supported EDGE_WEIGHT_TYPE.
The website of the TSPLIB standard already contains a large library of different TSP instances provided as files in TSPLIB format. The TSPLIB library of the University of Waterlo provides more problem instances, among others a World TSP and instances based on cities of different countries.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTSPLIBImporter.Metadata<V,E>Container for the meta data of an imported TSPLIB95 file.static classTSPLIBImporter.NodeA node imported from the NODE_COORD_SECTION of a TSPLIB95-file.static classTSPLIBImporter.SpecificationContainer for the entry values read from the specification part of a file in TSPLIB95 format.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringCAPACITYprivate static java.lang.StringCOMMENTprivate static java.lang.StringDIMENSIONprivate static java.lang.StringDISPLAY_DATA_TYPEprivate static java.lang.StringEDGE_DATA_FORMATprivate static java.lang.StringEDGE_WEIGHT_FORMATprivate static java.lang.StringEDGE_WEIGHT_TYPEprivate TSPLIBImporter.Metadata<V,E>metadataprivate static java.lang.StringNAMEprivate static java.lang.StringNODE_COORD_SECTIONprivate static java.lang.StringNODE_COORD_TYPE(package private) static doublePI(package private) static doubleRRRprivate static java.lang.StringTOUR_SECTIONprivate static java.lang.StringTYPEprivate static java.util.List<java.lang.String>VALID_DISPLAY_DATA_TYPEprivate static java.util.List<java.lang.String>VALID_EDGE_DATA_FORMATSprivate static java.util.List<java.lang.String>VALID_EDGE_WEIGHT_FORMATSprivate static java.util.List<java.lang.String>VALID_EDGE_WEIGHT_TYPESprivate static java.util.List<java.lang.String>VALID_NODE_COORD_TYPESprivate static java.util.List<java.lang.String>VALID_TYPESprivate intvectorLengthprivate static java.util.regex.PatternWHITE_SPACE
-
Constructor Summary
Constructors Constructor Description TSPLIBImporter()Constructs a new importer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) intcompute2DCeilingEuclideanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)Computes the distance of the two nodes n1 and n2 according to theCEIL_2Dmetric, the round up version ofEUC_2D.(package private) intcompute2DGeographicalDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)Computes the distance of the two nodes n1 and n2 according to theGEOmetric.(package private) intcompute2DPseudoEuclideanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)Computes the distance of two the two nodes n1 and n2 according to theATTmetric.(package private) intcomputeEuclideanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)Computes the distance of the two nodes n1 and n2 according to theEUC_2DorEUC_3Dmetric depending on their dimension.(package private) intcomputeManhattanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)Computes the distance of the two nodes n1 and n2 according to theMAN_2DorMAN_3Dmetric depending on their dimension.(package private) intcomputeMaximumDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)Computes the distance of the two nodes n1 and n2 according to theMAX_2DorMAX_3Dmetric depending on their dimension.private static doublecomputeRadiansAngle(double x)java.lang.StringextractValueBeforeWhitespace(java.lang.String value)private java.util.function.ToIntBiFunction<TSPLIBImporter.Node,TSPLIBImporter.Node>getEdgeWeightFunction(java.lang.String edgeWeightType)private static ImportExceptiongetImportException(java.lang.Exception e, java.lang.String target)private static java.lang.StringgetKey(java.lang.String[] keyValue)private doublegetL1Distance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)private doublegetL2Distance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)private static java.util.Iterator<java.lang.String>getLineIterator(java.io.Reader in)private doublegetLInfDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)TSPLIBImporter.Metadata<V,E>getMetadata()Returns theTSPLIBImporter.Metadataof the latest imported file or null, if no import completed yet or the latest import failed.private java.util.List<V>getOrderedVertices(java.util.Map<V,TSPLIBImporter.Node> vertex2node)private java.lang.StringgetValue(java.lang.String[] keyValue)private java.util.List<V>getVertexTour(java.util.List<java.lang.Integer> tour, java.util.Map<V,TSPLIBImporter.Node> vertex2node)voidimportGraph(Graph<V,E> graph, java.io.Reader in)Import a graph using the givenReader.java.util.List<V>importTour(TSPLIBImporter.Metadata<V,E> referenceMetadata, java.io.Reader in)Imports a tour described by aListofverticesusing the given Reader.private java.lang.IntegerparseInteger(java.lang.String valueStr, java.lang.String valueType)private TSPLIBImporter.NodeparseNode(java.lang.String line)private TSPLIBImporter.Metadata<V,E>readContentForGraph(java.util.Iterator<java.lang.String> lines, Graph<V,E> graph)private TSPLIBImporter.Metadata<V,E>readContentForTour(java.util.Iterator<java.lang.String> lines, java.util.Map<V,TSPLIBImporter.Node> vertex2node)private static java.lang.StringreadLine(java.io.BufferedReader reader)private java.util.Map<V,TSPLIBImporter.Node>readNodeCoordinateSection(java.util.Iterator<java.lang.String> lines, TSPLIBImporter.Metadata<V,E> data)Reads all nodes of the NODE_COORD_SECTION and fills the graph of the data accordingly.private java.util.List<TSPLIBImporter.Node>readNodes(java.util.Iterator<java.lang.String> lines, int dimension)private booleanreadSpecificationSection(java.lang.String key, TSPLIBImporter.Specification spec, java.lang.String[] lineElements)private java.util.List<java.lang.Integer>readTourSection(java.util.Iterator<java.lang.String> lines, java.lang.Integer dimension)Reads a tour of the TOUR_SECTION and returns the List of ordered vertex numbers describing the tour.private voidrequireNotSet(java.lang.Object target, java.lang.String keyName)private voidrequireSet(java.lang.Object requirement, java.lang.String target)private java.lang.StringrequireValidValue(java.lang.String value, java.util.List<java.lang.String> validValues, java.lang.String valueType)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jgrapht.nio.GraphImporter
importGraph, importGraph
-
-
-
-
Field Detail
-
NAME
private static final java.lang.String NAME
- See Also:
- Constant Field Values
-
TYPE
private static final java.lang.String TYPE
- See Also:
- Constant Field Values
-
COMMENT
private static final java.lang.String COMMENT
- See Also:
- Constant Field Values
-
DIMENSION
private static final java.lang.String DIMENSION
- See Also:
- Constant Field Values
-
CAPACITY
private static final java.lang.String CAPACITY
- See Also:
- Constant Field Values
-
EDGE_WEIGHT_TYPE
private static final java.lang.String EDGE_WEIGHT_TYPE
- See Also:
- Constant Field Values
-
EDGE_WEIGHT_FORMAT
private static final java.lang.String EDGE_WEIGHT_FORMAT
- See Also:
- Constant Field Values
-
EDGE_DATA_FORMAT
private static final java.lang.String EDGE_DATA_FORMAT
- See Also:
- Constant Field Values
-
NODE_COORD_TYPE
private static final java.lang.String NODE_COORD_TYPE
- See Also:
- Constant Field Values
-
DISPLAY_DATA_TYPE
private static final java.lang.String DISPLAY_DATA_TYPE
- See Also:
- Constant Field Values
-
NODE_COORD_SECTION
private static final java.lang.String NODE_COORD_SECTION
- See Also:
- Constant Field Values
-
TOUR_SECTION
private static final java.lang.String TOUR_SECTION
- See Also:
- Constant Field Values
-
VALID_TYPES
private static final java.util.List<java.lang.String> VALID_TYPES
-
VALID_EDGE_WEIGHT_TYPES
private static final java.util.List<java.lang.String> VALID_EDGE_WEIGHT_TYPES
-
VALID_EDGE_WEIGHT_FORMATS
private static final java.util.List<java.lang.String> VALID_EDGE_WEIGHT_FORMATS
-
VALID_EDGE_DATA_FORMATS
private static final java.util.List<java.lang.String> VALID_EDGE_DATA_FORMATS
-
VALID_NODE_COORD_TYPES
private static final java.util.List<java.lang.String> VALID_NODE_COORD_TYPES
-
VALID_DISPLAY_DATA_TYPE
private static final java.util.List<java.lang.String> VALID_DISPLAY_DATA_TYPE
-
vectorLength
private int vectorLength
-
metadata
private TSPLIBImporter.Metadata<V,E> metadata
-
WHITE_SPACE
private static final java.util.regex.Pattern WHITE_SPACE
-
PI
static final double PI
- See Also:
- Constant Field Values
-
RRR
static final double RRR
- See Also:
- Constant Field Values
-
-
Method Detail
-
getMetadata
public TSPLIBImporter.Metadata<V,E> getMetadata()
Returns theTSPLIBImporter.Metadataof the latest imported file or null, if no import completed yet or the latest import failed.- Returns:
TSPLIBFileDataof the latest import
-
importGraph
public void importGraph(Graph<V,E> graph, java.io.Reader in)
Import a graph using the givenReader.It is the callers responsibility to ensure the
Readeris closed after this method returned.The given
Graphmust be weighted. Also the graph should be empty, otherwise the behavior is unspecified which could lead to exceptions.The source of the given Reader should contain a NODE_COORD_SECTION (if not the graph is not changed) and can contain a TOUR_SECTION. If a TOUR_SECTION is present a corresponding NODE_COORD_SECTION is mandatory and the read vertex numbers are referred to the NODE_COORD_SECTION in the same source.
TSPLIBImporter.Metadataof the import can be obtained withgetMetadata()after this method returns. If the readers source contains a TOUR_SECTION the imported tour can be obtained fromTSPLIBImporter.Metadata.getTour().This implementation is not thread-safe and must be synchronized externally if called by concurrent threads.
- Specified by:
importGraphin interfaceGraphImporter<V,E>- Parameters:
graph- the graph into which this importer writes, must weighted.in- the input reader- Throws:
java.lang.IllegalArgumentException- if the specifiedgraphis not weighted
-
readContentForGraph
private TSPLIBImporter.Metadata<V,E> readContentForGraph(java.util.Iterator<java.lang.String> lines, Graph<V,E> graph)
-
readNodeCoordinateSection
private java.util.Map<V,TSPLIBImporter.Node> readNodeCoordinateSection(java.util.Iterator<java.lang.String> lines, TSPLIBImporter.Metadata<V,E> data)
Reads all nodes of the NODE_COORD_SECTION and fills the graph of the data accordingly.- Returns:
- a mapping from created graph
vertexto corresponding importedTSPLIBImporter.Node
-
getEdgeWeightFunction
private java.util.function.ToIntBiFunction<TSPLIBImporter.Node,TSPLIBImporter.Node> getEdgeWeightFunction(java.lang.String edgeWeightType)
-
readNodes
private java.util.List<TSPLIBImporter.Node> readNodes(java.util.Iterator<java.lang.String> lines, int dimension)
-
parseNode
private TSPLIBImporter.Node parseNode(java.lang.String line)
-
importTour
public java.util.List<V> importTour(TSPLIBImporter.Metadata<V,E> referenceMetadata, java.io.Reader in)
Imports a tour described by aListofverticesusing the given Reader.It is the callers responsibility to ensure the
Readeris closed after this method returned.The source of the given Reader should contain a TOUR_SECTION (if not null is returned). The vertices specified by their number in the TOUR_SECTION are referred to the nodes respectively vertices in the given
metadata.The
TSPLIBImporter.Metadataof the import can be obtained withgetMetadata()after this method returns. TheMetadata#getVertexToNodeMapping() vertexToNodeMappingin the metadata of this import is the same as in the givenmetadata.This implementation is not thread-safe and must be synchronized externally if called by concurrent threads.
- Parameters:
referenceMetadata- theMetadatadefining the available vertices and theirNodes.in- the input reader- Returns:
- the imported tour or null, if no tour was imported
-
readContentForTour
private TSPLIBImporter.Metadata<V,E> readContentForTour(java.util.Iterator<java.lang.String> lines, java.util.Map<V,TSPLIBImporter.Node> vertex2node)
-
readTourSection
private java.util.List<java.lang.Integer> readTourSection(java.util.Iterator<java.lang.String> lines, java.lang.Integer dimension)Reads a tour of the TOUR_SECTION and returns the List of ordered vertex numbers describing the tour.- Returns:
- the list of vertex number describing the tour
-
getVertexTour
private java.util.List<V> getVertexTour(java.util.List<java.lang.Integer> tour, java.util.Map<V,TSPLIBImporter.Node> vertex2node)
-
getOrderedVertices
private java.util.List<V> getOrderedVertices(java.util.Map<V,TSPLIBImporter.Node> vertex2node)
-
readSpecificationSection
private boolean readSpecificationSection(java.lang.String key, TSPLIBImporter.Specification spec, java.lang.String[] lineElements)
-
requireValidValue
private java.lang.String requireValidValue(java.lang.String value, java.util.List<java.lang.String> validValues, java.lang.String valueType)
-
parseInteger
private java.lang.Integer parseInteger(java.lang.String valueStr, java.lang.String valueType)
-
extractValueBeforeWhitespace
public java.lang.String extractValueBeforeWhitespace(java.lang.String value)
-
getLineIterator
private static java.util.Iterator<java.lang.String> getLineIterator(java.io.Reader in)
-
readLine
private static java.lang.String readLine(java.io.BufferedReader reader)
-
getKey
private static java.lang.String getKey(java.lang.String[] keyValue)
-
getValue
private java.lang.String getValue(java.lang.String[] keyValue)
-
requireNotSet
private void requireNotSet(java.lang.Object target, java.lang.String keyName)
-
requireSet
private void requireSet(java.lang.Object requirement, java.lang.String target)
-
getImportException
private static ImportException getImportException(java.lang.Exception e, java.lang.String target)
-
computeEuclideanDistance
int computeEuclideanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
Computes the distance of the two nodes n1 and n2 according to theEUC_2DorEUC_3Dmetric depending on their dimension. The used metric is also known as L2-norm.- Parameters:
n1- aNodewith two or three dimensional coordinatesn2- aNodewith two or three dimensional coordinates- Returns:
- the
EUC_2DorEUC_3Dedge weight for nodes n1 and n2
-
computeMaximumDistance
int computeMaximumDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
Computes the distance of the two nodes n1 and n2 according to theMAX_2DorMAX_3Dmetric depending on their dimension. The used metric is also known as L∞-norm.- Parameters:
n1- aNodewith two or three dimensional coordinatesn2- aNodewith two or three dimensional coordinates- Returns:
- the
MAX_2DorMAX_3Dedge weight for nodes n1 and n2
-
computeManhattanDistance
int computeManhattanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
Computes the distance of the two nodes n1 and n2 according to theMAN_2DorMAN_3Dmetric depending on their dimension. The used metric is also known as L1-norm.- Parameters:
n1- aNodewith two or three dimensional coordinatesn2- aNodewith two or three dimensional coordinates- Returns:
- the
MAN_2DorMAN_3Dedge weight for nodes n1 and n2
-
compute2DCeilingEuclideanDistance
int compute2DCeilingEuclideanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
Computes the distance of the two nodes n1 and n2 according to theCEIL_2Dmetric, the round up version ofEUC_2D. The points must have dimension two.- Parameters:
n1- aNodewith two or three dimensional coordinatesn2- aNodewith two or three dimensional coordinates- Returns:
- the
CEIL_2Dedge weight for nodes n1 and n2 - See Also:
#computeEuclideanDistance(RealVector, RealVector)
-
compute2DGeographicalDistance
int compute2DGeographicalDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
Computes the distance of the two nodes n1 and n2 according to theGEOmetric. The used metric computes the distance between two points on a earth-like sphere, while the point coordinates describe their geographical latitude and longitude. The points must have dimension two.- Parameters:
n1- aNodewith two or three dimensional coordinatesn2- aNodewith two or three dimensional coordinates- Returns:
- the
GEOedge weight for nodes n1 and n2
-
computeRadiansAngle
private static double computeRadiansAngle(double x)
-
compute2DPseudoEuclideanDistance
int compute2DPseudoEuclideanDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
Computes the distance of two the two nodes n1 and n2 according to theATTmetric. The nodes must have two dimensional coordinates.- Parameters:
n1- aNodewith two dimensional coordinatesn2- aNodewith two dimensional coordinates- Returns:
- the
ATTedge weight for nodes n1 and n2
-
getL1Distance
private double getL1Distance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
-
getL2Distance
private double getL2Distance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
-
getLInfDistance
private double getLInfDistance(TSPLIBImporter.Node n1, TSPLIBImporter.Node n2)
-
-