/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ /* This file is part of the program */ /* GCG --- Generic Column Generation */ /* a Dantzig-Wolfe decomposition based extension */ /* of the branch-cut-and-price framework */ /* SCIP --- Solving Constraint Integer Programs */ /* */ /* Copyright (C) 2010-2022 Operations Research, RWTH Aachen University */ /* Zuse Institute Berlin (ZIB) */ /* */ /* This program is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public License */ /* as published by the Free Software Foundation; either version 3 */ /* of the License, or (at your option) any later version. */ /* */ /* This program is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* GNU Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public License */ /* along with this program; if not, write to the Free Software */ /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/ /* */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /**@file graph_def.h * @brief miscellaneous graph methods for structure detection * @author Martin Bergner */ /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/ #define SCIP_Debug #ifndef GCG_GRAPH_DEF_H_ #define GCG_GRAPH_DEF_H_ #include "scip/scip.h" #include "graph.h" namespace gcg { template Graph::Graph( SCIP* scip /**< SCIP data structure */ ) : name("graph"),scip_(scip),graph(NULL),nconss(0),nvars(0),nnonzeroes(0),dummynodes(0) { graph = new T(); } template Graph::~Graph() { if(graph != NULL) delete graph; } template SCIP_RETCODE Graph::addNNodes(int _n_nodes) { return graph->addNNodes(_n_nodes); } template SCIP_RETCODE Graph::addNNodes(int _n_nodes, std::vector weights) { return graph->addNNodes(_n_nodes, weights); } template int Graph::getNNodes() { return graph->getNNodes(); } template int Graph::getNEdges() { return graph->getNEdges(); } template SCIP_RETCODE Graph::getEdges(std::vector& edges) { return graph->getEdges(edges); } template SCIP_RETCODE Graph::addNode(int i,int weight) { SCIP_CALL( graph->addNode(i, weight) ); return SCIP_OKAY; } template SCIP_RETCODE Graph::addNode() { SCIP_CALL( graph->addNode() ); return SCIP_OKAY; } /** adds the edge to the graph */ template SCIP_RETCODE Graph::addEdge(int i, int j) { SCIP_CALL( graph->addEdge(i, j) ); return SCIP_OKAY; } template SCIP_RETCODE Graph::flush() { SCIP_CALL( graph->flush() ); return SCIP_OKAY; } template SCIP_RETCODE Graph::normalize() { SCIP_CALL( graph->normalize() ); return SCIP_OKAY; } template int Graph::edge(int i, int j) { assert( i>= 0); assert(j >= 0); int edge_ij=0; std::vector Neighbors; Neighbors = getNeighbors(i); for(int k=0; k<(int)Neighbors.size(); k++) { if(Neighbors[k] == j) { edge_ij = 1; k = (int)Neighbors.size(); } } return edge_ij; } template int Graph::getNNeighbors(int i) { assert( i >= 0); return graph->getNNeighbors(i); } template std::vector Graph::getNeighbors(int i) { assert(i >= 0); return graph->getNeighbors(i); } template void Graph::setPartition(int i, int ID) { partition.resize(getNNodes(), -1); partition[i] = ID; } /** write the graph to a file */ template SCIP_RETCODE Graph::writeToFile( int fd, SCIP_Bool writeweights ) { int nnodes; int nedges; FILE* file; file = fdopen(fd, "wx"); if( file == NULL ) return SCIP_FILECREATEERROR; nnodes = Graph::getNNodes(); nedges = Graph::getNEdges(); SCIPinfoMessage(scip_, file, "%d %d\n", nnodes+dummynodes, nedges/2); for( int i = 0; i < nnodes; ++i ) { int nneighbors = Graph::getNNeighbors(i); std::vector neighbors = Graph::getNeighbors(i); if( writeweights ) { SCIPinfoMessage(scip_, file, "%d ", Graph::getWeight(i)); } for( int j = 0; j < nneighbors; ++j ) { SCIPinfoMessage(scip_, file, "%d ", neighbors[j]+1); } SCIPinfoMessage(scip_, file, "\n"); } for( int i = 0; i < dummynodes; ++i ) { SCIPinfoMessage(scip_, file, "\n"); } return SCIP_OKAY; } /** read in the partition from a file */ template SCIP_RETCODE Graph::readPartition( const char* filename ) { ifstream input(filename); if( !input.good() ) { SCIPerrorMessage("Could not open file <%s> for reading\n", filename); return SCIP_READERROR; } partition.resize(getNNodes(), -1); for( int i = 0; i < getNNodes(); ++i ) { int part = 0; if( !(input >> part) ) { SCIPerrorMessage("Could not read from file <%s>. It may be in the wrong format\n", filename); return SCIP_READERROR; } partition[i] = part; } input.close(); return SCIP_OKAY; } /** return the weight of given node */ template int Graph::getWeight( int i /**< the given node */ ) { return graph->graphGetWeights(i); } /** adds the weighted edge to the graph */ template SCIP_RETCODE Graph::addEdge(int i, int j, double weight) { SCIP_CALL( graph->addEdge(i, j, weight) ); return SCIP_OKAY; } /** sets the weight of the edge in the graph */ template SCIP_RETCODE Graph::setEdge(int i, int j, double weight) { SCIP_CALL( graph->setEdge(i, j, weight) ); return SCIP_OKAY; } /** returns the weight of the edge in the graph */ template double Graph::getEdgeWeight(int i, int j) { return graph->getEdgeWeight(i, j); } template std::vector > Graph::getNeighborWeights(int i) { return graph->getNeighborWeights(i); } template double Graph::getEdgeWeightPercentile(double q) { return graph->getEdgeWeightPercentile(q); } #ifdef WITH_GSL template void Graph::expand(int factor) { graph->expand(factor); } template void Graph::inflate(double factor) { graph->inflate(factor); } template void Graph::colL1Norm() { graph->colL1Norm(); } template void Graph::prune() { graph->prune(); } template bool Graph::stopMCL(int iter) { return graph->stopMCL(iter); } template std::vector Graph::getClustersMCL() { return graph->getClustersMCL(); } template void Graph::initMCL() { graph->initMCL(); } template void Graph::clearMCL() { graph->clearMCL(); } #endif } /* namespace gcg */ #endif