Skip to content

Commit

Permalink
Merge branch 'remove-gzstream' into 'v9-minor'
Browse files Browse the repository at this point in the history
remove gzstream

See merge request integer/scip!3399
  • Loading branch information
alexhoen committed May 15, 2024
2 parents 4fd58db + 0a74043 commit 20439fe
Showing 7 changed files with 88 additions and 543 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ Fixed bugs
Examples and applications
-------------------------

- TSP: remove `gzstream.h/hpp` and parse gzipped file differently

Interface changes
-----------------

3 changes: 1 addition & 2 deletions examples/TSP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -20,8 +20,7 @@ add_executable(sciptsp
src/HeurFarthestInsert.cpp
src/HeurFrats.cpp
src/ProbDataTSP.cpp
src/ReaderTSP.cpp
src/gzstream.cpp)
src/ReaderTSP.cpp)

target_link_libraries(sciptsp ${SCIP_LIBRARIES})

3 changes: 1 addition & 2 deletions examples/TSP/Makefile
Original file line number Diff line number Diff line change
@@ -80,8 +80,7 @@ MAINOBJ = cppmain.o \
EventhdlrNewSol.o \
HeurFarthestInsert.o \
HeurFrats.o \
Heur2opt.o \
gzstream.o
Heur2opt.o
MAINSRC = $(addprefix $(SRCDIR)/,$(MAINOBJ:.o=.cpp))

MAIN = $(MAINNAME).$(BASE).$(LPS)$(EXEEXTENSION)
108 changes: 79 additions & 29 deletions examples/TSP/src/ReaderTSP.cpp
Original file line number Diff line number Diff line change
@@ -30,13 +30,13 @@
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#include "objscip/objscip.h"

#include "scip/cons_linear.h"
#include "scip/pub_fileio.h"
#include <math.h>

#include "ReaderTSP.h"
@@ -50,30 +50,74 @@ using namespace std;

#define NINT(x) (floor(x+0.5))

/** get token */
string getToken(char*& str)
{
string token;

// skip spaces and ':'
while( *str != '\0' && ( isspace(*str) || *str == ':') )
++str;

// collect token
while( *str != '\0' && *str != ':' && ! isspace(*str) )
{
token += *str;
++str;
}

// skip spaces and ':'
while( *str != '\0' && ( isspace(*str) || *str == ':') )
++str;

return token;
}

/** parses the node list */
void ReaderTSP::getNodesFromFile(
tspifstream& filedata, /**< filestream containing the data to extract */
SCIP_RETCODE ReaderTSP::getNodesFromFile(
SCIP_FILE* file, /**< file containing the data to extract */
double* x_coords, /**< double array to be filled with the x-coordinates of the nodes */
double* y_coords, /**< same for y-coordinates */
GRAPH* graph /**< the graph which is to be generated by the nodes */
)
{
char str[SCIP_MAXSTRLEN];
int i = 0;
int nodenumber;
GRAPHNODE* node = &(graph->nodes[0]);

// extract every node out of the filestream
while ( i < graph->nnodes && !filedata.eof() )
while ( i < graph->nnodes && ! SCIPfeof(file) )
{
filedata >> nodenumber >> x_coords[i] >> y_coords[i];
// read next line
(void) SCIPfgets(str, SCIP_MAXSTRLEN, file);
char* s = str;

if ( ! SCIPstrToIntValue(str, &nodenumber, &s) )
{
SCIPerrorMessage("Could not read node number:\n%s\n", str);
return SCIP_INVALIDDATA;
}

if ( ! SCIPstrToRealValue(s, &x_coords[i], &s) )
{
SCIPerrorMessage("Could not read x coordinate:\n%s\n", str);
return SCIP_INVALIDDATA;
}

if ( ! SCIPstrToRealValue(s, &y_coords[i], &s) )
{
SCIPerrorMessage("Could not read x coordinate:\n%s\n", str);
return SCIP_INVALIDDATA;
}

// assign everything
node->id = i;
if( nodenumber-1 != i)
{
cout << "warning: nodenumber <" << nodenumber << "> does not match its index in node list <" << i+1
<< ">. Node will get number " << i+1 << " when naming variables and constraints!" << endl;
return SCIP_INVALIDDATA;
}
node->x = x_coords[i];
node->y = y_coords[i];
@@ -82,6 +126,8 @@ void ReaderTSP::getNodesFromFile(
i++;
}
assert( i == graph->nnodes );

return SCIP_OKAY;
} /*lint !e1762*/

/** adds a variable to both halfedges and captures it for usage in the graph */
@@ -198,37 +244,35 @@ SCIP_DECL_READERREAD(ReaderTSP::scip_read)
*result = SCIP_DIDNOTRUN;

// open the file
tspifstream filedata(filename);
if( !filedata )
SCIP_FILE* file = SCIPfopen(filename, "r");
if( !file )
return SCIP_READERROR;
filedata.clear();

// read the first lines of information
filedata >> token;
while( ! filedata.eof() )
char str[SCIP_MAXSTRLEN];
(void) SCIPfgets(str, SCIP_MAXSTRLEN, file);

// get first token
char* s = str;
token = getToken(s);

while( ! SCIPfeof(file) )
{
if( token == "NAME:" )
filedata >> name;
else if( token == "NAME" )
filedata >> token >> name;
else if( token == "TYPE:" )
filedata >> type;
if( token == "NAME" )
name = getToken(s);
else if( token == "TYPE" )
filedata >> token >> type;
else if( token == "DIMENSION:" )
{
filedata >> nnodes;
nedges = nnodes * (nnodes-1);
}
type = getToken(s);
else if( token == "DIMENSION" )
{
filedata >> token >> nnodes;
if ( ! SCIPstrToIntValue(s, &nnodes, &s) )
{
SCIPerrorMessage("Could not read number of nodes:\n%s\n", s);
return SCIP_INVALIDDATA;
}
nedges = nnodes * (nnodes-1);
}
else if( token == "EDGE_WEIGHT_TYPE:" )
filedata >> edgeweighttype;
else if( token == "EDGE_WEIGHT_TYPE" )
filedata >> token >> edgeweighttype;
edgeweighttype = getToken(s);
else if( token == "NODE_COORD_SECTION" || token == "DISPLAY_DATA_SECTION" )
{
// there should be some nodes to construct a graph
@@ -245,7 +289,7 @@ SCIP_DECL_READERREAD(ReaderTSP::scip_read)

x_coords = new double[nnodes];
y_coords = new double[nnodes];
getNodesFromFile(filedata, x_coords, y_coords, graph);
SCIP_CALL( getNodesFromFile(file, x_coords, y_coords, graph) );
}
else
{
@@ -254,7 +298,9 @@ SCIP_DECL_READERREAD(ReaderTSP::scip_read)
}
}
else if( token == "COMMENT:" || token == "COMMENT" || token == "DISPLAY_DATA_TYPE" || token == "DISPLAY_DATA_TYPE:" )
(void) getline( filedata, token );
{
// do nothing
}
else if( token == "EOF" )
break;
else if( token == "" )
@@ -264,9 +310,13 @@ SCIP_DECL_READERREAD(ReaderTSP::scip_read)
cout << "parse error in file <" << name << "> unknown keyword <" << token << ">" << endl;
return SCIP_READERROR;
}
filedata >> token;
(void) SCIPfgets(str, SCIP_MAXSTRLEN, file);
s = str;
token = getToken(s);
}// finished parsing the input

SCIPfclose(file);

// check whether the input data was valid
if( ! checkValid(graph, name, type, edgeweighttype, nnodes) )
retcode = SCIP_READERROR;
17 changes: 5 additions & 12 deletions examples/TSP/src/ReaderTSP.h
Original file line number Diff line number Diff line change
@@ -32,19 +32,12 @@
#ifndef __TSPREADER_H__
#define __TSPREADER_H__

#include <iostream>
#include <fstream>

#include <string>
#include "GomoryHuTree.h"
#include "objscip/objscip.h"
#include "scip/def.h"

#ifdef SCIP_WITH_ZLIB
#include "gzstream.h"
typedef gzstream::igzstream tspifstream;
#else
typedef std::ifstream tspifstream;
#endif
#include "GomoryHuTree.h" // for GRAPH
#include "scip/pub_fileio.h"


namespace tsp
@@ -101,8 +94,8 @@ class ReaderTSP : public scip::ObjReader
private:

/** parses the node list */
void getNodesFromFile(
tspifstream& filedata, /**< filestream containing the data to extract */
SCIP_RETCODE getNodesFromFile(
SCIP_FILE* file, /**< file containing the data to extract */
double* x_coords, /**< double array to be filled with the x-coordinates of the nodes */
double* y_coords, /**< same for y-coordinates */
GRAPH* graph /**< the graph which is to be generated by the nodes */
Loading

0 comments on commit 20439fe

Please sign in to comment.