00001 #include<LEDA/graph.h>
00002 #include<LEDA/graphwin.h>
00003 #include<LEDA/graph_misc.h>
00004 #include<LEDA/graph_alg.h>
00005 #include<LEDA/delaunay.h>
00006
00007 #include<scil/scil.h>
00008
00009 using namespace LEDA;
00010 using namespace SCIL;
00011
00012 #define RHO 0.4
00013
00014 class mysep : public sym_constraint {
00015 graph& G;
00016 var_map<node>& VM;
00017 var_map<edge>& EM;
00018
00019 public:
00020 mysep(graph& G_, var_map<node>& VM_, var_map<edge>& EM_)
00021 : G(G_), VM(VM_), EM(EM_) {
00022 };
00023
00024 status separate(subproblem& s) {
00025 status sta=no_constraint_found;
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
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 return sta;
00076 };
00077 };
00078
00079 void MY_DFS(graph& G, node s, node_array<bool>& R, edge_array<int>& C, edge_array<int>& F) {
00080 R[s]=true;
00081 edge e;
00082 forall_out_edges(e,s) if(!R[target(e)]) {
00083 if(C[e]!=F[e]) MY_DFS(G,target(e),R,C,F);
00084 }
00085 forall_in_edges(e,s) if(!R[source(e)]) {
00086 if(F[e]>0) MY_DFS(G,source(e),R,C,F);
00087 }
00088 };
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 void compute_partitioning(graph& G, edge_array<double>& C, list<node>& L) {
00152
00153 G.make_undirected();
00154
00155 ILP_Problem IP(Optsense_Min);
00156
00157 var_map<node> VM;
00158 node u;
00159 row r;
00160 forall_nodes(u, G) {
00161 VM[u]=IP.add_variable(0,0,1,Vartype_Integer);
00162 r+=VM[u];
00163 };
00164
00165 u=G.choose_node();
00166 IP.add_basic_constraint((row) VM[u]==0);
00167 IP.add_basic_constraint(r>=(int) (RHO*G.number_of_nodes()));
00168 IP.add_basic_constraint(r<=(int) ((1-RHO)*G.number_of_nodes()));
00169
00170 var_map<edge> EM;
00171 edge e;
00172 forall_edges(e,G) {
00173 EM[e]=IP.add_variable(1/C[e],0,1,Vartype_Integer);
00174 IP.add_basic_constraint((row) EM[e]>=(row) VM[source(e)]-VM[target(e)]);
00175 IP.add_basic_constraint((row) EM[e]>=(row) VM[target(e)]-VM[source(e)]);
00176 }
00177
00178
00179
00180
00181
00182 IP.optimize();
00183
00184 forall_nodes(u, G) {
00185 if(IP.get_solution(VM[u])>0.5) L.append(u);
00186 }
00187
00188 };
00189
00190 void compute_partitioning_2(graph& G, edge_array<double>& C, list<node>& L) {
00191 G.make_undirected();
00192
00193 ILP_Problem IP(Optsense_Min);
00194
00195 var_map<node> SM;
00196 var_map<node> TM;
00197 node u;
00198 row rs;
00199 row rt;
00200 forall_nodes(u, G) {
00201 SM[u]=IP.add_variable(-1,0,1,Vartype_Integer);
00202 rs+=SM[u];
00203 TM[u]=IP.add_variable(-1,0,1,Vartype_Integer);
00204 rt+=TM[u];
00205 IP.add_basic_constraint(SM[u]+TM[u]<=1);
00206 };
00207
00208 cout<<"l\n";
00209 u=G.choose_node();
00210 IP.add_basic_constraint((row) SM[u]-TM[u]<=0);
00211 cout<<"d\n";
00212 IP.add_basic_constraint(rs<=(int) ((1-RHO)*G.number_of_nodes()));
00213 IP.add_basic_constraint(rt<=(int) ((1-RHO)*G.number_of_nodes()));
00214
00215 cout<<"Hier\n";
00216 edge e;
00217 forall_edges(e,G) {
00218 IP.add_basic_constraint(SM[source(e)]+TM[target(e)]<=1);
00219 IP.add_basic_constraint(TM[source(e)]+SM[target(e)]<=1);
00220 }
00221
00222 cout<<"done\n";
00223
00224 IP.optimize();
00225
00226 forall_nodes(u, G) {
00227 if(IP.get_solution(SM[u])+IP.get_solution(TM[u])<0.5) L.append(u);
00228 }
00229
00230 };
00231
00232
00233 void triang_points(GraphWin& GW)
00234 {
00235 graph& G=GW.get_graph();
00236 int n=G.number_of_nodes();
00237 if (n!=0) {
00238
00239 GW.set_flush(false);
00240
00241 point_set T;
00242 node_array<node> tr(T,n,nil);
00243 node_array<int> num(T,n,0);
00244 node v,v1;
00245 int i=0;
00246
00247 double x,y;
00248 forall_nodes(v1,G)
00249 {
00250 x=GW.get_position(v1).xcoord();
00251 y=GW.get_position(v1).ycoord();
00252 point p(x,y);
00253 v=T.insert(p);
00254 tr[v]=v1;
00255 num[v]=i++;
00256 }
00257
00258 G.del_all_edges();
00259 GW.update_graph();
00260 edge e;
00261 forall_edges(e,T) if (num[T.source(e)]<num[T.target(e)])
00262 GW.new_edge(tr[T.source(e)],tr[T.target(e)]);
00263 GW.redraw();
00264 GW.set_flush(true);
00265 GW.redraw();
00266 }
00267 }
00268
00269 void generate_del(GraphWin& GW)
00270 {
00271 GW.set_flush(false);
00272
00273 graph& G=GW.get_graph();
00274 if (!G.empty()) GW.clear_graph();
00275
00276 int n=10;
00277
00278 panel p;
00279 p.int_item("Number of nodes : ",n);
00280 p.open(GW.get_window());
00281
00282 random_source ran;
00283
00284 double x,y;
00285 for(int i=0;i<n;i++)
00286 { ran >> x >> y;
00287 x=(x+0.08)*(GW.get_xmax()-GW.get_xmin())*0.9+GW.get_xmin();
00288 y=(y+0.08)*(GW.get_ymax()-GW.get_ymin())*0.9+GW.get_ymin();
00289 point p1(x,y);
00290 GW.new_node(p1);
00291 }
00292 triang_points(GW);
00293
00294 GW.set_flush(true);
00295 GW.redraw();
00296 }
00297
00298 int main()
00299 {
00300 GraphWin GW;
00301
00302
00303 GW.set_node_width(8);
00304 GW.set_node_height(8);
00305 GW.set_flush(true);
00306 GW.set_edge_direction(undirected_edge);
00307
00308
00309 int gn=gw_add_menu(GW,"Generation");
00310 gw_add_simple_call(GW,triang_points,"Triangulate defined points",gn);
00311 gw_add_simple_call(GW,generate_del, "Generate random Delaunay-Graph",gn);
00312
00313 GW.display();
00314
00315 while(GW.edit())
00316 {
00317 graph& G=GW.get_graph();
00318
00319 edge e;
00320 edge_array<double> C(G);
00321 forall_edges(e, G)
00322 C[e]=GW.get_position(source(e)).distance(GW.get_position(target(e)));
00323
00324 list<node> T;
00325 compute_partitioning_2(G, C, T);
00326
00327 GW.set_flush(false);
00328 node u;
00329 forall_nodes(u,G) {
00330 GW.set_color(u, black);
00331 }
00332 forall(u,T) {
00333 GW.set_color(u,red);
00334 }
00335 GW.set_flush(true);
00336 GW.redraw();
00337 }
00338 return 0;
00339 }