00001 #ifndef SCIL_MAX_CC
00002 #define SCIL_MAX_CC
00003
00004 #include<scil/scil.h>
00005 #include <scil/variable.h>
00006 #include<scil/constraints/max.h>
00007 #include <map>
00008 #include <list>
00009 #include <queue>
00010
00011 #include<iostream>
00012 #include<cstdarg>
00013
00014 #define LEPS 1e-4
00015
00016
00017
00018
00019 using namespace SCIL;
00020 using std::cout;
00021 using std::cerr;
00022 using std::endl;
00023 using std::pair;
00024
00025
00026
00027 struct paircomp {
00028 bool operator() (const pair<double,var>& p1, const pair<double,var>& p2) const
00029 {
00030 return p1.first > p2.first;
00031 }
00032 };
00033
00034 namespace SCIL {
00035 MAX::MAX(var v_, var_map<int>& VM_, std::map<var, double>& CF_, int n_, bool geq_)
00036 : v(v_), VM(VM_), CF(CF_), n(n_), geq(geq_){
00037 priority_queue< std::pair<double,var>, vector<std::pair<double,var> >, paircomp> QQ;
00038
00039 for( int i = 0; i < n; i++ ) {
00040 QQ.push(pair<double,var>(CF[VM[i]], VM[i]));
00041 }
00042
00043 for( int i = 0; i < n; i++ ) {
00044 VM[i] = QQ.top().second;
00045 QQ.pop();
00046 }
00047
00048 mc = CF[VM[n - 1]];
00049
00050 };
00051 }
00052
00053
00054 void MAX::init(subproblem& S) {
00055 if(S.configuration("MAX_Debug_Out")=="true")
00056 cerr << "MAX::init\n";
00057
00058 #ifndef NO_MAX_SEPARATION
00059
00060 for( int i = 0; i < n - 1; i++ ) {
00061 row r;
00062 r += VM[i] * CF[VM[i]];
00063 r += VM[n-1] * (CF[VM[n-1]] - CF[VM[i]]);
00064 r -= v;
00065 S.add_basic_constraint(r<=0., Static);
00066 }
00067 row r;
00068 r += VM[n-1] * CF[VM[n-1]];
00069 r -= v;
00070 S.add_basic_constraint(r<=0., Static);
00071 return;
00072 #else
00073 #if 0
00074 for( int i = 0; i < n; i++ ) {
00075 row r;
00076 r += VM[i] * CF[VM[i]];
00077 r -= v;
00078 S.add_basic_constraint(r<=0., Static);
00079 }
00080 #endif
00081 return;
00082 #endif
00083 };
00084
00085
00086 MAX::status MAX::feasible(solution& S) {
00087 if(S.originating_subproblem()->configuration("MAX_Debug_Out")=="true")
00088 cerr << "MAX::feasible\n";
00089
00090
00091
00092
00093 double m = CF[VM[0]] * S.value(VM[0]);
00094 double t;
00095 for( int i = 1; i < n; i++ ) {
00096 t = CF[VM[i]] * S.value(VM[i]);
00097 if( t >= m )
00098 m = t;
00099 }
00100
00101 bool feas = false;
00102
00103 if( geq && (m < S.value(v) + LEPS) ) {
00104 feas = true;
00105 }
00106 else if( !geq && (fabs(m - S.value(v)) < LEPS) )
00107 feas = true;
00108
00109
00110 return feas ? feasible_solution : infeasible_solution;
00111 }
00112
00113 MAX::status MAX::standard_separation(subproblem& S) {
00114 if(S.configuration("MAX_Debug_Out")=="true")
00115 cerr << "MAX::standard_separation\n";
00116
00117 #ifdef NO_MAX_SEPARATION
00118 return no_constraint_found;
00119 #endif//NO_MAX_SEPARATION
00120
00121 std::list< pair<double, int> > lQ;
00122 for( int i = 0; i < n; i++ ) {
00123 lQ.push_back( pair<double, int> (S.value(VM[i]), i));
00124 }
00125
00126 lQ.sort();
00127
00128 int st = -1;
00129 double s = 0.;
00130 double m = 0.;
00131 double l = 0.;
00132 row r;
00133 do {
00134 if( (lQ.back()).second < st ) {
00135 lQ.pop_back();
00136 continue;
00137 }
00138 st = lQ.back().second;
00139 lQ.pop_back();
00140
00141
00142 s = CF[VM[st]] - m;
00143
00144
00145
00146
00147
00148
00149 r += VM[st] * s;
00150 l += S.value(VM[st]) * s;
00151 m += s;
00152 } while( !lQ.empty() );
00153
00154
00155
00156 if( l > S.value(v) + LEPS ) {
00157 r -= v;
00158 S.add_basic_constraint(r <= 0.);
00159 if(S.configuration("MAX_Debug_Out")=="true")
00160 cerr << "MAX::standard_separation: constraint added\n";
00161 return constraint_found;
00162 }
00163 return no_constraint_found;
00164 }
00165
00166 void MAX::info() {
00167 cout<<"Configurations:\n";
00168 cout<<"MAX_Debug_Out [true|false]\n";
00169 }
00170 #endif