00001 #include<LEDA/map.h>
00002 #include<LEDA/list.h>
00003 #include<scil/global.h>
00004 #include<scil/var_obj.h>
00005 #include<scil/cons_obj.h>
00006 #include<scil/variable.h>
00007 #include<scil/subproblem.h>
00008 #include<scil/ilp_problem.h>
00009 #include<scil/row.h>
00010 #include<scil/sym_constraint.h>
00011
00012 #ifdef OSL
00013 #include<ekk_c_api.h>
00014 #endif
00015 #ifdef CPLEX
00016 #include<cplex.h>
00017 #endif
00018
00019 const
00020 string& ILP_Problem::configuration(const string& s) const {
00021 return ConfMap[s];
00022 };
00023
00024 string& ILP_Problem::configuration(const string& s) {
00025 return ConfMap[s];
00026 };
00027
00028 bool ILP_Problem::optimization_started() {
00029 return opt_started;
00030 };
00031
00032 void ILP_Problem::read_configuration_file(string s) {
00033 ifstream ifs(s);
00034
00035 string s1, s2;
00036 while(!ifs.eof()) {
00037 if(ifs.peek()=='#') s2.read_line(ifs); else {
00038 ifs >> s1;
00039 ifs >> s2;
00040 configuration(s1)=s2;
00041 }
00042 }
00043 };
00044
00046
00052 #ifdef OSL
00053 ILP_Problem* global;
00054 int test=0;
00055
00056
00057
00058
00059 int OSLLINKAGE_CB evnucallback(EKKModel* model) {
00060
00061 global->SP->fixvar=-1;
00062
00063 ekk_dualSimplex(model);
00064
00065 for(int i=0; i<global->number_of_variables(); i++) {
00066 if(ekk_columnStatus(model,i)==0) global->SP->fixvar=i;
00067 };
00068
00069 global->SP->mipModel=model;
00070
00071 bool s=global->SP->feasible();
00072 if(s==false) {
00073
00074 double * c=ekk_getColsol(model);
00075 global->SP->fixvalue=c[global->SP->fixvar];
00076 c[global->SP->fixvar]=0.9;
00077
00078 ekk_setColsol(model,c);
00079
00080
00081 return 0;
00082 }
00083
00084 cout<<"FEASIBLE SOLUTION\n";
00085
00086 return 0;
00087 };
00088
00089
00090 int lpcallback(EKKModel * model, int reason, EKKIntegerPresolve *,
00091 EKKCuts * cuts, int depth) {
00092 test++;
00093 if(test==1) {
00094 for(int i=0; i<global->number_of_variables(); i++) {
00095 if(ekk_columnStatus(model,i)==0) global->SP->fixvar=i;
00096 };
00097 double * c=ekk_getColsol(model);
00098 global->SP->fixvalue=c[global->SP->fixvar];
00099
00100 c[global->SP->fixvar]=0.9;
00101
00102 return 0;
00103 }
00104
00105 cuts->numberCuts=0;
00106 cuts->maxCuts=100000;
00107 cuts->cut=new EKKCut[100000];
00108
00109 if(reason!=0) return 0;
00110 global->iter++;
00111 global->SP->mipModel=model;
00112 global->SP->Cuts=cuts;
00113
00114 global->SP->separate();
00115
00116 return 0;
00117 }
00118
00119 void test2(EKKModel * model, int reason, int * iarray,
00120 double * rarray, int * muser , int nuser) {
00121 cout<<"HIER\n";
00122 };
00123
00124 void test3 (EKKModel * model, int * xadj,
00125 int * adjncy , int * perm , int * invp,
00126 int * mwork, int nwords, int nrow) {
00127 cout<<"ordu\n";
00128 };
00129
00130 void test1(EKKModel * model, int reason,
00131 double * objective_value , int * status) {
00132 cout<<reason<<" reason\n";
00133
00134 global->SP->fixvar=1;
00135
00136
00137
00138
00139
00140
00141
00142 global->SP->mipModel=model;
00143
00144 cout<<global->SP->fixvar<<"modifiing\n";
00145
00146 bool s=global->SP->feasible();
00147 if(s==false) {
00148 double * c=ekk_getColsol(model);
00149 global->SP->fixvalue=c[global->SP->fixvar];
00150 c[global->SP->fixvar]=0.9;
00151
00152 ekk_setColsol(model,c);
00153
00154 };
00155
00156 cout<<"treturning\n";
00157 };
00158 #endif
00159
00160 #ifdef CPLEX
00161
00162 mycutcallback (CPXENVptr env,
00163 void *cbdata,
00164 int wherefrom,
00165 void *cbhandle,
00166 int *useraction_p) {
00167 {
00168
00169 ILP_Problem * ILP;
00170 ILP = static_cast <ILP_Problem *> (cbhandle);
00171
00172
00173 int status = CPXgetcallbacknodex(env, cbdata, wherefrom, x, 0, NUMCOLS-1);
00174
00175 };
00176 #endif
00177
00178 ILP_Problem::ILP_Problem (Optsense optSense) {
00179 os = optSense;
00180
00181 iter=0;
00182 global=this;
00183 opt_started=false;
00184
00185 nvars=1;
00186 nconss=1;
00187
00188 vararray.resize(0, 1000);
00189
00190 SP=new subproblem(this);
00191
00192 #ifdef OSL
00193 env = ekk_initializeContext();
00194 mipModel=ekk_newModel(env,NULL);
00195
00196 int rtcod = ekk_setIlinelen(mipModel,100);
00197
00198
00199 rtcod = ekk_setIprtinfomask(mipModel,63);
00200
00201
00202 rtcod = ekk_setIsolmask(mipModel,6);
00203
00204
00205 rtcod = ekk_setIiterufreq(mipModel,9999999);
00206
00207
00208
00209
00210
00211 if(os==Optsense_Max)
00212 rtcod = ekk_setRmaxmin(mipModel, -1.000000);
00213 else
00214 rtcod = ekk_setRmaxmin(mipModel, 1.000000);
00215
00216
00217
00218
00219
00220
00221
00222
00223 ekk_messagePrintOff(mipModel,317);
00224 ekk_messagePrintOff(mipModel,324);
00225 ekk_messagePrintOff(mipModel,321);
00226 ekk_messagePrintOff(mipModel,323);
00227 ekk_messagePrintOff(mipModel,114);
00228
00229 ekk_resizeModel(mipModel, 1, 1, 0);
00230
00231 ekk_setPresolveOff(mipModel);
00232
00233 ekk_registerCutuCallBack(mipModel, lpcallback);
00234 ekk_registerEvnuCallBack(mipModel, evnucallback);
00235
00236
00237
00238 #endif
00239
00240 #ifdef CPLEX
00241 env = CPXopenCPLEX (&status);
00242
00243 if ( env == NULL )
00244 {
00245 char errmsg[1024];
00246 fprintf (stderr, "Could not open CPLEX environment.\n");
00247 CPXgeterrorstring (env, status, errmsg);
00248 fprintf (stderr, "%s", errmsg);
00249
00250 throw CplexError();
00251 }
00252
00253 status = CPXsetintparam (env, CPX_PARAM_SCRIND, CPX_ON);
00254 if ( status ) {
00255 fprintf (stderr,
00256 "Failure to turn on screen indicator, error %d.\n", status);
00257
00258 throw CplexError();
00259 }
00260 #endif
00261 };
00262
00263
00265
00267 ILP_Problem::~ILP_Problem () {
00268 };
00269
00270 var ILP_Problem::add_variable (double obj, double lBound, double uBound,
00271 Vartype t, Activation a = Static)
00272 {
00273 var_obj* v=new var_obj(obj, lBound, uBound, t);
00274 return add_variable(v);
00275 };
00276
00277
00278 var ILP_Problem::add_variable (var_obj * v, Activation a = Static)
00279 {
00280 v->set_index(nvars);
00281 nvars++;
00282
00283 ekk_addOneColumn(mipModel, v->obj(), v->lower_bound(), v->upper_bound(),
00284 v->non_zeros, v->columns, v->coeffs );
00285
00286 if(v->type()==Vartype_Integer)
00287 ekk_markAsInteger( mipModel , nvars-1 );
00288
00289 if(nvars==vararray.high()) vararray.resize(0, 2*vararray.high());
00290 vararray[nvars-1]=v;
00291
00292 return v;
00293 };
00294
00295
00296
00297
00298
00299
00300
00301
00302 cons
00303 ILP_Problem::add_basic_constraint (cons_obj * c, Activation a = Static)
00304 {
00305 nconss++;
00306
00307 c->init();
00308 ekk_addOneRow(mipModel , c->lower_bound() , c->upper_bound() ,
00309 c->non_zeros , c->rows , c->coeffs );
00310 conslist.append(c);
00311 return c;
00312 };
00313
00314
00315 cons ILP_Problem::add_basic_constraint(cons_sense s, double r, Activation a=Static) {
00316
00317
00318
00319 cons_obj* I=new cons_obj(s, r);
00320 return add_basic_constraint(I, a);
00321 }
00322
00323 void ILP_Problem::add_sym_constraint(sym_constraint* c) {
00324
00325 sym_constraints.append(c);
00326 c->init(*SP);
00327 }
00328
00329 void ILP_Problem::set_coefficient(var v, cons i, double d) {
00330
00331
00332 cout<<"TODO\n";
00333 };
00334
00335 void ILP_Problem::set_primal_heuristic(primal_heuristic* P) {
00336
00337 primal_heur=P;
00338 };
00339
00340 void ILP_Problem::optimize() {
00341
00342
00343
00344 Cuts=new EKKCuts;
00345
00346 Cuts->numberCuts=0;
00347 Cuts->maxCuts=100000;
00348 Cuts->cut=new EKKCut[100000];
00349
00350 Cuts->cut->lowerBound=0;
00351 Cuts->cut->upperBound=0;
00352 Cuts->cut->numberElements=0;
00353 Cuts->cut->variable=new int[1];
00354 Cuts->cut->variable[0]=0;
00355 Cuts->cut->coefficient=new double[1];
00356 Cuts->cut->coefficient[0]=0;
00357
00358 opt_started=true;
00359
00360 ekk_branchAndCut(mipModel, NULL, NULL, NULL, Cuts, 100000, 0);
00361
00362 const double* d=ekk_colsol(mipModel);
00363 for(int i=0; i<nvars; i++) {
00364 if(d[i]>0.001) Sol.set_value(vararray[i], d[i]);
00365 };
00366 };
00367
00368
00369 double ILP_Problem::get_solution(var v) {
00370
00371 if(Sol.value(v)>0.5) return 1;
00372 return 0;
00373
00374 }
00375
00376 double ILP_Problem::get_solution(row& r) {
00377 row_entry re;
00378 double d=0;
00379 forall(re, r) d+=re.coeff*get_solution(re.Var);
00380 return d;
00381 };