Scippy

GCG

Branch-and-Price & Column Generation for Everyone

graph_gcg.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* GCG --- Generic Column Generation */
5/* a Dantzig-Wolfe decomposition based extension */
6/* of the branch-cut-and-price framework */
7/* SCIP --- Solving Constraint Integer Programs */
8/* */
9/* Copyright (C) 2010-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file graph_gcg.h
30 * @brief Implementation of the graph which supports both node and edge weights.
31 * @author Igor Pesic
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35#include <algorithm>
36#include <iostream>
37#include <cmath>
38#include <set>
39#include "graph/graph_gcg.h"
40
41#ifdef WITH_GSL
42#include <gsl/gsl_errno.h>
43#endif
44
45using namespace std;
46
47namespace gcg {
48
49
51{
52 undirected = true;
53 locked = false;
54 initialized = false;
55 nodes = vector<int>(0);
56#ifdef WITH_GSL
57 adj_matrix_sparse = gsl_spmatrix_alloc(1,1);
58 working_adj_matrix = NULL;
59#endif
60}
61
62GraphGCG::GraphGCG(int _n_nodes, bool _undirected)
63{
64 undirected = _undirected;
65 locked = false;
66 initialized = true;
67 assert(_n_nodes >= 0);
68 nodes = vector<int>(_n_nodes, 0);
69#ifdef WITH_GSL
70 adj_matrix_sparse = gsl_spmatrix_alloc(_n_nodes, _n_nodes);
71 working_adj_matrix = NULL;
72#else
73 adj_matrix = vector<vector<double>>(_n_nodes, vector<double>(_n_nodes, 0.0));
74#endif
75}
76
78{
79 vector<int>().swap(this->nodes);
80#ifndef WITH_GSL
81 vector<vector<double>>().swap(adj_matrix);
82 assert((int)adj_matrix.size() == 0);
83#else
84 gsl_spmatrix_free(adj_matrix_sparse);
85 /*if(working_adj_matrix != NULL)
86 gsl_spmatrix_free(working_adj_matrix);*/
87#endif
88 for(auto edge: edges)
89 {
90 if (edge != NULL)
91 delete edge;
92 }
93 initialized = false;
94}
95
97{
98 if(locked || initialized)
99 {
100 return SCIP_ERROR;
101 }
102#ifdef WITH_GSL
103 if(adj_matrix_sparse == NULL)
104 adj_matrix_sparse = gsl_spmatrix_alloc(_n_nodes, _n_nodes);
105 // add diagonal because of MCL algorithm
106 for(auto i = 0; i < _n_nodes; i++)
107 {
108 gsl_spmatrix_set(adj_matrix_sparse, i, i, 1.0);
109 }
110#else
111 assert(adj_matrix.size() == 0);
112 adj_matrix = vector<vector<double>>(_n_nodes, vector<double>(_n_nodes, 0.0));
113
114#endif
115 nodes = vector<int>(_n_nodes, 0);
116 initialized = true;
117 return SCIP_OKAY;
118}
119
120SCIP_RETCODE GraphGCG::addNNodes(int _n_nodes, std::vector<int> weights)
121{
122 auto res = addNNodes(_n_nodes);
123 nodes = vector<int>(weights);
124 return res;
125}
126
127
129{
130 if(!initialized) return 0;
131#ifdef WITH_GSL
132 assert(nodes.size() == adj_matrix_sparse->size1);
133 assert(adj_matrix_sparse->size1 == adj_matrix_sparse->size2);
134 return (int) adj_matrix_sparse->size1;
135#else
136 assert(nodes.size() == adj_matrix.size());
137 return (int)adj_matrix.size();
138#endif
139}
140
141
142#ifdef WITH_GSL
143gsl_spmatrix* GraphGCG::getAdjMatrix()
144{
145 cout << "Return adj_matrix_sparse..." << endl;
146 return adj_matrix_sparse;
147}
148
149
150void GraphGCG::expand(int factor)
151{
152 assert(working_adj_matrix->sptype == (size_t)1);
153 const double alpha = 1.0;
154 gsl_spmatrix *tmp;
155 for(int i = 1; i < factor; i++)
156 {
157 tmp = gsl_spmatrix_alloc_nzmax(working_adj_matrix->size1, working_adj_matrix->size2, working_adj_matrix->nz, GSL_SPMATRIX_CCS);
158 gsl_spblas_dgemm(alpha, working_adj_matrix, working_adj_matrix, tmp);
159 gsl_spmatrix_free(working_adj_matrix);
160 working_adj_matrix = gsl_spmatrix_alloc_nzmax(tmp->size1, tmp->size2, tmp->nz, GSL_SPMATRIX_CCS);
161 gsl_spmatrix_memcpy(working_adj_matrix, tmp);
162 gsl_spmatrix_free(tmp);
163 }
164}
165
166
167// inflate columns and normalize them
168void GraphGCG::inflate(double factor)
169{
170 assert(working_adj_matrix->sptype == (size_t)1);
171 size_t i = 0;
172 while(i < working_adj_matrix->nz)
173 {
174 working_adj_matrix->data[i] = pow(working_adj_matrix->data[i], factor);
175 i++;
176 }
177 colL1Norm();
178}
179
180
181// normalize columns, but remove values below 1e-6
182void GraphGCG::colL1Norm()
183{
184 assert(working_adj_matrix->sptype == (size_t)1);
185 // normalize the columns to sum up to 1
186 for(int col = 0; col < (int)working_adj_matrix->size2; col++)
187 {
188 double col_sum = 0.0;
189 size_t begin_ = working_adj_matrix->p[col];
190 const size_t end_ = working_adj_matrix->p[col+1];
191 while(begin_ < end_)
192 {
193 col_sum += working_adj_matrix->data[begin_++];
194 }
195 begin_ = working_adj_matrix->p[col];
196 while(begin_ < end_)
197 {
198 working_adj_matrix->data[begin_] /= col_sum;
199 begin_++;
200 }
201 }
202
203 // We need this part to remove very very small values, otherwise GSL behaves strangely
204 double threshold = 1e-8;
205 gsl_spmatrix *tmp = gsl_spmatrix_alloc(working_adj_matrix->size1, working_adj_matrix->size2);
206
207 for (int col = 0; col < (int)working_adj_matrix->size1; col++)
208 {
209 size_t begin_ = working_adj_matrix->p[col];
210 const size_t end_ = working_adj_matrix->p[col+1];
211 while(begin_ < end_)
212 {
213 if(working_adj_matrix->data[begin_] > threshold)
214 {
215 gsl_spmatrix_set(tmp, working_adj_matrix->i[begin_], col, working_adj_matrix->data[begin_]);
216 }
217 begin_++;
218 }
219 }
220
221 gsl_spmatrix *tmp_comp = gsl_spmatrix_compcol(tmp);
222 gsl_spmatrix_free(working_adj_matrix);
223 working_adj_matrix = gsl_spmatrix_alloc_nzmax(tmp_comp->size1, tmp_comp->size2, tmp_comp->nz, GSL_SPMATRIX_CCS);
224 gsl_spmatrix_memcpy(working_adj_matrix, tmp_comp);
225 gsl_spmatrix_free(tmp);
226 gsl_spmatrix_free(tmp_comp);
227}
228
229
230// remove values below 1e-3 or 5*1e-4 or 1e-4 and then normalize
231void GraphGCG::prune()
232{
233 double threshold_start = 1e-3;
234 gsl_spmatrix *tmp = gsl_spmatrix_alloc(working_adj_matrix->size1, working_adj_matrix->size2);
235
236 double ave_col_sum = 0.0;
237 for (int col = 0; col < (int)working_adj_matrix->size1; col++)
238 {
239 size_t begin_ = working_adj_matrix->p[col];
240 const size_t end_ = working_adj_matrix->p[col+1];
241 while(begin_ < end_)
242 {
243 if(working_adj_matrix->data[begin_] > threshold_start)
244 {
245 gsl_spmatrix_set(tmp, working_adj_matrix->i[begin_], col, working_adj_matrix->data[begin_]);
246 ave_col_sum += working_adj_matrix->data[begin_];
247 }
248 begin_++;
249 }
250 }
251 ave_col_sum /= (double)(tmp->size1);
252
253 double thresholds[2] = {5*1e-4, 1e-4};
254 for(int i = 0; i < 2; i++)
255 {
256 if(ave_col_sum > 0.85)
257 {
258 break;
259 }
260
261 gsl_spmatrix_set_zero(tmp);
262 ave_col_sum = 0.0;
263 for (int col = 0; col < (int)working_adj_matrix->size1; col++)
264 {
265 size_t begin_ = working_adj_matrix->p[col];
266 const size_t end_ = working_adj_matrix->p[col+1];
267 while(begin_ < end_)
268 {
269 if(working_adj_matrix->data[begin_] > thresholds[i])
270 {
271 gsl_spmatrix_set(tmp, working_adj_matrix->i[begin_], col, working_adj_matrix->data[begin_]);
272 ave_col_sum += working_adj_matrix->data[begin_];
273 }
274 begin_++;
275 }
276 }
277 ave_col_sum /= (double)(tmp->size1);
278 }
279
280 gsl_spmatrix *tmp_comp = gsl_spmatrix_compcol(tmp);
281 gsl_spmatrix_free(working_adj_matrix);
282 working_adj_matrix = gsl_spmatrix_alloc_nzmax(tmp_comp->size1, tmp_comp->size2, tmp_comp->nz, GSL_SPMATRIX_CCS);
283 gsl_spmatrix_memcpy(working_adj_matrix, tmp_comp);
284
285 gsl_spmatrix_free(tmp);
286 gsl_spmatrix_free(tmp_comp);
287
288 colL1Norm();
289}
290
291
292// checks if A*A - A stays unchanged
293bool GraphGCG::stopMCL(int iter)
294{
295 if(iter > 6)
296 {
297 gsl_spmatrix *tmpSquare = gsl_spmatrix_alloc_nzmax(working_adj_matrix->size1, working_adj_matrix->size2, working_adj_matrix->nz, GSL_SPMATRIX_CCS);
298 gsl_spmatrix *tmpMinus = gsl_spmatrix_alloc_nzmax(working_adj_matrix->size1, working_adj_matrix->size2, working_adj_matrix->nz, GSL_SPMATRIX_CCS);
299 gsl_spmatrix *res = gsl_spmatrix_alloc_nzmax(working_adj_matrix->size1, working_adj_matrix->size2, working_adj_matrix->nz, GSL_SPMATRIX_CCS);
300 gsl_spmatrix_memcpy(tmpMinus, working_adj_matrix);
301 gsl_spmatrix_scale(tmpMinus, -1.0);
302 double alpha = 1.0;
303 gsl_spblas_dgemm(alpha, working_adj_matrix, working_adj_matrix, tmpSquare);
304 gsl_spmatrix_add(res, tmpSquare, tmpMinus);
305 double min, max;
306 gsl_set_error_handler_off();
307 int status = gsl_spmatrix_minmax(res, &min, &max);
308 if(status == GSL_EINVAL)
309 {
310 min = 0.0;
311 max = 0.0;
312 }
313
314 gsl_spmatrix_free(res);
315 gsl_spmatrix_free(tmpSquare);
316 gsl_spmatrix_free(tmpMinus);
317 if(abs(max - min) < 1e-8)
318 {
319 return true;
320 }
321 }
322 return false;
323}
324
325vector<int> GraphGCG::getClustersMCL()
326{
327 assert(working_adj_matrix->size1 == working_adj_matrix->size2);
328 vector<int> res(working_adj_matrix->size1, 0);
329 map<int, int> row_to_label;
330 set<int> nzrows;
331 vector<int> labels(working_adj_matrix->size1);
332
333 for(int i = 0; i < (int)working_adj_matrix->nz; i++)
334 {
335 nzrows.insert(working_adj_matrix->i[i]);
336 }
337 int c = 0;
338 for(auto nzrow: nzrows)
339 {
340 row_to_label[nzrow] = c;
341 c++;
342 }
343
344 map<int, vector<int> > clust_map;
345 // all data that belongs to that row belongs to its cluster
346 for (int col = 0; col < (int)working_adj_matrix->size2; col++)
347 {
348 size_t begin_ = working_adj_matrix->p[col];
349 const size_t end_ = working_adj_matrix->p[col+1];
350 while(begin_ < end_)
351 {
352 labels[col] = row_to_label[working_adj_matrix->i[begin_]];
353 clust_map[row_to_label[working_adj_matrix->i[begin_]]].push_back(col); // python version
354 begin_++;
355 }
356 }
357
358 for(auto item : clust_map)
359 {
360 for(auto val : item.second)
361 {
362 labels[val] = item.first;
363 }
364 }
365 // enumerations of labels may be broken (e.g. we can have labels 0,1,4,5), so we have to fix it to 0,1,2,3
366 set<int> existinglabels;
367 map<int, int> fixlabels;
368 for(int i = 0; i < (int)labels.size(); i++)
369 {
370 existinglabels.insert(labels[i]);
371 }
372
373 c = 0;
374 for(auto existinglabel: existinglabels)
375 {
376 fixlabels[existinglabel] = c;
377 c++;
378 }
379 for(int i = 0; i < (int)labels.size(); i++)
380 {
381 labels[i] = fixlabels[labels[i]];
382 }
383 return labels;
384}
385
386
387void GraphGCG::initMCL()
388{
389 working_adj_matrix = gsl_spmatrix_alloc_nzmax(adj_matrix_sparse->size1, adj_matrix_sparse->size2, adj_matrix_sparse->nz, GSL_SPMATRIX_CCS);
390 gsl_spmatrix_memcpy(working_adj_matrix, adj_matrix_sparse);
391}
392
393
394void GraphGCG::clearMCL()
395{
396 gsl_spmatrix_free(working_adj_matrix);
397}
398
399#else
400vector<vector<double>> GraphGCG::getAdjMatrix()
401{
402 return adj_matrix;
403}
404#endif
405
406
407// TODO: we can use edges.size() here
409{
410 int n_edges = 0;
411#ifdef WITH_GSL
412 if(initialized)
413 n_edges = (int)(adj_matrix_sparse->nz - adj_matrix_sparse->size1);
414 else
415 n_edges = 0;
416#else
417 for(int i = 0; i < (int)adj_matrix.size(); i++)
418 {
419 n_edges += getNNeighbors(i);
420 }
421#endif
422 if(undirected)
423 {
424 assert(n_edges % 2 == 0);
425 n_edges = (int) ( n_edges / 2.0);
426 }
427 assert(n_edges == (int)edges.size());
428 return n_edges;
429}
430
431SCIP_Bool GraphGCG::isEdge(int node_i, int node_j)
432{
433 assert(node_i >= 0);
434 assert(node_j >= 0);
435#ifdef WITH_GSL
436 if(gsl_spmatrix_get(adj_matrix_sparse, node_i, node_j) != 0.0)
437 {
438 return 1;
439 }
440#else
441 if(adj_matrix[node_i][node_j] != 0.0)
442 {
443 return 1;
444 }
445#endif
446 return 0;
447}
448
450{
451 assert(node >= 0);
452 int n_neighbors;
453#ifdef WITH_GSL
454 if(!initialized) return 0;
455 assert(adj_matrix_sparse->sptype == (size_t)1);
456 assert(node < (int)adj_matrix_sparse->size2);
457 const size_t begin_ = adj_matrix_sparse->p[node];
458 const size_t end_ = adj_matrix_sparse->p[node+1];
459 n_neighbors = (int)(end_ - begin_);
460 if(gsl_spmatrix_get(adj_matrix_sparse, node, node) != 0.0)
461 {
462 n_neighbors -=1 ;
463 }
464#else
465 assert(node < (int)adj_matrix.size());
466 n_neighbors = count_if(adj_matrix[node].begin(), adj_matrix[node].end(), [](double i) {return i != 0.0;});
467 // remove itself as a neighbor
468 if(adj_matrix[node][node] != 0.0)
469 {
470 n_neighbors -= 1;
471 }
472#endif
473 return n_neighbors;
474}
475
476vector<int> GraphGCG::getNeighbors(int node)
477{
478 vector<int> res;
479#ifdef WITH_GSL
480 if(!initialized || !locked) return res;
481 assert(adj_matrix_sparse->sptype == (size_t)1);
482 assert(node < (int)adj_matrix_sparse->size2);
483 size_t begin_ = adj_matrix_sparse->p[node];
484 size_t end_ = adj_matrix_sparse->p[node+1];
485 res.resize(end_ - begin_);
486 size_t curr = 0;
487 bool self_found = false;
488 while(begin_ < end_){
489 if((int)adj_matrix_sparse->i[begin_] == node) self_found = true;
490 res[curr++] = adj_matrix_sparse->i[begin_++];
491 }
492 assert(curr == res.size());
493 if(self_found)
494 res.erase(remove(res.begin(), res.end(), node), res.end());
495#else
496 for(int i = 0; i < (int)adj_matrix[node].size(); i++)
497 {
498 if(adj_matrix[node][i] != 0.0 && node != i)
499 {
500 res.push_back(i);
501 }
502 }
503#endif
504 return res;
505}
506
507vector<pair<int, double> > GraphGCG::getNeighborWeights(int node)
508{
509 vector<pair<int, double> > res;
510#ifdef WITH_GSL
511 if(!initialized || !locked) return res;
512 assert(adj_matrix_sparse->sptype == (size_t)1);
513 assert(node < (int)adj_matrix_sparse->size2);
514 size_t begin_ = adj_matrix_sparse->p[node];
515 size_t end_ = adj_matrix_sparse->p[node+1];
516 res.resize(end_ - begin_);
517 size_t curr = 0;
518 bool self_found = false;
519 int found_pos = 0;
520 while(begin_ < end_){
521 if((int)adj_matrix_sparse->i[begin_] == node)
522 {
523 self_found = true;
524 found_pos = (int) curr;
525 }
526 double value = adj_matrix_sparse->data[begin_];
527 int row = (int)adj_matrix_sparse->i[begin_];
528 res[curr++] = pair<int, double>(row, value);
529 begin_++;
530 }
531 assert(curr == res.size());
532 if(self_found)
533 {
534 res.erase(res.begin() + found_pos);
535 }
536#else
537 for(int i = 0; i < (int)adj_matrix[node].size(); i++)
538 {
539 if(adj_matrix[node][i] != 0.0 && node != i)
540 {
541 res.push_back(make_pair(i, adj_matrix[node][i]));
542 }
543 }
544#endif
545
546
547 return res;
548}
549
550/** int node is obsolete, it must be the next available id */
551SCIP_RETCODE GraphGCG::addNode(int node, int weight)
552{
553
554 if(locked)
555 {
556 return SCIP_ERROR;
557 }
558 int next_id = (int)nodes.size();
559 assert(node == next_id);
560
561#ifdef WITH_GSL
562 if(adj_matrix_sparse == NULL)
563 adj_matrix_sparse = gsl_spmatrix_alloc(1, 1);
564 // add diagonal because of MCL algorithm
565 gsl_spmatrix_set(adj_matrix_sparse, next_id, next_id, 1.0);
566
567 //return SCIP_NOTIMPL;
568#else
569
570 // add new column
571 for(size_t i = 0; i < adj_matrix.size(); i++)
572 {
573 adj_matrix[i].push_back(0.0);
574 }
575 // add new row
576 adj_matrix.push_back(vector<double>(next_id+1));
577
578 // add self loop
579 adj_matrix[next_id][next_id] = 1.0;
580 assert(adj_matrix.size() == adj_matrix[0].size());
581#endif
582 initialized = true;
583 nodes.push_back(weight);
584 return SCIP_OKAY;
585}
586
588{
589#ifdef WITH_GSL
590 return addNode((int)adj_matrix_sparse->size2, 0);
591#else
592 return addNode((int)adj_matrix.size(), 0);
593#endif
594}
595
597{
598 if(locked)
599 {
600 return SCIP_ERROR;
601 }
602 // Would be very inefficient and is not necessary
603 return SCIP_INVALIDCALL;
604}
605
606SCIP_RETCODE GraphGCG::addEdge(int node_i, int node_j)
607{
608 return addEdge(node_i, node_j, 1);
609}
610
611SCIP_RETCODE GraphGCG::addEdge(int node_i, int node_j, double weight)
612{
613 if(locked || !initialized || node_i == node_j)
614 {
615 return SCIP_ERROR;
616 }
617 bool new_edge = true;
618 assert(weight >= 0.0);
619 assert(node_i >= 0);
620 assert(node_j >= 0);
621#ifdef WITH_GSL
622 assert(node_i < (int)adj_matrix_sparse->size2);
623 assert(node_j < (int)adj_matrix_sparse->size2);
624 if (gsl_spmatrix_get(adj_matrix_sparse, node_i, node_j) != 0.0)
625 {
626 new_edge = false;
627 }
628 if(new_edge)
629 {
630 gsl_spmatrix_set(adj_matrix_sparse, node_i, node_j, weight);
631 if(undirected)
632 {
633 gsl_spmatrix_set(adj_matrix_sparse, node_j, node_i, weight);
634 }
635 }
636#else
637 assert(node_i < (int)adj_matrix.size());
638 assert(node_j < (int)adj_matrix.size());
639 (adj_matrix[node_i][node_j] == 0.0) ? new_edge = true : new_edge = false;
640 adj_matrix[node_i][node_j] = weight;
641 if(undirected)
642 {
643 adj_matrix[node_j][node_i] = weight;
644 }
645#endif
646 if(new_edge && node_i != node_j)
647 {
648 int first, second;
649 first = node_i < node_j ? node_i : node_j;
650 second = node_i == first ? node_j : node_i;
651 EdgeGCG* edge1 = new EdgeGCG(first, second, weight);
652 edges.push_back(edge1);
653 }
654
655 return SCIP_OKAY;
656}
657
658SCIP_RETCODE GraphGCG::setEdge(int node_i, int node_j, double weight)
659{
660 if(locked || !initialized)
661 {
662 return SCIP_ERROR;
663 }
664 assert(weight >= 0.0);
665 assert(node_i >= 0);
666 assert(node_j >= 0);
667#ifdef WITH_GSL
668 assert(node_i < (int)adj_matrix_sparse->size2);
669 assert(node_j < (int)adj_matrix_sparse->size2);
670 gsl_spmatrix_set(adj_matrix_sparse, node_i, node_j, weight);
671 if(undirected)
672 {
673 gsl_spmatrix_set(adj_matrix_sparse, node_j, node_i, weight);
674 }
675#else
676 assert(node_i < (int)adj_matrix.size());
677 assert(node_j < (int)adj_matrix.size());
678 adj_matrix[node_i][node_j] = weight;
679 if(undirected)
680 {
681 adj_matrix[node_j][node_i] = weight;
682 }
683#endif
684 if(node_i != node_j)
685 for(auto edge: edges)
686 {
687 if((edge->src == node_i && edge->dest == node_j) || (edge->src == node_j && edge->dest == node_i))
688 {
689 edge->weight = weight;
690 }
691 }
692 return SCIP_OKAY;
693}
694
695SCIP_RETCODE GraphGCG::deleteEdge(int node_i, int node_j)
696{
697 return setEdge(node_i, node_j, 0);
698}
699
701{
702 return nodes[node];
703}
704
705double GraphGCG::getEdgeWeight(int node_i, int node_j)
706{
707 assert(node_i >= 0);
708 assert(node_j >= 0);
709 double weight;
710#ifdef WITH_GSL
711 assert(node_i < (int)adj_matrix_sparse->size1);
712 assert(node_j < (int)adj_matrix_sparse->size1);
713 weight = gsl_spmatrix_get(adj_matrix_sparse, node_i, node_j);
714#else
715 assert(node_i < (int)adj_matrix.size());
716 assert(node_j < (int)adj_matrix.size());
717 weight = adj_matrix[node_i][node_j];
718#endif
719 return weight;
720}
721
723{
724 locked = true;
725#ifdef WITH_GSL
726 gsl_spmatrix *adj_matrix_sparse_tmp = gsl_spmatrix_compcol(adj_matrix_sparse);
727 gsl_spmatrix_free(adj_matrix_sparse);
728 adj_matrix_sparse = adj_matrix_sparse_tmp;
729#endif
730 return SCIP_OKAY;
731}
732
734{
735 double scaler = 0.0;
736#ifdef WITH_GSL
737 double min, max;
738 gsl_spmatrix_minmax(adj_matrix_sparse, &min, &max);
739 scaler = max;
740 gsl_spmatrix_scale(adj_matrix_sparse, double(1.0/scaler));
741
742#else
743 for(int i = 0; i < (int)adj_matrix.size(); i++)
744 {
745 vector<double>::iterator tmp = max_element(adj_matrix[i].begin(), adj_matrix[i].end());
746 double curr_max = *tmp;
747 if( curr_max > scaler )
748 {
749 scaler = curr_max;
750 }
751 }
752
753 for(int i = 0; i < (int)adj_matrix.size(); i++)
754 {
755 for(int j = 0; j < (int)adj_matrix[i].size(); j++)
756 {
757 adj_matrix[i][j] /= scaler;
758 assert(adj_matrix[i][j] <= 1);
759 assert(adj_matrix[i][j] >= 0);
760 }
761 }
762#endif
763 return SCIP_OKAY;
764}
765
767{
768 double res = -1;
769 vector<double> all_weights;
770#ifdef WITH_GSL
771 all_weights = vector<double>(adj_matrix_sparse->data, adj_matrix_sparse->data + adj_matrix_sparse->nz);
772#else
773 for(int i = 0; i < (int)adj_matrix.size(); i++)
774 {
775 for(int j = 0; j < (int)adj_matrix[i].size(); j++)
776 {
777 if(adj_matrix[i][j] != 0.0)
778 {
779 all_weights.push_back(adj_matrix[i][j]);
780 }
781 }
782 }
783#endif
784 sort(all_weights.begin(), all_weights.end());
785
786 int upper_pos = (int) floor((q/100.0)*all_weights.size());
787 int lower_pos = (int) ceil((q/100.0)*all_weights.size());
788 if(upper_pos != lower_pos)
789 {
790 res = (all_weights[upper_pos] + all_weights[lower_pos]) / 2.0;
791 }
792 else
793 {
794 res = all_weights[lower_pos];
795 }
796 return res;
797}
798
799
800SCIP_RETCODE GraphGCG::getEdges(vector<void *>& _edges)
801{
802 _edges.resize(edges.size());
803 for(int i = 0; i < (int)edges.size(); i++)
804 {
805 assert(edges[i]->src < (int)nodes.size());
806 assert(edges[i]->dest < (int)nodes.size());
807 assert(edges[i]->src != edges[i]->dest);
808
809 (_edges[i]) = (void *)(edges[i]);
810 }
811 return SCIP_OKAY;
812}
813
814
815// Compare two edges according to their weights.
816// Used in sort() for sorting an array of edges
817int GraphGCG::edgeComp(const EdgeGCG* a, const EdgeGCG* b)
818{
819 return (a->src < b->src) || (a->src == b->src && a->weight < b->weight);
820}
821
822
823
824
825}
SCIP_VAR * a
SCIP_VAR ** b
virtual double getEdgeWeight(int node_i, int node_j)
Definition: graph_gcg.cpp:705
virtual SCIP_RETCODE addNNodes(int _n_nodes)
Definition: graph_gcg.cpp:96
virtual int graphGetWeights(int node)
Definition: graph_gcg.cpp:700
virtual double getEdgeWeightPercentile(double q)
Definition: graph_gcg.cpp:766
virtual std::vector< std::pair< int, double > > getNeighborWeights(int node)
Definition: graph_gcg.cpp:507
virtual SCIP_RETCODE flush()
Definition: graph_gcg.cpp:722
virtual int getNNeighbors(int node)
Definition: graph_gcg.cpp:449
virtual SCIP_RETCODE getEdges(std::vector< void * > &edges)
Definition: graph_gcg.cpp:800
virtual int getNNodes()
Definition: graph_gcg.cpp:128
virtual std::vector< std::vector< double > > getAdjMatrix()
Definition: graph_gcg.cpp:400
virtual SCIP_RETCODE addNode()
Definition: graph_gcg.cpp:587
virtual SCIP_RETCODE deleteNode(int node)
Definition: graph_gcg.cpp:596
virtual std::vector< int > getNeighbors(int node)
Definition: graph_gcg.cpp:476
virtual SCIP_RETCODE addEdge(int i, int j)
Definition: graph_gcg.cpp:606
virtual SCIP_RETCODE deleteEdge(int node_i, int node_j)
Definition: graph_gcg.cpp:695
virtual int getNEdges()
Definition: graph_gcg.cpp:408
virtual int edgeComp(const EdgeGCG *a, const EdgeGCG *b)
Definition: graph_gcg.cpp:817
virtual SCIP_RETCODE setEdge(int node_i, int node_j, double weight)
Definition: graph_gcg.cpp:658
virtual SCIP_Bool isEdge(int node_i, int node_j)
Definition: graph_gcg.cpp:431
virtual SCIP_RETCODE normalize()
Definition: graph_gcg.cpp:733
virtual ~GraphGCG()
Definition: graph_gcg.cpp:77
#define SCIP_Bool
Implementation of the graph which supports both node and edge weights.
#define NULL
INLINE Rational & abs(Rational &r)
INLINE Rational & max(Rational &r1, Rational &r2)
INLINE Rational & min(Rational &r1, Rational &r2)
SCIP_OKAY
SCIP_INVALIDCALL
SCIP_ERROR
enum SCIP_Retcode SCIP_RETCODE