Main Page   Class Hierarchy   Compound List   File List   Contact   Download   Symbolic Constraints   Examples  

partitioning.c

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     edge_array<double> E(G);
00028     node_array<double> C(G);
00029     node_array<edge> P(G);
00030     edge e,f;
00031     forall_edges(e, G) {
00032       E[e]=s.value(EM[e]);
00033       if(E[e]<=0) E[e]=0;
00034     };
00035 
00036     G.sort_edges(E);
00037 
00038     list<edge> L=MIN_SPANNING_TREE(G,E);
00039 
00040     forall(e, L) {
00041       G.hide_edge(e);
00042     }
00043 
00044     list<edge> R=G.all_edges();
00045 
00046     forall(e, R) G.hide_edge(e);
00047     forall(e, L) G.restore_edge(e);
00048 
00049     edge_array<int> Cp(G);
00050     node_array<bool> Co(G);
00051     int sz;
00052     double d;
00053     node v;
00054 
00055     forall(e,R) {
00056       G.restore_edge(e);
00057  
00058       BICONNECTED_COMPONENTS(G, Cp);
00059 
00060       forall_nodes(v, G) Co[v]=false;
00061       sz=0; d=0;
00062       row r;
00063       forall_edges(f,G) if(Cp[f]==Cp[e]) {
00064         if(Co[source(f)]==false) { sz++; Co[source(f)]=true; }
00065         if(Co[target(f)]==false) { sz++; Co[target(f)]=true; }
00066         r+=EM[f]; d+=E[f];
00067       }
00068 
00069       if ((sz>RHO*G.number_of_nodes()) && (d<1.999)) {
00070         s.add_basic_constraint(r>=2);
00071         sta=constraint_found;
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 class myheuristic : public primal_heuristic {
00092 private:
00093   graph& G;
00094   node r;
00095   var_map<edge>& EM;
00096   var_map<node>& NM;
00097   edge_array<double>& Cost;
00098 
00099 public:
00100   myheuristic(graph& G_, var_map<edge>& EM_, var_map<node>& NM_, edge_array<double>& Cost_, node r_) 
00101   : G(G_), EM(EM_), NM(NM_), Cost(Cost_), r(r_) {
00102   };
00103 
00104   int heuristic(subproblem& Sub) {
00105     node_array<double> D(G);
00106 
00107     node v;
00108     forall_nodes(v, G) D[v]=Sub.value(NM[v]);
00109 
00110     node_array<int> D1(G);
00111     BFS(G,r,D1);
00112     forall_nodes(v, G) D[v]!=((double) D1[v])/1000.0;
00113 
00114     G.sort_nodes(D);
00115 
00116     int i=0;
00117     graph H;
00118     node s=H.new_node();
00119     node t=H.new_node();
00120     node_array<node> GtoH(G);
00121     forall_nodes(v, G) {
00122       if(i<RHO*G.number_of_nodes()) GtoH[v]=s;
00123       else {
00124         if(i>(1-RHO)*G.number_of_nodes()) GtoH[v]=t;
00125         else GtoH[v]=H.new_node();
00126       }
00127       i++;
00128     }
00129 
00130     edge_array<int> C(H,2*G.number_of_edges(), 0);
00131     edge e;
00132     forall_edges(e, G) { 
00133       C[H.new_edge(GtoH[source(e)], GtoH[target(e)])]=(int) 10000.0*Cost[e];
00134       C[H.new_edge(GtoH[target(e)], GtoH[source(e)])]=(int) 10000.0*Cost[e];
00135     }
00136 
00137     edge_array<int> F(H);
00138     MAX_FLOW(H,s,t,C,F);
00139 
00140     node_array<bool> R(H, false);
00141     MY_DFS(H,s,R,C,F);
00142 
00143     solution S;
00144 //    forall_nodes(v, G) if(!R[GtoH[v]]) S.set_value(NM[v],1);
00145  //   forall_edges(e, G) if(R[GtoH[source(e)]]!=R[GtoH[target(e)]]) S.set_value(EM[e],1);
00146     Sub.save_solution(S);
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   //IP.add_sym_constraint(new mysep(G, VM, EM));
00179 
00180   //IP.set_primal_heuristic(new myheuristic(G, EM, VM, C, u));
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 // Setup of the graph window
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 // Additional functionality of the graph window; the implementation is hidden
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 }

Generated on Tue Nov 16 15:18:18 2004 for SCIL by doxygen1.2.16