Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
216 lines (192 loc) · 7.48 KB

File metadata and controls

216 lines (192 loc) · 7.48 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// Created by Jannick Borowitz on 24.04.22.
//
#ifdef USEILP
#include <algorithm>
#include <iostream>
#include <memory>
#include "DeltaOrientationsConfig.h"
#include "dyn_graph_access.h"
#include "graph_access.h"
#include "gurobi_c++.h"
#include "ilp.h"
void solveUsingILP(graph_access &orientation, dyn_graph_access &G,
const DeltaOrientationsConfig &config) {
std::cout << "Solving using ILP" << std::endl;
try {
auto env = std::make_shared<GRBEnv>();
GRBModel model = GRBModel(*env);
// set model
model.set(GRB_StringAttr_ModelName, "Minimizing Out-Degree");
model.set(GRB_DoubleParam_TimeLimit, config.time_limit);
model.set(GRB_DoubleParam_MIPGap, 0);
// set decision variables for edges
std::vector<GRBVar> edges(2 * G.number_of_edges());
for (auto &edgeVar : edges) {
edgeVar = model.addVar(0.0, 1.0, 0, GRB_BINARY);
}
graph_access G_undirected;
G.convert_to_graph_access(G_undirected);
// add constraints
GRBVar phi = model.addVar(
0.0, 1.0, 0, GRB_INTEGER); // std::numeric_limits<NodeID>::max();
phi.set(GRB_DoubleAttr_UB, G.maxDegree());
for (NodeID node = 0; node < G.number_of_nodes(); ++node) {
GRBLinExpr outDeg = 0;
forall_out_edges(G_undirected, e, node) {
outDeg += edges[e]; // sum up potential outgoing edges
if (node < G_undirected.getEdgeTarget(e)) {
NodeID target = G_undirected.getEdgeTarget(e);
forall_out_edges(G_undirected, rev_e, target) {
if (G_undirected.getEdgeTarget(rev_e) == node) {
GRBLinExpr oneDirectedEdgeExist = edges[e] + edges[rev_e];
model.addConstr(oneDirectedEdgeExist == 1,
"either directed or reversed edge must exist");
break;
}
}
endfor
}
}
model.addConstr(outDeg <= phi, "outDeg is bounded by phi");
endfor
}
// set objective
model.setObjective(GRBLinExpr(phi), GRB_MINIMIZE);
// optimize model
model.optimize();
// set solution
if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
orientation.start_construction(G_undirected.number_of_nodes(),
G_undirected.number_of_edges());
for (NodeID node = 0; node < G_undirected.number_of_nodes(); ++node) {
orientation.new_node();
forall_out_edges(G_undirected, e, node) {
if (round(edges[e].get(GRB_DoubleAttr_X)) == 1) {
orientation.new_edge(node, G_undirected.getEdgeTarget(e));
}
}
endfor
}
orientation.finish_construction();
std::cout << "solution of ILP: " << phi.get(GRB_DoubleAttr_X)
<< std::endl;
} else {
std::cout << "No solution found by ILP solver" << std::endl;
exit(EXIT_FAILURE);
}
} catch (GRBException &e) {
std::cout << "Oooops something bad happend with Gurobi: " << e.getMessage()
<< std::endl;
exit(EXIT_FAILURE);
}
}
void solveUsingRelaxedILP(graph_access &orientation, dyn_graph_access &G,
const DeltaOrientationsConfig &config) {
try {
// Todo a bit hacky but it allows us to easier map between decision
// variables and edges in AdjArray
// because we get an contiguously edge vector
std::cout << "TODO update" << std::endl;
exit(0);
graph_access GStatic;
G.convert_to_graph_access(GStatic);
auto env = std::make_shared<GRBEnv>();
GRBModel model = GRBModel(*env);
// define variables
// for each (edge,endpoint) a variable is needed for setting orientation
std::vector<GRBVar> edges(GStatic.number_of_edges());
// set model
model.set(GRB_StringAttr_ModelName, "Minimizing Out-Degree");
model.set(GRB_DoubleParam_TimeLimit, config.time_limit);
model.set(GRB_DoubleParam_MIPGap, 0);
// set decision variables
for (auto &edgeVar : edges) {
edgeVar = model.addVar(0.0, 1.0, 0, GRB_CONTINUOUS);
}
// add constraints
GRBVar maxOutDeg = model.addVar(
0.0, 1.0, 0, GRB_CONTINUOUS); // std::numeric_limits<NodeID>::max();
// ToDo if we observe a performance decrease
// we maybe need to determine a better upper bound for maxOutDeg
// e.g. determine current maxDegree
maxOutDeg.set(GRB_DoubleAttr_UB, G.maxDegree());
DELTAORI_LOG("nodes: " << G.number_of_nodes());
for (NodeID node = 0; node < GStatic.number_of_nodes(); ++node) {
GRBLinExpr outDeg = 0;
NodeID currentOutDeg = 0;
forall_out_edges(GStatic, e, node) {
// Todo This now the hacky thing: e is also the position in edges
outDeg += edges[e]; // sum up potential outgoing edges
++currentOutDeg;
if (node < GStatic.getEdgeTarget(e)) {
// edges[e].set(GRB_Double)
auto target = GStatic.getEdgeTarget(e);
forall_out_edges(GStatic, rev_e, target) {
if (GStatic.getEdgeTarget(rev_e) == node) {
GRBLinExpr oneDirectedEdgeExist = edges[e] + edges[rev_e];
DELTAORI_DEBUG(DELTAORI_LOG("add oneDirectedEdgeExist bound"));
model.addConstr(oneDirectedEdgeExist == 1,
"either directed or reversed edge must exist");
break;
}
}
endfor
}
}
DELTAORI_DEBUG(DELTAORI_LOG("add outDeg bound"));
model.addConstr(outDeg <= maxOutDeg, "outDeg is bounded by maxOutDeg");
endfor
}
// set objective
model.setObjective(GRBLinExpr(maxOutDeg), GRB_MINIMIZE);
// optimize model
model.optimize();
// set solution
if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL ||
model.get(GRB_IntAttr_Status) == GRB_TIME_LIMIT) {
DELTAORI_LOG("found ILP solution");
orientation.start_construction(GStatic.number_of_nodes(),
GStatic.number_of_edges() / 2);
DELTAORI_DEBUG(NodeID maxFoundOutDeg = 0;);
// Todo set oriented edges
for (NodeID node = 0; node < GStatic.number_of_nodes(); ++node) {
orientation.new_node();
DELTAORI_DEBUG(NodeID outDeg = 0;);
forall_out_edges(GStatic, e, node) {
// create the oriented edge if model decided this way
// DELTAORI_LOG(edges[e].get(GRB_DoubleAttr_X));
if (round(edges[e].get(GRB_DoubleAttr_X)) == 1) {
orientation.new_edge(node, GStatic.getEdgeTarget(e));
DELTAORI_DEBUG(++outDeg;);
#ifndef DELTAORI_NDEBUG
forall_out_edges(GStatic, rev_e, GStatic.getEdgeTarget(e)) {
if (GStatic.getEdgeTarget(rev_e) == node) {
DELTAORI_ASSERT(edges[rev_e].get(GRB_DoubleAttr_X) == 0.5 ||
round(edges[rev_e].get(GRB_DoubleAttr_X)) == 0);
break;
}
}
endfor
#endif
}
}
endfor DELTAORI_DEBUG(
if (outDeg > maxFoundOutDeg) { maxFoundOutDeg = outDeg; });
}
// DELTAORI_LOG("max found out deg: " << maxFoundOutDeg);
DELTAORI_ASSERT(maxOutDeg.get(GRB_DoubleAttr_X) == maxFoundOutDeg);
orientation.finish_construction();
// DELTAORI_LOG("MINIMUM maxOutDegree (ILP): " <<
// maxOutDeg.get(GRB_DoubleAttr_X));
std::cout << "maxOutDegree (ILP Relaxed): "
<< maxOutDeg.get(GRB_DoubleAttr_X) << std::endl;
} else {
std::cout << "No solution found by ILP solver" << std::endl;
exit(EXIT_FAILURE);
}
} catch (GRBException &e) {
exit(EXIT_FAILURE);
}
}
#endif
Morty Proxy This is a proxified and sanitized view of the page, visit original site.