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

dnatag.c

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

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