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

dnatag.cc

00001 #include<scil/scil.h>
00002 #include<boost/foreach.hpp>
00003 #include<boost/graph/iteration_macros.hpp>
00004 #include<boost/graph/adjacency_list.hpp>
00005 
00006 using namespace SCIL;
00007 using namespace boost;
00008 
00009 #define P 100
00010 #define T 50
00011 #define Pr 0.02
00012 
00013 typedef boost::adjacency_list<vecS,vecS,bidirectionalS> Graph;
00014 typedef boost::graph_traits<Graph>::vertex_descriptor vertex_descriptor;
00015 typedef boost::graph_traits<Graph>::edge_descriptor edge_descriptor;
00016 
00017 class PrimerVariable : public var_obj {
00018   cons* C;
00019   std::list<int> PV;
00020 
00021   public:
00022 
00023   PrimerVariable(bool* B_, cons* C_) : var_obj(1.,0.,1.,Vartype_Binary) {
00024     C=new cons[P];
00025     for(int i=0; i<P; i++) {
00026       C[i]=C_[i];
00027       if(B_[i]) PV.push_back(i);
00028     };
00029   };
00030 
00031   PrimerVariable(cons* C_) : var_obj(100.,0.,1.,Vartype_Binary) {
00032     C=new cons[P];
00033     for(int i=0; i<P; i++) {
00034       C[i]=C_[i];
00035       PV.push_back(i);
00036     };
00037   };
00038 
00039   ~PrimerVariable() {
00040     delete[] C;
00041   };
00042 
00043   void non_zero_entries(column& Co) {
00044     cout<<PV.size()<<" HIER\n";
00045     BOOST_FOREACH(int i, PV) {
00046       Co+=(column) C[i];
00047     }
00048   }
00049 
00050 };
00051 
00052 double Solve_Pricing(Graph& G, double* C, bool* B, vector<int> N) { /*node_array<int> N*/
00053   ILP_Problem ILP(Optsense_Min);
00054   
00055   //ILP.set_silent();
00056 
00057   var_map<vertex_descriptor> VN;
00058   row r;
00059   BGL_FORALL_VERTICES(u,G,Graph) {
00060     if(N[u]==-1) {
00061       VN[u]=ILP.add_binary_variable(0.);
00062       r+=VN[u];
00063     } else { 
00064       VN[u]=ILP.add_binary_variable((double)-C[N[u]]);
00065       r-=VN[u];
00066     }
00067   }
00068   ILP.add_basic_constraint(r==0);  
00069 
00070   var_map<edge_descriptor> VE;
00071   BGL_FORALL_EDGES(e,G,Graph) {
00072     VE[e]=ILP.add_variable(0.,0.,0.,Vartype_Integer);
00073     ILP.add_basic_constraint((row) VE[e]>=VN[source(e,G)]+VN[target(e,G)]-1);
00074   }
00075 
00076   BGL_FORALL_VERTICES(u,G,Graph) if(in_degree(u,G)+out_degree(u,G) != 0) {
00077     row r1;
00078     BGL_FORALL_INEDGES (u,e,G,Graph) r1+=(row) VE[e];
00079     BGL_FORALL_OUTEDGES(u,e,G,Graph) r1+=(row) VE[e];
00080     ILP.add_basic_constraint(r1<=1);
00081   }
00082 
00083   ILP.optimize();
00084 
00085   double D=0;
00086   BGL_FORALL_VERTICES(u, G, Graph) if(N[u]!=-1) {
00087     if(ILP.get_solution(VN[u])==1) {
00088       B[N[u]]=1;
00089       D+=C[N[u]];
00090     } else B[N[u]]=0;
00091   };
00092   cout<<D<<" SOLIUTION\n";
00093   return D;
00094 };
00095 
00096 class PrimerPricer : public sym_constraint {
00097   private:
00098     Graph& G;
00099     cons* C;
00100     vector<int>& PN;
00101     bool no_more_pricing;
00102 
00103   public:
00104 
00105   PrimerPricer(Graph& G_, cons* C_, vector<int>& PN_) : G(G_), C(C_), PN(PN_) {
00106     no_more_pricing=false;
00107   };
00108 
00109   status feasible(subproblem& s) {
00110     return feasible_solution;
00111   };
00112 
00113   status LP_pricing(subproblem& s) {
00114     if(no_more_pricing) return no_variable_found;
00115     cout<<"HIER\n";
00116     double Co[P];
00117     bool B[P];
00118     for(int i=0; i<P; i++) Co[i]=s.value(C[i])+0.001;
00119     double d=Solve_Pricing(G, Co, B, PN);
00120     //for(int i=0; i<P; i++) cout<<B[i]<<" "; cout<<endl;
00121     cout<<d<<"----------------- ADD AN VARIABLE"<<endl;
00122     if(d>1.05) { s.add_variable(new PrimerVariable(B, C));
00123       return variable_found;
00124     }
00125     no_more_pricing=true;
00126     return no_variable_found;
00127   };
00128 };
00129 
00130 int main() {
00131   // Make Input
00132 
00133   vertex_descriptor PN[P];
00134   vertex_descriptor TN[T];
00135 
00136   Graph G;
00137   srand ( time(NULL) );    /* initialize random number generator */ 
00138   double p;
00139 
00140 
00141   for(int i=0; i<P; i++) PN[i]=add_vertex(G);
00142 
00143   for(int j=0; j<T; j++) {
00144     TN[j]=add_vertex(G);
00145     for(int i=0; i<P; i++) {
00146       p = (double)rand() / RAND_MAX;
00147 
00148       if(p<=Pr) add_edge(PN[i],TN[j],G);
00149     }
00150   };
00151   
00152 
00153   // Optimize
00154 
00155   vector<int> PNN(num_vertices(G));
00156   for(int i=0; i<P; i++) PNN[PN[i]]=i;
00157   for(int j=0; j<T; j++) PNN[TN[j]]=-1;
00158 
00159   ILP_Problem ILP(Optsense_Min); //, true);
00160 
00161   cons CS[P];
00162   for(int i=0; i<P; i++) CS[i]=ILP.add_basic_constraint(Greater,1);
00163 
00164   ILP.add_variable(new PrimerVariable(CS));
00165 
00166   ILP.add_sym_constraint(new PrimerPricer(G, CS, PNN));
00167     
00168   ILP.optimize();
00169 
00170   cout<<"OPTIMIZED\n";
00171 
00172   return 0;
00173 
00174 }
Generated on Mon Mar 28 22:03:48 2011 for SCIL by  doxygen 1.6.3