problem.h

Go to the documentation of this file.
00001 // Copyright (C) 2006 Ivo Nowak and Stefan Vigerske
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // Author: Ivo Nowak, Stefan Vigerske
00006 
00007 #ifndef PROBLEM_H
00008 #define PROBLEM_H
00009 
00010 #include "standard.h"
00011 #include "usermatrix.h"
00012 #include "func.h"
00013 
00035 class MinlpProblem {
00042    friend ostream& operator << (ostream &out, const MinlpProblem& a) {
00043      a.print(out); return out;
00044    };
00045 
00046    double compute_scale(char* option, int c, double eps, UserVector<double>& x);
00047 
00048    protected:
00051       int dim_;
00052 
00053    public:
00057       Pointer<ostream> out_problem_p;
00061       Pointer<ostream> out_problem_log_p;
00067 #define out_problem if (out_problem_p) (*out_problem_p)
00068 
00073 #define out_problem_log if (out_problem_log_p) (*out_problem_log_p)
00074 
00077                         typedef enum { QQP, MINLP } ptype;
00078 
00082       ptype problem_type;
00083 
00087       Pointer<char> prob_name;
00088 
00092       vector<ivector> block;
00093 
00097       vector<Pointer<char> > var_names;
00098 
00099       // definition of the optimization problem
00103       Pointer<SepQcFunc> obj;
00104 
00108       vector<Pointer<SepQcFunc> > con;
00109 
00113       vector<Pointer<char> > con_names;
00114 
00118       vector<bool> con_eq;
00119 
00123       vector<int> i_discr;
00124 
00128       vector<int> i_cont;
00129 
00133       vector<bool> discr;
00134 
00138       dvector lower;
00139 
00143       dvector upper;
00144 
00147       dvector primal_point;
00148 
00159       MinlpProblem(ptype p_type=MINLP, char* prob_name_=(char*)NULL, Pointer<ostream> out_problem_p_=out_out_p, Pointer<ostream> out_problem_log_p_=out_log_p)
00160       : problem_type(p_type), prob_name(prob_name_ ? strdup(prob_name_) : NULL), dim_(0), out_problem_p(out_problem_p_), out_problem_log_p(out_problem_log_p_)
00161       { };
00162 
00172       MinlpProblem(Pointer<SepQcFunc> obj_, const dvector &lower_, const dvector &upper_, bool discr_=false, Pointer<ostream> out_problem_p_=out_out_p, Pointer<ostream> out_problem_log_p_=out_log_p)
00173       : dim_(obj_->dim()), prob_name(0), problem_type(MINLP), out_problem_p(out_problem_p_), out_problem_log_p(out_problem_log_p_), block(obj_->block), discr(obj_->dim(), discr_), lower(lower_), upper(upper_), var_names(obj_->dim()), primal_point(obj_->dim())
00174       { if (discr_) for (int i=0; i<dim(); i++) i_discr.push_back(i);
00175                                 else for (int i=0; i<dim(); i++) i_cont.push_back(i);
00176         add_obj(obj_);
00177       }
00178 
00182       MinlpProblem(const MinlpProblem &p)
00183       : dim_(p.dim()), block(p.block), var_names(p.var_names),
00184         obj(p.obj), con(p.con), con_names(p.con_names), con_eq(p.con_eq),
00185         i_discr(p.i_discr),i_cont(p.i_cont), discr(p.discr),
00186         lower(p.lower), upper(p.upper), primal_point(p.primal_point),
00187         problem_type(p.problem_type), prob_name(p.prob_name),
00188         out_problem_p(p.out_problem_p), out_problem_log_p(p.out_problem_log_p)
00189       { }
00190 
00196                         MinlpProblem(const MinlpProblem& p, int k);
00197 
00200       virtual ~MinlpProblem() { }
00201 
00205       int dim() const { return dim_; };
00206 
00228       virtual void add_var(int i_, int bnum, bool discr_, double lower_, double upper_, const char* name=NULL);
00229 
00239       virtual void add_con(Pointer<SepQcFunc> f, bool eq=true, const char* name=NULL);
00240 
00245       virtual void del_con(int connr);
00246 
00252       virtual void add_obj(Pointer<SepQcFunc> f) { obj=f; };
00253 
00258       void taylor_approx(UserVector<double>& point, const int degree=2);
00259 
00265       int feasible(const UserVector<double>& x, double tol, ostream* out=NULL);
00266       
00274       void print_most_violated_constraints(const UserVector<double>& x, ostream& out, int nr=5, double tol=1E-4);
00275 
00281       int scale(Pointer<Param> param=NULL) { return scale(primal_point, param); }
00282 
00288       int scale(UserVector<double>& x, Pointer<Param> param=NULL);
00289 
00290                         void get_sparsity(vector<Pointer<SparsityInfo> >& si) const;
00291                         Pointer<SparsityInfo> get_sparsity(int k) const;
00292 
00316       virtual void print(ostream& out) const;
00317 
00320                         void print_as_gams(ostream& out) const;
00321 };
00322 
00323 
00327 class MinlpPenaltyFunc: public Func {
00328   private:
00331     Pointer<MinlpProblem> minlp;
00332 
00333   public:
00336     dvector delta;
00337 
00343     MinlpPenaltyFunc(Pointer<MinlpProblem> minlp_, Pointer<Param> penalty_param=NULL)
00344     : Func(minlp_->dim()), minlp(minlp_), delta(minlp_->con.size(), 1000.)
00345     { if (penalty_param) {
00346                 penalty_param->read();
00347               for (int i=0; i<minlp->con.size(); i++)
00348               delta[i]=penalty_param->get_d(minlp->con_names[i], 1000.);
00349       }
00350     }
00351 
00358     double eval(const UserVector<double>& x) const;
00359                 using Func::eval;
00360 
00370     int valgrad(double& val, UserVector<double>& y, const UserVector<double>& x) const;
00371                 using Func::valgrad;
00372 
00379     void grad(UserVector<double>& y, const UserVector<double>& x) const;
00380                 using Func::grad;
00381 
00387     void HessMult(UserVector<double>& y, const UserVector<double>& z, const UserVector<double>& x) const;
00388                 using Func::HessMult;
00389 
00390                 virtual void set_curvature(CurvatureType ct) { out_err << "MinlpPenaltyFunc::set_curvature() not implemented. Aborting." << endl; exit (-1); };
00391                 virtual CurvatureType get_curvature() const  { out_err << "MinlpPenaltyFunc::get_curvature() not implemented. Aborting." << endl; exit (-1); return Func::UNKNOWN; };
00392 
00398     void print(ostream& out) const {
00399       out << "MinlpPenaltyFunc: dim=" << dim() << endl;
00400       out << "delta-vector: " << delta;
00401     }
00402 
00403 };
00404 
00405 
00410 class MipProblem {
00411         public:
00412                 typedef enum { CONTINUOUS, BINARY, INTEGER } VarType;
00413 
00414                 friend ostream& operator<<(ostream& out, const VarType& type) {
00415                         switch (type) {
00416                                 case CONTINUOUS: out << "Continuous"; break;
00417                                 case BINARY: out << "Binary"; break;
00418                                 case INTEGER: out << "Integer"; break;
00419                                 default: out << "Unknown !!";
00420                         }
00421                         return out;
00422                 }
00423                 friend ostream& operator<<(ostream& out, const MipProblem& mip);
00424 
00425         private:
00426                 int nr_col, nr_row;
00427 
00428         // Variables
00429                 dvector col_lower, col_upper;
00430                 vector<Pointer<char> > col_names;
00431                 vector<VarType> col_types;
00432 
00433         // Objective function
00434                 Pointer<UserVector<double> > obj;
00435                 double obj_const;
00436 
00437         // Constraints
00438                 SparseMatrix A;
00439                 dvector row_lower, row_upper;
00440                 vector<Pointer<char> > row_names;
00441 
00442         public:
00450                 MipProblem(int nr_col=0, int nr_row=0);
00451 
00457                 MipProblem(const dvector& lower, const dvector& upper, const vector<int>& i_discr=vector<int>(0), const vector<Pointer<char> >& names=vector<Pointer<char> >(0));
00458 
00461                 MipProblem(const MinlpProblem& minlp);
00462 
00465                 void reset();
00466 
00467                 int dim() const { return nr_col; }
00468                 int rows() const { return nr_row; }
00469 
00470                 const SparseMatrix& getMatrix() const { return A; }
00471                 const dvector& getRowLower() const { return row_lower; }
00472                 const dvector& getRowUpper() const { return row_upper; }
00473                 const dvector& getColLower() const { return col_lower; }
00474                 const dvector& getColUpper() const { return col_upper; }
00475                 const VarType& getColType(int i) const { assert(i<dim() && i>=0); return col_types[i]; }
00476                 const UserVector<double>& getObj() const { assert(obj); return *obj; }
00477                 double getObjConst() const { return obj_const; }
00478 
00479                 void setColBounds(int index, double low, double up);
00480                 void setColType(int index, VarType type);
00481                 void setColName(int index, Pointer<char> name);
00482                 void setCol(int index, double low, double up, VarType type=CONTINUOUS, Pointer<char> name=NULL);
00483 
00484                 void setObj(Pointer<UserVector<double> > obj_, double obj_const_=0.);
00485 
00486                 void setRow(int index, const UserVector<double>& row, double low, double up, Pointer<char> name=NULL);
00487                 void setRow(int index, const UserVector<double>& row, const ivector& indices, double low, double up, Pointer<char> name=NULL);
00488                 void setRowBounds(int index, double low, double up);
00489 
00490                 void resize_col(int nr_col_);
00491                 void resize_row(int nr_row_);
00492 
00493                 void finish();
00494 };
00495 
00496 #endif // PROBLEM_H

Generated on Mon Oct 20 03:12:06 2008 for LaGO by  doxygen 1.4.7