genmodel 0.0.30 → 0.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,6 +49,11 @@ vector<vector<size_t> > GmGraph::GetCliqueCover()
49
49
  ++(node_depth[dc[i]]);
50
50
  size_t depth = 1;
51
51
 
52
+ // Find a mximal clique containing the current edge curr_e
53
+ // The clique C is maximal in the sense that there is no clique C'
54
+ // strictly containing C (and mot in the sense that is the largest clique
55
+ // containing curr_e)
56
+
52
57
  while(next_n != -1 && depth < e*e)
53
58
  {
54
59
  //printf("1-Add %ld (depth %ld)\n", next_n, depth);
@@ -0,0 +1,400 @@
1
+ /*
2
+ * HyperGraph.cpp
3
+ *
4
+ * Created on: 2011-03-17
5
+ * Author: mbouchard
6
+ */
7
+
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include "HyperGraph.h"
11
+
12
+ void checkbound(uint i, uint ub);
13
+
14
+ int HyperGraph::SetQty(uint n, uint e)
15
+ {
16
+ hnodes.resize(n);
17
+ hnodes.assign(n, HyperGen(0.0));
18
+
19
+ hedges.resize(n);
20
+ hedges.assign(n, HyperGen(0.0));
21
+
22
+ hn = n;
23
+ he = e;
24
+
25
+ return 0;
26
+ }
27
+
28
+ int HyperGraph::SetTopologicalOrder()
29
+ {
30
+ vector<bool> nvis;
31
+ nvis.resize(hn, false);
32
+ vector<uint> nnext;
33
+
34
+ list<uint> tocheck;
35
+ for(uint i = 0; i < hn; i++)
36
+ tocheck.push_back(i);
37
+
38
+ uint nodecount = 0;
39
+
40
+ layers.push_back(vector<uint>());
41
+ list<uint>::iterator it;
42
+ for(it = tocheck.begin(); it != tocheck.end();)
43
+ {
44
+ if(hnodes[*it].dout == 0)
45
+ {
46
+ layers.back().push_back(*it);
47
+ nvis[*it] = true;
48
+ nodecount++;
49
+ it = tocheck.erase(it);
50
+ }
51
+ else
52
+ it++;
53
+ }
54
+
55
+ uint iter = 0;
56
+ is_cyclic = false;
57
+
58
+ while(!tocheck.empty() && iter < hn)
59
+ {
60
+ layers.push_back(vector<uint>());
61
+
62
+ vector<bool> tempvis;
63
+ tempvis.resize(hn, true);
64
+
65
+ for(it = tocheck.begin(); it != tocheck.end();)
66
+ {
67
+ uint in = *it;
68
+
69
+ for(uint j = 0; j < hnodes[in].dout; j++)
70
+ {
71
+ uint ie = hnodes[in].all[hnodes[in].out[j]];
72
+ for(uint k = 0; k < hedges[ie].dout; k++)
73
+ {
74
+ uint inn = hedges[ie].all[hedges[ie].out[k]];
75
+ if(!nvis[inn])
76
+ tempvis[in] = false;
77
+ }
78
+ }
79
+ if(tempvis[in])
80
+ {
81
+ nnext.push_back(in);
82
+ layers.back().push_back(in);
83
+ it = tocheck.erase(it);
84
+ nodecount++;
85
+ }
86
+ else
87
+ it++;
88
+ }
89
+ if(nnext.empty())
90
+ {
91
+ is_cyclic = true;
92
+ break;
93
+ }
94
+
95
+ for(uint i = 0; i < (uint)(nnext.size()); i++)
96
+ nvis[nnext[i]] = true;
97
+ nnext.clear();
98
+
99
+ iter++;
100
+ }
101
+ if(iter > he)
102
+ is_cyclic = true;
103
+
104
+ order.clear();
105
+ order.resize(hn, 0);
106
+ uint index = 0;
107
+ for(uint i = 0; i < layers.size(); ++i)
108
+ for(uint j = 0; j < layers[i].size(); ++j, ++index)
109
+ order[index] = layers[i][j];
110
+
111
+ return 0;
112
+ }
113
+
114
+ int HyperGraph::PairNodeAndEdge(uint ni, uint ei, double t, bool force)
115
+ {
116
+ if(!force && t == 0.0)
117
+ return 0;
118
+
119
+ checkbound(ei, he);
120
+ checkbound(ni, hn);
121
+ hedges[ei].all.push_back(ni);
122
+ hedges[ei].tau.push_back(t);
123
+ hedges[ei].dall++;
124
+ hnodes[ni].all.push_back(ei);
125
+ hnodes[ni].tau.push_back(t);
126
+ hnodes[ni].dall++;
127
+
128
+ if(t < 0)
129
+ {
130
+ hedges[ei].in.push_back(hedges[ei].dall-1);
131
+ hedges[ei].din++;
132
+ hnodes[ni].out.push_back(hnodes[ni].dall-1);
133
+ hnodes[ni].dout++;
134
+ }
135
+ else
136
+ {
137
+ hedges[ei].out.push_back(hedges[ei].dall-1);
138
+ hedges[ei].dout++;
139
+ hnodes[ni].in.push_back(hnodes[ni].dall-1);
140
+ hnodes[ni].din++;
141
+ }
142
+
143
+ return 0;
144
+ }
145
+
146
+ int AddGens(uint nq, vector<HyperGen>* vhg, uint* cq)
147
+ {
148
+ if(nq == 1)
149
+ vhg->push_back(HyperGen(0.0));
150
+ else
151
+ vhg->resize(*cq+nq, HyperGen(0.0));
152
+ (*cq)+=nq;
153
+ return 0;
154
+ }
155
+
156
+ int SetGenVal(uint i, double nval, vector<HyperGen>* vhg, uint s)
157
+ {
158
+ checkbound(i, s);
159
+ (*vhg)[i].val = nval;
160
+ return 0;
161
+ }
162
+
163
+ int SetGenUb(uint i, double nub, vector<HyperGen>* vhg, uint s)
164
+ {
165
+ checkbound(i, s);
166
+ (*vhg)[i].ub = nub;
167
+ return 0;
168
+ }
169
+
170
+ int SetGenLb(uint i, double nlb, vector<HyperGen>* vhg, uint s)
171
+ {
172
+ checkbound(i, s);
173
+ (*vhg)[i].lb = nlb;
174
+ return 0;
175
+ }
176
+
177
+ int SetGenName(uint i, string name, vector<HyperGen>* vhg, uint s)
178
+ {
179
+ checkbound(i, s);
180
+ (*vhg)[i].name = name;
181
+ return 0;
182
+ }
183
+
184
+ int RemoveGen(uint n1, vector<HyperGen>* hv1, vector<HyperGen>* hv2, uint* hq)
185
+ {
186
+ for(uint i = 0; i < (*hv1)[n1].dall; i++)
187
+ {
188
+ uint n2 = (*hv1)[n1].all[i];
189
+ vector<uint>::iterator ita = (*hv2)[n2].all.begin();
190
+ vector<double>::iterator itt = (*hv2)[n2].tau.begin();
191
+ vector<uint>::iterator iti = (*hv2)[n2].in.begin();
192
+ vector<uint>::iterator ito = (*hv2)[n2].out.begin();
193
+
194
+ uint remindex = 0;
195
+ uint indexcnt = 0;
196
+ while(ita != (*hv2)[n2].all.end())
197
+ {
198
+ if(n1 == *ita)
199
+ {
200
+ ita = (*hv2)[n2].all.erase(ita);
201
+ itt = (*hv2)[n2].tau.erase(itt);
202
+ remindex=indexcnt;
203
+ }
204
+ else
205
+ {
206
+ ita++;
207
+ itt++;
208
+ indexcnt++;
209
+ }
210
+ }
211
+
212
+ while(iti != (*hv2)[n2].in.end())
213
+ {
214
+ if(remindex == *iti)
215
+ iti = (*hv2)[n2].in.erase(iti);
216
+ else
217
+ {
218
+ if(*iti > remindex)
219
+ (*iti)--;
220
+ iti++;
221
+ }
222
+ }
223
+
224
+ while(ito != (*hv2)[n2].out.end())
225
+ {
226
+ if(remindex == *ito)
227
+ ito = (*hv2)[n2].out.erase(ito);
228
+ else
229
+ {
230
+ if(*ito > remindex)
231
+ (*ito)--;
232
+ ito++;
233
+ }
234
+ }
235
+ }
236
+
237
+ vector<HyperGen>::iterator it = hv1->begin();
238
+ for(uint i = 0; i < n1; i++)
239
+ it++;
240
+
241
+ hv1->erase(it);
242
+ (*hq)--;
243
+
244
+ return 0;
245
+ }
246
+
247
+ int HyperGraph::AddNodes(uint n)
248
+ {
249
+ return AddGens(n, &hnodes, &hn);
250
+ }
251
+
252
+ int HyperGraph::SetNodeVal(uint i, double nval)
253
+ {
254
+ return SetGenVal(i, nval, &hnodes, hn);
255
+ }
256
+
257
+ int HyperGraph::SetNodeUb(uint i, double nub)
258
+ {
259
+ return SetGenUb(i, nub, &hnodes, hn);
260
+ }
261
+
262
+ int HyperGraph::SetNodeLb(uint i, double nlb)
263
+ {
264
+ return SetGenLb(i, nlb, &hnodes, hn);
265
+ }
266
+
267
+
268
+ int HyperGraph::SetNodeName(uint i, string name)
269
+ {
270
+ return SetGenName(i, name, &hnodes, hn);
271
+ }
272
+
273
+ int HyperGraph::AddEdges(uint e)
274
+ {
275
+ return AddGens(e, &hedges, &he);
276
+ }
277
+
278
+ int HyperGraph::SetEdgeVal(uint i, double nval)
279
+ {
280
+ return SetGenVal(i, nval, &hedges, he);
281
+ }
282
+
283
+ int HyperGraph::SetEdgeUb(uint i, double nub)
284
+ {
285
+ return SetGenUb(i, nub, &hedges, he);
286
+ }
287
+
288
+ int HyperGraph::SetEdgeLb(uint i, double nlb)
289
+ {
290
+ return SetGenLb(i, nlb, &hedges, he);
291
+ }
292
+
293
+ int HyperGraph::SetEdgeName(uint i, string name)
294
+ {
295
+ return SetGenName(i, name, &hedges, he);
296
+ }
297
+
298
+ int HyperGraph::RemoveEdge(uint e)
299
+ {
300
+ return RemoveGen(e, &hedges, &hnodes, &he);
301
+ }
302
+ int HyperGraph::RemoveNode(uint n)
303
+ {
304
+ return RemoveGen(n, &hnodes, &hedges, &hn);
305
+ }
306
+
307
+ int HyperGraph::Clean(vector<bool>& exin, vector<bool>& exout)
308
+ {
309
+ vector<uint> nstatus;
310
+ vector<uint> ndin;
311
+ vector<uint> ndout;
312
+ for(long i = 0; i < hn; i++)
313
+ {
314
+ ndin.push_back(hnodes[i].din);
315
+ ndout.push_back(hnodes[i].dout);
316
+ }
317
+ nstatus.resize(hn, 0);
318
+ vector<uint> estatus;
319
+ vector<uint> edin;
320
+ vector<uint> edout;
321
+ for(long i = 0; i < he; i++)
322
+ {
323
+ edin.push_back(hedges[i].din);
324
+ edout.push_back(hedges[i].dout);
325
+ }
326
+ estatus.resize(he, 0);
327
+
328
+ vector<uint> nfinal;
329
+ for(long i = 0; i < hn; i++)
330
+ nfinal[i] = i;
331
+
332
+
333
+ uint erem = 1;
334
+ uint nrem = 1;
335
+ uint terem = 1;
336
+ uint tnrem = 1;
337
+
338
+ // list<double> rn;
339
+ // for(long i = 0; i < hn; i++)
340
+ // rn.push_back(i);
341
+ // list<double> re;
342
+ // for(long i = 0; i < he; i++)
343
+ // re.push_back(i);
344
+
345
+ while(erem > 0 || nrem > 0)
346
+ {
347
+ erem = 0;
348
+ nrem = 0;
349
+ // Analysing nodes
350
+ for(uint i = 0; i < hn; i++)
351
+ {
352
+ if(nstatus[i] > 0)
353
+ continue;
354
+ if(ndin[i] == 0 && !exout[i])
355
+ {
356
+ nstatus[i] = 1;
357
+ for(uint j = 0; j < hnodes[i].dout; j++)
358
+ edin[hnodes[i].all[hnodes[i].out[j]]]--;
359
+ nrem++;
360
+ tnrem++;
361
+ }
362
+ else if(ndout[i] == 0 && !exin[i])
363
+ {
364
+ nstatus[i] = 2;
365
+ for(uint j = 0; j < hnodes[i].din; j++)
366
+ edin[hnodes[i].all[hnodes[i].in[j]]]--;
367
+ nrem++;
368
+ tnrem++;
369
+ }
370
+ }
371
+
372
+ // Analysing edges
373
+ for(uint i = 0; i < he; i++)
374
+ {
375
+ if(estatus[i] > 0)
376
+ continue;
377
+ if(edin[i] == 0 || edout[i] == 0)
378
+ {
379
+ estatus[i] = 1;
380
+ for(uint j = 0; j < hedges[i].dout; j++)
381
+ ndin[hedges[i].all[hedges[i].out[j]]]--;
382
+ for(uint j = 0; j < hedges[i].din; j++)
383
+ ndout[hedges[i].all[hedges[i].in[j]]]--;
384
+
385
+ erem++;
386
+ terem++;
387
+ }
388
+ }
389
+ }
390
+
391
+ //printf("%d nnodes removed, %d edges removed\n", tnrem, terem);
392
+
393
+ return 0;
394
+ }
395
+
396
+ void checkbound(uint i, uint ub)
397
+ {
398
+ if(i > ub)
399
+ throw string("HyperGraph : index out of bound");
400
+ }
@@ -0,0 +1,90 @@
1
+ /*
2
+ * HyperGraph.h
3
+ *
4
+ * Created on: 2011-03-17
5
+ * Author: mbouchard
6
+ */
7
+
8
+ #ifndef HYPERGRAPH_H_
9
+ #define HYPERGRAPH_H_
10
+
11
+ #include <vector>
12
+ #include <limits>
13
+ #include <string>
14
+ #include <list>
15
+ #include <stdlib.h>
16
+ #define uint unsigned long
17
+
18
+ using namespace std;
19
+
20
+ class HyperGen;
21
+ class GenFunction;
22
+
23
+ class HyperGraph
24
+ {
25
+ public:
26
+ HyperGraph() {hn=0; he = 0;}
27
+ ~HyperGraph() {}
28
+ uint hn;
29
+ uint he;
30
+
31
+ int SetQty(uint n, uint e);
32
+ int AddNodes(uint n);
33
+ int SetNodeUb(uint i, double nub);
34
+ int SetNodeLb(uint i, double nlb);
35
+ int SetNodeVal(uint i, double nval);
36
+ int SetNodeName(uint i, string name);
37
+ int SetEdgeUb(uint i, double nub);
38
+ int SetEdgeLb(uint i, double nlb);
39
+ int SetEdgeVal(uint i, double nval);
40
+ int SetEdgeName(uint i, string name);
41
+ int SetTopologicalOrder();
42
+ int AddEdges(uint e);
43
+ int RemoveEdge(uint e);
44
+ int RemoveNode(uint e);
45
+ //int Contract();
46
+ int Clean(vector<bool>& exin, vector<bool>& exout);
47
+ int PairNodeAndEdge(uint ni, uint ei, double t, bool force = false);
48
+
49
+ vector<HyperGen> hnodes;
50
+ vector<HyperGen> hedges;
51
+
52
+ vector<uint> order;
53
+ vector<vector<uint> > layers;
54
+ bool is_cyclic;
55
+ };
56
+
57
+ class HyperGen
58
+ {
59
+ public:
60
+ HyperGen() {val = 0.0; dall=0; din=0; dout = 0; ub = numeric_limits<double>::infinity();}
61
+ HyperGen(double nval) {val = nval; dall=0; din=0; dout = 0; ub = numeric_limits<double>::infinity();}
62
+ ~HyperGen() {}
63
+ uint dall;
64
+ uint din;
65
+ uint dout;
66
+ // max val x; Ax=b; lb <= x <= ub; min b y + ub c+ + lb c- A'y + c+ - c- = cost
67
+ double val; // cost for hedges, demand for hnodes
68
+ double ub; // ub for hedges, positive complement cost for hnodes
69
+ double lb; // lb for hedges, negative complement cost for hnodes
70
+ string name;
71
+ // GenFunction* f;
72
+ vector<uint> all;
73
+ vector<uint> in;
74
+ vector<uint> out;
75
+ vector<double> tau;
76
+ };
77
+
78
+ /*
79
+ class GenFunction
80
+ {
81
+ public:
82
+ GenFunction() {};
83
+ ~GenFunction() {};
84
+ virtual double f(double x) = 0;
85
+ // virtual double invf(double x) = 0;
86
+ virtual double auc(double x) = 0;
87
+ };
88
+ */
89
+
90
+ #endif /* HYPERGRAPH_H_ */
@@ -12,8 +12,8 @@ vector<size_t> myGraph::ie(size_t i)
12
12
  {
13
13
  vector<size_t> out(incs[i].size());
14
14
  map<size_t, size_t>::iterator it;
15
- size_t ii;
16
- for (it = incs[i].begin(); it != incs[i].end(); ++it)
15
+ size_t ii = 0;
16
+ for (it = incs[i].begin(); it != incs[i].end(); ++it, ++ii)
17
17
  out[ii] = it->second;
18
18
  return out;
19
19
  }
@@ -32,8 +32,8 @@ vector<size_t> myGraph::in(size_t i)
32
32
  {
33
33
  vector<size_t> out(incs[i].size());
34
34
  map<size_t, size_t>::iterator it;
35
- size_t ii;
36
- for (it = incs[i].begin(); it != incs[i].end(); ++it)
35
+ size_t ii = 0;
36
+ for (it = incs[i].begin(); it != incs[i].end(); ++it, ++ii)
37
37
  out[ii] = it->first;
38
38
  return out;
39
39
  }
@@ -76,6 +76,8 @@ size_t myGraph::addEdge(size_t i, size_t j)
76
76
  else
77
77
  {
78
78
  edges.push_back(pair<size_t, size_t>(i,j));
79
+ incs[i][j] = _e;
80
+ incs[j][i] = _e;
79
81
  return _e++;
80
82
  }
81
83
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genmodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.30
4
+ version: 0.0.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathieu Bouchard
@@ -30,6 +30,8 @@ files:
30
30
  - ext/Genmodel/Genmodel.cpp
31
31
  - ext/Genmodel/GraphTools.cpp
32
32
  - ext/Genmodel/GraphTools.h
33
+ - ext/Genmodel/HyperGraph.cpp
34
+ - ext/Genmodel/HyperGraph.h
33
35
  - ext/Genmodel/ProblemReaderOsi.cpp
34
36
  - ext/Genmodel/ProblemReaderOsi.h
35
37
  - ext/Genmodel/extconf.rb