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

path.cc

00001 #ifndef PATH_CONSTRAINT_CC
00002 #define PATH_CONSTRAINT_CC
00003 
00004 #include<scil/scil.h>
00005 #include<LEDA/graph.h>
00006 #include<LEDA/graph_alg.h>
00007 #include<LEDA/set.h>
00008 #include<scil/constraints/path.h>
00009 #include<LEDA/p_queue.h>
00010 
00011 using namespace LEDA;
00012 using namespace SCIL;
00013 
00014 <<<<<<< path.cc
00015 #define VALONE 1000.0
00016 #define NT int
00017 =======
00018 #define VALONE 1000.0
00019 //#define NT int
00020 >>>>>>> 1.3
00021 
00022 class PATH::path_cutset_inequality : public cons_obj {
00023 private:
00024   graph& G;
00025   scil_map<edge>& VM;
00026   node_array<bool> cut;
00027   double rhs;
00028   
00029 public:
00030   
00031   path_cutset_inequality(graph& Gt, scil_map<edge>& VM_, list<node>& L) 
00032     : cons_obj(Greater, 1), G(Gt), VM(VM_) {
00033     node v;
00034     cut.init(G, false);
00035     forall(v,L) cut[v]=true;
00036   }
00037 
00038   void non_zero_entries(row& r) { 
00039     edge e;
00040     forall_edges(e,G) {
00041       if(coeff(e)==1) r+=VM(e);
00042     }
00043   }
00044 
00045   virtual
00046   ~path_cutset_inequality() {};
00047   
00048   double coeff(edge e) { 
00049     if(e==nil) return 0;
00050     if((cut[G.source(e)]) && (!cut[G.target(e)])) return 1;
00051     return(0);
00052   };
00053 };
00054 
00055 PATH::PATH(graph& G_, node u_, node v_, scil_map<edge>& VM_)
00056   /*{\Mcreate Ensures that there is a path from $u$ to $v$ in the graph
00057     selected by the edges in VM.\\
00058     For a demo see: ptsp.
00059     }*/
00060   : G(G_), u(u_), v(v_), VM(VM_) { 
00061 };
00062 
00063 void PATH::dfs(graph& G, node u, node_array<bool>& R) {
00064   R[u]=true;
00065   edge e;
00066   forall_out_edges(e, u) if(!R[target(e)]) {
00067     dfs(G, target(e), R);
00068   };
00069 };
00070 
00071 void PATH::dfs(graph& G, node u, edge_array<NT>& val, edge_array<NT>& f, 
00072                node_array<bool>& R, list<node>& mc) {
00073   R[u]=true; mc.append(u);
00074   edge e;
00075   forall_out_edges(e, u) if((!R[target(e)]) && (val[e]!=f[e])) {
00076     dfs(G, target(e), val, f, R, mc);
00077   };
00078   forall_in_edges(e, u) if((!R[source(e)]) && (f[e]!=0)) {
00079     dfs(G, source(e), val, f, R, mc);
00080   };
00081 };
00082 
00083 #define NT int
00084 
00085 PATH::status PATH::separate(subproblem& S) {
00086   //cout<<"prep\n";
00087   // Preperation
00088   int i=0;
00089   edge e;
00090   //node s=G.new_node();
00091   //edge e1=G.new_edge(s,u);
00092 
00093   edge_array<NT> val(G);
00094   forall_edges(e, G) 
00095     //if(e==e1) { 
00096     //  val[e]=VALONE;
00097     //} else {
00098     val[e]=leda_max((int) (VALONE*S.value(VM(e)))+1,1);
00099   //};
00100 
00101 <<<<<<< path.cc
00102 /*
00103   node_array<bool> R1(G, false);
00104   R1[u]=true;
00105   p_queue<NT, edge> PQ;
00106   edge_array<pq_item> EI(G, nil);
00107   NT cval=(int) VALONE, cs=0;
00108 
00109   forall_out_edges(e, u) {
00110       EI[e]=PQ.insert(-val[e], e);
00111       cs+=val[e];
00112   };
00113 
00114   bool sucess=false;
00115   edge e1;
00116   list<node> NL;
00117   NL.append(u);
00118   //cout<<"start\n";
00119 
00120   while (!PQ.empty()) {
00121       while(!PQ.empty() && (-PQ.prio(PQ.find_min())>=cval)) {
00122           e1=PQ.inf(PQ.find_min());
00123           PQ.del_min();
00124           R1[target(e1)]=true;
00125           NL.append(target(e1));
00126           cs-=val[e1];
00127           forall_in_edges(e, target(e1)) if((e!=e1) && (R1[source(e)])) {
00128               cs-=val[e];
00129               if(EI[e]==nil) cout<<"EROR2\n";
00130               PQ.del_item(EI[e]);
00131           };
00132           forall_out_edges(e, target(e1)) if(!R1[target(e)]) {
00133               cs+=val[e];
00134               EI[e]=PQ.insert(-val[e],e);
00135           };
00136           if(target(e1)==v) {
00137               cs=2*(int) VALONE;
00138               PQ.clear();
00139           };
00140       };
00141 
00142       if(cs<VALONE*900/1000) {
00143           cout<<cs<<" Heuristic found cut\n";
00144           cout<<NL.size()<<" size\n";
00145           cons_obj* c=new path_cutset_inequality(G,VM,NL);
00146           if(!(c->violated(S))) cout<<"ERROR\n";
00147           S.add_basic_constraint(c, Dynamic);
00148           sucess=true;
00149       };
00150       if(!PQ.empty())
00151           cval=PQ.prio(PQ.find_min());
00152   };
00153 
00154   if(sucess) return constraint_found;
00155   cout<<"no cut by heuristic\n";
00156 */
00157 
00158   //cout<<"start\n";
00159 =======
00160 /*
00161   node_array<bool> R1(G, false);
00162   R1[u]=true;
00163   p_queue<NT, edge> PQ;
00164   edge_array<pq_item> EI(G, nil);
00165   NT cval=(int) VALONE, cs=0;
00166 
00167   forall_out_edges(e, u) {
00168       EI[e]=PQ.insert(-val[e], e);
00169       cs+=val[e];
00170   };
00171 
00172   bool sucess=false;
00173   edge e1;
00174   list<node> NL;
00175   NL.append(u);
00176   cout<<"start\n";
00177 
00178   while (!PQ.empty()) {
00179       while(!PQ.empty() && (-PQ.prio(PQ.find_min())>=cval)) {
00180           e1=PQ.inf(PQ.find_min());
00181           PQ.del_min();
00182           R1[target(e1)]=true;
00183           NL.append(target(e1));
00184           cs-=val[e1];
00185           forall_in_edges(e, target(e1)) if((e!=e1) && (R1[source(e)])) {
00186               cs-=val[e];
00187               if(EI[e]==nil) cout<<"EROR2\n";
00188               PQ.del_item(EI[e]);
00189           };
00190           forall_out_edges(e, target(e1)) if(!R1[target(e)]) {
00191               cs+=val[e];
00192               EI[e]=PQ.insert(-val[e],e);
00193           };
00194           if(target(e1)==v) {
00195               cs=2*(int) VALONE;
00196               PQ.clear();
00197           };
00198       };
00199 
00200       if(cs<VALONE*900/1000) {
00201           cout<<cs<<" Heuristic found cut\n";
00202           cout<<NL.size()<<" size\n";
00203           cons_obj* c=new path_cutset_inequality(G,VM,NL);
00204           if(!(c->violated(S))) cout<<"ERROR\n";
00205           S.add_basic_constraint(c, Dynamic);
00206           sucess=true;
00207       };
00208       if(!PQ.empty())
00209           cval=PQ.prio(PQ.find_min());
00210   };
00211 
00212   if(sucess) return constraint_found;
00213   cout<<"no cut by heuristic\n";
00214 */
00215 
00216 >>>>>>> 1.3
00217   // exact separation
00218   edge_array<NT> f(G);
00219   MAX_FLOW(G, u, v, val, f);
00220   node_array<bool> R(G, false);
00221   list<node> mc;
00222   dfs(G, u, val, f, R, mc);
00223   if(R[v]) cout<<"ERRROR\n";
00224   //mc.pop_front();
00225   //G.del_node(s);
00226   //cout<<"add\n";
00227   cons_obj* c=new path_cutset_inequality(G,VM,mc);
00228   //cout<<"constr\n";
00229   if (c->violated(S)) {
00230     //cout<<"jetzt\n";
00231     i++; S.add_basic_constraint(c, Dynamic); 
00232     //cout<<"end\n";
00233   } else delete c;
00234   //cout<<"edn\n";
00235   if(i>0) {
00236     return constraint_found;
00237   }
00238   //cout<<"No path constraint\n";
00239   return no_constraint_found;
00240 }
00241 
00242 PATH::status PATH::feasible(solution& S) {
00243   node_array<node> GtoH(G);
00244   graph H;
00245   node u1;
00246   forall_nodes(u1, G)
00247     GtoH[u1]=H.new_node();
00248   edge e;
00249   forall_edges(e,G) {
00250     if(S.value(VM(e))>0.5) H.new_edge(GtoH[source(e)], GtoH[target(e)]);
00251   }
00252   node_array<bool> R(H, false);
00253   dfs(H, GtoH[u], R);
00254   if (R[GtoH[v]]) return feasible_solution;
00255   return infeasible_solution;
00256 };
00257 
00258 #undef VALONE
00259 
00260 #endif

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