00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 void mdfs(graph& G, node u, edge_array<double>& U, edge_array<double>& U1,
00051 node_array<bool>& R, list<node>& C) {
00052 C.append(u);
00053 R[u]=true;
00054 edge e;
00055 forall_out_edges(e, u) if((U[e]-U1[e]>0.5) && (!R[target(e)]))
00056 mdfs(G, target(e), U, U1, R, C);
00057
00058
00059 };
00060
00061 void MIN_ST_CUT_SPECIAL(graph& G, edge_array<double>& D, node s, node t,
00062 edge_array<int>& C, list<node>& CUT) {
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 ILP_Problem ILP(Optsense_Max);
00073 edge e,f;
00074 double d;
00075 var_map<edge> VMM;
00076 forall_edges(e, G) {
00077 if(source(e)==s) d=1; else d=0;
00078 VMM[e]=ILP.add_variable(d, 0, 1000, Vartype_Float);
00079 };
00080 forall_edges(e, G) {
00081 row r; d=0;
00082 forall_out_edges(f, source(e)) if(D[f]>=D[e]) {
00083 r+=VMM[f]; d+=leda_max(C[f],0);
00084 }
00085 ILP.add_basic_constraint(r<=d);
00086 }
00087
00088 node v;
00089 forall_nodes(v, G) if((v!=s) && (v!=t)) {
00090 row r;
00091 forall_out_edges(e, v) r+=VMM[e];
00092 forall_in_edges(e, v) r-=VMM[e];
00093 ILP.add_basic_constraint(r==0);
00094 }
00095
00096 ILP.optimize();
00097
00098 edge_array<double> U(G);
00099 edge_array<double> U1(G);
00100 forall_edges(e, G) {
00101 row r; d=0;
00102 forall_out_edges(f, source(e)) if(D[f]>=D[e]) {
00103 U1[e]+=ILP.get_solution(VMM[e]); U[e]+=C[e];
00104 }
00105 }
00106
00107 node_array<bool> R(G, false);
00108 mdfs(G, s, U, U1, R, CUT);
00109 };
00110
00111
00112 class dir_cut2 : public cons_obj {
00113 private:
00114 var_map<edge>& VMy;
00115 graph& G;
00116 edge_array<double>& D;
00117 list<row> NZ;
00118
00119 public:
00120
00121 dir_cut2(graph& G_, node_array<double>& C, var_map<edge>& VMy_,
00122 edge_array<double>& D_, GraphWin& GW)
00123 : cons_obj(Greater, 1), VMy(VMy_), G(G_), D(D_) {
00124 node_array<double> CD(G);
00125 node u;
00126 forall_nodes(u, G) CD[u]=C[u];
00127 edge e;
00128 forall_edges(e, G) {
00129 if((CD[source(e)]>0) && (CD[target(e)]==0) && (CD[source(e)]>D[e]))
00130 CD[source(e)]=D[e];
00131 }
00132 forall_edges(e, G) {
00133
00134 if(coeff(e, CD)>0) {
00135 NZ.append(VMy[e]);
00136
00137 }
00138 };
00139
00140 };
00141
00142 double coeff(edge e, node_array<double>& CD) {
00143 if((CD[source(e)]>0) && (CD[source(e)]<=D[e])) return 1;
00144 return 0;
00145 };
00146
00147 virtual void non_zero_entries(row& r) {
00148 row v;
00149 forall(v, NZ) r+=v;
00150 };
00151 };
00152
00153
00154 class Broadcast2 : public sym_constraint {
00155
00156 private:
00157 graph& G;
00158 var_map<edge>& VM;
00159 var_map<edge>& VMy;
00160 GraphWin& GW;
00161 edge_array<double>& C;
00162
00163 public:
00164
00165 Broadcast2(graph& G_, var_map<edge>& VM_, var_map<edge>& VMy_,
00166 GraphWin& GW_, edge_array<double>& C_)
00167 : G(G_), VM(VM_), VMy(VMy_), GW(GW_), C(C_) {
00168 };
00169
00170 void init(subproblem& s) {
00171 };
00172
00173 void mc_dfs(node u, edge_array<int>& c, edge_array<int>& f, node_array<bool>& r) {
00174 r[u]=true;
00175 edge e;
00176 forall_out_edges(e, u)
00177 if((!r[target(e)]) && (f[e]!=c[e]))
00178 mc_dfs(target(e), c, f, r);
00179 };
00180
00181 status separate(subproblem& s) {
00182 edge_array<int> cap(G);
00183 edge e;
00184
00185 status st=no_constraint_found;
00186
00187 node_array<double> SE(G, 0);
00188
00189 forall_edges(e, G) {
00190 cap[e]=(int) (1000*(s.value(VMy[e])+SE[source(e)]));
00191 SE[source(e)]+=s.value(VMy[e]);
00192 };
00193
00194 node u, v, w;
00195 int k;
00196 edge_array<int> f(G);
00197 u=G.choose_node();
00198 forall_nodes(v, G) if (u!=v) {
00199 {
00200 k=MAX_FLOW(G, u, v, cap, f);
00201
00202 node_array<bool> r(G, false);
00203 mc_dfs(u, cap, f, r);
00204 node_array<double> r1(G);
00205 forall_nodes(w, G) if(!r[w]) r1[w]=MAX_DOUBLE; else r1[w]=0;
00206 cons_obj* c=new dir_cut2(G, r1, VMy, C, GW);
00207 if(c->violated(s)) {
00208
00209 s.add_basic_constraint(c);
00210 st=constraint_found;
00211 };
00212 }
00213 {
00214 k=MAX_FLOW(G, v, u, cap, f);
00215
00216 node_array<bool> r(G, false);
00217 mc_dfs(v, cap, f, r);
00218 node_array<double> r1(G);
00219 forall_nodes(w, G) if(!r[w]) r1[w]=MAX_DOUBLE; else r1[w]=0;
00220 cons_obj* c=new dir_cut2(G, r1, VMy, C, GW);
00221 if(c->violated(s)) {
00222
00223 s.add_basic_constraint(c);
00224 st=constraint_found;
00225 };
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 return st;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 };
00307
00308 double ComputeBroadcast2(graph& G, edge_array<double>& Cost, list<edge>& T,
00309 GraphWin& GW) {
00310
00311 list<edge> MST=MIN_SPANNING_TREE(G, Cost);
00312 node_array<double> MSTC(G,0);
00313 edge e;
00314 forall(e,MST) {
00315 MSTC[source(e)]=leda_max(MSTC[source(e)], Cost[e]);
00316 MSTC[target(e)]=leda_max(MSTC[target(e)], Cost[e]);
00317 };
00318 node u; double mstc=0;
00319 forall_nodes(u, G) mstc+=MSTC[u]*MSTC[u];
00320
00321 graph H;
00322 edge f;
00323 node_array<node> GtoH(G);
00324 node_map<point> P(H);
00325 edge_array<edge> HtoG(H,2*G.number_of_edges(),e);
00326
00327 forall_nodes(u, G) {
00328 GtoH[u]=H.new_node();
00329 P[GtoH[u]]=GW.get_position(u);
00330 };
00331
00332 forall_edges(e, G) {
00333 f=H.new_edge(GtoH[source(e)], GtoH[target(e)]);
00334 HtoG[f]=e;
00335 f=H.new_edge(GtoH[target(e)], GtoH[source(e)]);
00336 HtoG[f]=e;
00337 };
00338
00339 edge_array<double> Cost2(H);
00340 forall_edges(e, H) Cost2[e]=Cost[HtoG[e]];
00341
00342 cout<<"created copy\n";
00343
00344 ILP_Problem IP(Optsense_Min);
00345
00346 e=nil;
00347 var_map<edge> VM;
00348 var_map<edge> VMy;
00349
00350 forall_edges(e,H) {
00351 VM[e]=IP.add_variable(0, 0, 1, Vartype_Integer);
00352 VMy[e]=IP.add_variable(Cost[HtoG[e]]*Cost[HtoG[e]], 0, 1, Vartype_Integer);
00353 }
00354
00355 cout<<"made constr 1\n";
00356
00357 forall_edges(e,H) {
00358 row r1;
00359 forall_out_edges(f,source(e)) if(Cost[HtoG[f]]>=Cost[HtoG[e]]-0.001) r1+=VMy[f];
00360 IP.add_basic_constraint(r1 >= VM[e]);
00361 }
00362
00363 cout<<"made cosntr 2\n";
00364
00365 forall_nodes(u, H) {
00366 row r;
00367 forall_out_edges(e, u) r+=VMy[e];
00368 IP.add_basic_constraint(r==1);
00369 };
00370
00371 cout<<"mad constr 3\n";
00372
00373 IP.add_sym_constraint(new StronglyConnected(H,VM));
00374
00375 IP.add_sym_constraint(new Broadcast2(H, VM, VMy, GW, Cost2));
00376
00377 IP.optimize();
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 list<edge> T1;
00390 double optc=0;
00391 forall_edges(e, H) {
00392 if(IP.get_solution(VM[e])>0.5) {
00393 T1.append(e);
00394 T.append(HtoG[e]);
00395 };
00396 }
00397 node_array<double> OPTC(H, 0);
00398 forall(e,T1) {
00399 OPTC[source(e)]=leda_max(OPTC[source(e)], Cost2[e]);
00400 };
00401 forall_nodes(u, H) optc+=OPTC[u]*OPTC[u];
00402
00403 cout<<"Number of nodes "<<G.number_of_nodes()<<endl;
00404 cout<<"Cost of mst "<<mstc<<"\n";
00405 cout<<"Cost of optimal solution "<<optc<<"\n";
00406
00407 cout<<"Save "<<(mstc-optc)/mstc*100<<"\n";
00408
00409
00410 return (mstc-optc)/mstc*100;
00411 }