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

ilp_problem.cc

00001 #include<scil/scil.h>
00002 
00003 #include<scip/objscip.h>
00004 extern"C" {
00005 #include<scip/scipdefplugins.h>
00006 #include<scip/cons_linear.h>
00007 }
00008 
00009 #include"scip_constraint_handler.h"
00010 #include<LEDA/list.h>
00011 
00012 using namespace scip;
00013 
00014 namespace SCIL {
00015 
00016   ILP_Problem::ILP_Problem(Optsense sense) {
00017     DATA=new ILP_Problem_Data;
00018 
00019     CHECK_ABORT( SCIPcreate(&((ILP_Problem_Data*) DATA)->scipp) );
00020     ((ILP_Problem_Data*) DATA)->MYSub=
00021       new subproblem(this);
00022     ((ILP_Problem_Data*) DATA)->sconshdlr=
00023       new SCIPConsHandler(((ILP_Problem_Data*) DATA)->MYSub);
00024 
00025     CHECK_ABORT( SCIPincludeDefaultPlugins(((ILP_Problem_Data*) DATA)->scipp) );
00026 
00027     CHECK_ABORT( SCIPincludeObjConshdlr(((ILP_Problem_Data*) DATA)->scipp, ((ILP_Problem_Data*) DATA)->sconshdlr, true) );
00028     ((ILP_Problem_Data*) DATA)->conshdlr=
00029       SCIPfindConshdlr(((ILP_Problem_Data*) DATA)->scipp, "CH");
00030     assert(conshdlr!=NULL);
00031     CHECK_ABORT( SCIPcreateProb(((ILP_Problem_Data*) DATA)->scipp, "bla", NULL, NULL, NULL, NULL, NULL, (PROBDATA*) this) );
00032   };
00033 
00034   ILP_Problem::~ILP_Problem() {
00035     /*
00036       CHECK_ABORT( SCIPfreeSolve(((ILP_Problem_Data*) DATA)->scipp) );
00037 
00038       var v;
00039       LEDA::list<var> L;
00040       forall_variables(v, *((ILP_Problem_Data*) DATA)->MYSub) L.append(v);
00041       forall(v, L) {
00042       SCIPreleaseVar(((ILP_Problem_Data*) DATA)->scipp, &v.var_pointer()->varP); 
00043       delete v.var_pointer();  
00044       };
00045     */
00046 
00047     cons c;
00048     forall(c, ((ILP_Problem_Data*) DATA)->CL) {
00049       delete c.cons_pointer();
00050     };
00051 
00052     CHECK_ABORT( SCIPfree(&((ILP_Problem_Data*) DATA)->scipp) );
00053 
00054     //delete ((ILP_Problem_Data*) DATA)->sconshdlr;
00055     delete ((ILP_Problem_Data*) DATA)->MYSub;
00056     delete (ILP_Problem_Data*) DATA;
00057   }
00058 
00059   var ILP_Problem::add_variable(double obj, double lBound, double uBound, Vartype t, Activation a) {
00060     return ((ILP_Problem_Data*) DATA)->MYSub->add_variable(obj, lBound, uBound, t, a);
00061   };
00062 
00063   var ILP_Problem::add_variable(var_obj* v, Activation a) {
00064     return ((ILP_Problem_Data*) DATA)->MYSub->add_variable(v, a);
00065   };
00066 
00067   cons ILP_Problem::add_basic_constraint(cons_sense s, double rhs, Activation a) {
00068     cons_obj* co=new cons_obj(s, rhs);
00069     return add_basic_constraint(co, a);
00070   };
00071 
00072 
00073   cons ILP_Problem::add_basic_constraint(cons_obj* co, Activation a) {
00074     //CHECK_ABORT( SCIPcreateEmptyRow(scipp, &co->rowp, "c", co->sense() == Less ? -SCIPinfinity(scipp) : co->rhs(), co->sense() == Greater ? SCIPinfinity(scipp) : co->rhs(), true, false /* true for pricing */, a==Static ? false : true ));
00075     row r;
00076     co->non_zero_entries(r);
00077     row_entry re;
00078     double CS[r.size()];
00079     VAR* VS[r.size()];
00080     int i=0;
00081     forall(re, r) { // TODO kein NULLPOINTER!
00082       if(re.Var.var_pointer()!=NULL) {
00083         VS[i]=((Variable_Data*) re.Var.var_pointer()->DATA)->varP;
00084         CS[i]=re.Coeff;
00085         i++;
00086       };
00087     };
00088     CONS* C; //TODO
00089     CHECK_ABORT( 
00090       SCIPcreateConsLinear(
00091         ((ILP_Problem_Data*) DATA)->scipp, &C, "C", r.size(), VS, CS, 
00092         co->sense() == Less ? -SCIPinfinity(((ILP_Problem_Data*) DATA)->scipp) : co->rhs(), 
00093         co->sense() == Greater ? SCIPinfinity(((ILP_Problem_Data*) DATA)->scipp) : co->rhs(),
00094          TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE) );
00095     CHECK_ABORT( SCIPaddCons(((ILP_Problem_Data*) DATA)->scipp, C) );
00096     CHECK_ABORT( SCIPreleaseCons(((ILP_Problem_Data*) DATA)->scipp, &C) );
00097     ((Basic_Constraint_Data*) co->DATA)->scipp=
00098       ((ILP_Problem_Data*) DATA)->scipp;
00099     ((ILP_Problem_Data*) DATA)->CL.append(co);
00100     cons c(co);
00101     return c;
00102   };
00103 
00104 
00105   void ILP_Problem::add_sym_constraint(sym_constraint* c) {
00106     ((ILP_Problem_Data*) DATA)->MYSub->add_sym_constraint(c);
00107   };
00108 
00109   void ILP_Problem::save_solution(solution& s) { 
00110     ((ILP_Problem_Data*) DATA)->MYSub->save_solution(s);
00111   };
00112 
00113   double ILP_Problem::get_solution (var v) {
00114     return ((ILP_Problem_Data*) DATA)->MYSub->get_solution(v);
00115   };
00116 
00117   double ILP_Problem::get_solution (row& r) {
00118     return ((ILP_Problem_Data*) DATA)->MYSub->get_solution(r);
00119   };
00120   
00121   void ILP_Problem::optimize() {
00122     CHECK_ABORT( SCIPsolve(((ILP_Problem_Data*) DATA)->scipp) );
00123   };
00124 
00125   double cons_obj::slack(subproblem& s) {
00126     row r;
00127     non_zero_entries(r);
00128     row_entry re;
00129     double d=rhs();
00130     forall(re,r) {
00131       d-=re.Coeff*s.value(re.Var);
00132     };
00133     if(sense()==Equal) return fabs(d);
00134     if(sense()==Less) return -d;
00135     return d;
00136   };
00137 
00138 bool var::operator== (const var& v) const {
00139   return compare(*this, v)==0;
00140 }
00141 
00142 row var::operator+ (const row& r) const {
00143   return row(*this)+r;
00144 }
00145 
00146 row var::operator- (const row& r) const {
00147   return row(*this)-r;
00148 }
00149 
00150 row var::operator* (double d) const {
00151   return row(*this)*d;
00152 }
00153 
00154 bool var::operator!= (const var& v) const {
00155   return compare(*this,v)!=0;
00156 }
00157 
00158 bool var::operator< (const var& v) const {
00159   return compare(*this,v)<0;
00160 }
00161 bool var::operator> (const var& v) const {
00162   return compare(*this,v)>0;
00163 }
00164 
00165 bool var::operator<= (const var& v) const {
00166   return compare(*this,v)<=0;
00167 }
00168 
00169 bool var::operator>= (const var& v) const {
00170   return compare(*this,v)>=0;
00171 }
00172 
00173 int SCIL::compare(const var& t1, const var& t2) {
00174   if((void*) t1.var_pointer()>(void*) t2.var_pointer()) return 1;
00175   if((void*) t1.var_pointer()<(void*) t2.var_pointer()) return -1;
00176   return 0;
00177 }
00178 
00179 row SCIL::operator* (double d, var v) {
00180   return row(v)*d;
00181 }
00182 
00183 } // END NAMESPACE
00184 
00185 int SCIL::Hash(var v) { 
00186   return LEDA::Hash(v.var_pointer()); 
00187 }
00188 
00189 int SCIL::ID_Number(const var & v) {
00190   return LEDA::ID_Number(v.var_pointer());
00191 }
00192 
00193 

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