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

smknap.cc

00001 #include <iostream>
00002 #include <cmath>
00003 #include<scil/scil.h>
00004 #include<scil/constraints/submodular.h>
00005 
00006 using namespace std;
00007 using namespace SCIL;
00008 
00009 class mysub : public submodular {
00010    public:
00011       std::map<var, double>& co;
00012 
00013       mysub(var& v_, var_map<int>& VM_, std::map<var, double>& co_, int n_) 
00014          : submodular(v_, VM_, n_), co(co_)
00015       {
00016       }
00017 
00018    private:
00019       double value(solution& S) {
00020          double w = 0;
00021          for( int i = 0; i < n; i++ )
00022             w += S.value(VM[i]) * co[VM[i]];
00023          return sqrt(w);
00024       }
00025 };
00026 
00027 
00028 int main()
00029 {
00030    int n;
00031    double b, k;
00032    double lb = 0.;
00033    double ub = 0.;
00034    cout << "Type n: ";
00035    cin >> n;
00036    double* s = new double[n];
00037    double* t = new double[n];
00038    double* bs = new double[n];
00039    //compute initial lower and upper bound on k
00040    for( int i = 0; i < n; i++ ) {
00041       cin >> s[i];
00042       if( s[i] < 0. )
00043          lb += s[i];
00044       else
00045          ub += s[i];
00046    }
00047    for( int i = 0; i < n; i++ )
00048       cin >> t[i];
00049    cin >> b;
00050    
00051    int no = 0;
00052    int ns = 0;
00053    int nl = 0;
00054    while( ub-lb > 1 ) {
00055       no++;
00056       ILP_Problem IP(Optsense_Min);
00057       IP.primal_bound(b + .1);
00058       IP.set_silent();
00059       var_map<int> VM;
00060       std::map<var, double> co;
00061       row r;
00062       var v = IP.add_variable(1., -1e4, 1e25, Vartype_Float);
00063       for( int i = 0; i < n; i++ ) {
00064          VM[i] = IP.add_binary_variable(0.);
00065          r += VM[i] * s[i];
00066       }
00067       for( int i = 0; i < n; i++ ) {
00068          co[VM[i]] = t[i];
00069       }
00070       IP.add_sym_constraint(new mysub(v, VM, co, n));
00071       k = lb + (ub - lb)/2.;
00072       IP.add_basic_constraint(r>=k, Static);
00073 
00074       IP.optimize();
00075       solution sol = IP.get_solution();
00076       cout << "\n#" << no << "  lb: " << lb << "  ub: " << ub << "  k: " << k << endl;
00077       cout << "feasible: " << IP.feasibleFound() << "  status: " << IP.status();
00078       cout << "  opt value: " << IP.get_optimum() << endl;
00079       for( int i = 0; i < n; i++ )
00080          cout << sol.value(VM[i]) << "  ";
00081       cout << endl;
00082       ns += IP.nSub();
00083       nl += IP.nLp();
00084 
00085       if( IP.feasibleFound() && IP.status() == 0 && IP.get_optimum() <= b ) {
00086          lb = k;
00087          for( int i = 0; i < n; i++ )
00088             bs[i] = sol.value(VM[i]);
00089       }
00090       else
00091          ub = k;
00092 
00093    }
00094 
00095    double val = 0.;
00096    cout << endl << "Best solution: ";
00097    for( int i = 0; i < n; i++ ) {
00098       cout << bs[i] << " ";
00099       val += bs[i] * s[i];
00100    }
00101    cout << endl << "value: " << val << endl;
00102    cout << "#solved IPs: " << no << "   #subs: " << ns << "   #LPs: " << nl << endl;
00103 
00104    delete[]s;
00105    delete[]t;
00106    delete[]bs;
00107    return 0;
00108 }
Generated on Mon Mar 28 22:03:49 2011 for SCIL by  doxygen 1.6.3