- 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 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 graph
and 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.
- Author:
- Hannes Wellmann
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TSPLIBImporter.Metadata<V,E>
Container for the meta data of an imported TSPLIB95 file.static class
TSPLIBImporter.Node
A node imported from the NODE_COORD_SECTION of a TSPLIB95-file.static class
TSPLIBImporter.Specification
Container for the entry values read from the specification part of a file in TSPLIB95 format.
-
Constructor Summary
Constructors Constructor Description TSPLIBImporter()
Constructs a new importer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
extractValueBeforeWhitespace(String value)
TSPLIBImporter.Metadata<V,E>
getMetadata()
Returns theTSPLIBImporter.Metadata
of the latest imported file or null, if no import completed yet or the latest import failed.void
importGraph(Graph<V,E> graph, Reader in)
Import a graph using the givenReader
.List<V>
importTour(TSPLIBImporter.Metadata<V,E> referenceMetadata, Reader in)
-
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
-
-
-
-
Method Detail
-
getMetadata
public TSPLIBImporter.Metadata<V,E> getMetadata()
Returns theTSPLIBImporter.Metadata
of the latest imported file or null, if no import completed yet or the latest import failed.- Returns:
TSPLIBFileData
of the latest import
-
importGraph
public void importGraph(Graph<V,E> graph, Reader in)
Import a graph using the givenReader
.It is the callers responsibility to ensure the
Reader
is closed after this method returned.The given
Graph
must 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.Metadata
of 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:
importGraph
in interfaceGraphImporter<V,E>
- Parameters:
graph
- the graph into which this importer writes, must weighted.in
- the input reader- Throws:
IllegalArgumentException
- if the specifiedgraph
is not weighted
-
importTour
public List<V> importTour(TSPLIBImporter.Metadata<V,E> referenceMetadata, Reader in)
Imports a tour described by aList
ofvertices
using the given Reader.It is the callers responsibility to ensure the
Reader
is 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.Metadata
of the import can be obtained withgetMetadata()
after this method returns. TheMetadata#getVertexToNodeMapping() vertexToNodeMapping
in 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
- theMetadata
defining the available vertices and theirNodes
.in
- the input reader- Returns:
- the imported tour or null, if no tour was imported
-
-