ampl.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: Stefan Vigerske
00006 
00007 
00008 #ifndef AMPL_H
00009 #define AMPL_H
00010 
00011 #include "standard.h"
00012 
00013 #ifdef COIN_HAS_ASL
00014 
00015 #include "func.h"
00016 #include "problem.h"
00017 
00018 #include "asl.h"
00019 #undef filename
00020 
00024 extern ASL *asl;
00025 
00028 class ampl {
00029 private:
00032   Pointer<char> stubfile;
00033 
00034 public:
00035 
00040   ampl(char* stubfile_) {
00041     if (! stubfile_) {
00042       out_err << "ampl::ampl: Given filename was NULL !" << endl;
00043       exit(-1);
00044     }
00045     stubfile = strdup(stubfile_);
00046   }
00047 
00051   Pointer<MinlpProblem> get_problem();
00052 
00057   void write_sol_file(const UserVector<double>& sol_point, char* message="") {
00058     write_sol(message, (double*)(const Pointer<double>)sol_point, NULL, NULL);
00059   }
00060 
00061 };
00062 
00066 class amplObj : public Func {
00067 private:
00068         Func::CurvatureType curv_type;
00069 public:
00075   amplObj(int dim_=0, Pointer<SparsityInfo> sparsity_=NULL, Pointer<ostream> out_func_p_=out_out_p, Pointer<ostream> out_func_log_p_=out_log_p)
00076   : Func(dim_, out_func_p_, out_func_log_p_)
00077   { sparsity=sparsity_;
00078   }
00079 
00085   double eval(const UserVector<double>& x) const {
00086         delete asl->i.err_jmp_;
00087         asl->i.err_jmp_=new Jmp_buf;
00088           if (setjmp(asl->i.err_jmp_->jb)) {
00089                 out_log << "ASL-error evaluating objective" << endl;
00090                 out_log << "Last point: " << endl;
00091                 for (int i=0; i<x.dim(); i++) out_log << var_name(i) << ": " << x(i) << endl;
00092             write_sol("ASL-error evaluating objective, aborted", (double*)(const Pointer<double>)x, NULL, NULL);
00093                 exit(0);
00094           }
00095     return objval(0, (double*)(const Pointer<double>)x, NULL);
00096   }
00097 
00103         void grad(dvector& g, const dvector& x) const {
00104         delete asl->i.err_jmp_;
00105         asl->i.err_jmp_=new Jmp_buf;
00106           if (setjmp(asl->i.err_jmp_->jb)) {
00107                 out_log << "ASL-error evaluating gradient of objective" << endl;
00108                 out_log << "Last point: " << endl;
00109                 for (int i=0; i<x.dim(); i++) out_log << var_name(i) << ": " << x(i) << endl;
00110             write_sol("ASL-error computing gradient of objective, aborted", (double*)(const Pointer<double>)x, NULL, NULL);
00111                 exit(0);
00112           }
00113     objgrd(0, (double*)(const Pointer<double>)x, (double*)(Pointer<double>)g, NULL);
00114         }
00115 
00116   void grad(UserVector<double>& g, const UserVector<double>& x) const {
00117         delete asl->i.err_jmp_;
00118         asl->i.err_jmp_=new Jmp_buf;
00119           if (setjmp(asl->i.err_jmp_->jb)) {
00120                 out_log << "ASL-error computing gradient of objective" << endl;
00121                 out_log << "Last point: " << endl;
00122                 for (int i=0; i<x.dim(); i++) out_log << var_name(i) << ": " << x(i) << endl;
00123             write_sol("ASL-error computing gradient of objective, aborted", (double*)(const Pointer<double>)x, NULL, NULL);
00124                 exit(0);
00125           }
00126     double* gr=new double[g.size()];
00127     objgrd(0, (double*)(const Pointer<double>)x, gr, NULL);
00128     g.set(gr, g.dim());
00129     delete gr;
00130   }
00131         
00132         using Func::grad;
00133 
00139   void HessMult(dvector& y, const dvector& x, const dvector& z) const {
00140     eval(x);
00141     hvcomp((double*)(const Pointer<double>)y, (double*)(const Pointer<double>)z, 0, NULL, NULL);
00142   }
00143 
00144   void HessMult(UserVector<double>& y, const UserVector<double>& x, const UserVector<double>& z) const {
00145     eval(x);
00146     double* y0=new double[y.size()];
00147     hvcomp(y0, (double*)(const Pointer<double>)z, 0, NULL, NULL);
00148     y.set(y0, y.dim());
00149     delete y0;
00150   }
00151         
00152         using Func::HessMult;
00153 
00154         void set_curvature(CurvatureType ct) { curv_type=ct; };
00155         CurvatureType get_curvature() const { return curv_type; };
00156 
00161   void print(ostream& out) const {
00162     out << "amplObj: dim=" << dim() << endl;
00163   }
00164 
00165 };
00166 
00169 class amplCon : public Func {
00170 private:
00173   fint connr;
00174 
00177   Pointer<real> con_select;
00178 
00179         Func::CurvatureType curv_type;
00180 public:
00189   amplCon(int connr_, int dim_=0, Pointer<SparsityInfo> sparsity_=NULL, Pointer<ostream> out_func_p_=out_out_p, Pointer<ostream> out_func_log_p_=out_log_p)
00190   : Func(dim_, out_func_p_, out_func_log_p_), connr(connr_), con_select(new real[n_con])
00191   { for (int i=0; i<n_con; i++) con_select[i]=0;
00192     con_select[connr]=1;
00193     sparsity=sparsity_;
00194   }
00195 
00201   double eval(const UserVector<double>& x) const {
00202         delete asl->i.err_jmp_;
00203         asl->i.err_jmp_=new Jmp_buf;
00204           if (setjmp(asl->i.err_jmp_->jb)) {
00205                 out_log << "ASL-error evaluating constraint " << connr << ": " << con_name(connr) << endl;
00206                 out_log << "Last point: " << endl;
00207                 for (int i=0; i<x.dim(); i++) out_log << var_name(i) << ": " << x(i) << endl;
00208                 char* str=new char[200]; sprintf(str, "ASL-error evaluating constraint %i: %s", connr, con_name(connr));
00209             write_sol(str, (double*)(const Pointer<double>)x, NULL, NULL);
00210                 exit(0);
00211   }
00212         return conival(connr, (double*)(const Pointer<double>)x, NULL);
00213   }
00214 
00220   void grad(dvector &g, const dvector &x) const {
00221         delete asl->i.err_jmp_;
00222         asl->i.err_jmp_=new Jmp_buf;
00223           if (setjmp(asl->i.err_jmp_->jb)) {
00224                 out_log << "ASL-error computing gradient of constraint " << connr << ": " << con_name(connr) << endl;
00225                 out_log << "Last point: " << endl;
00226                 for (int i=0; i<x.dim(); i++) out_log << var_name(i) << ": " << x(i) << endl;
00227                 char* str=new char[200]; sprintf(str, "ASL-error computing gradient of constraint %i: %s", connr, con_name(connr));
00228             write_sol(str, (double*)(const Pointer<double>)x, NULL, NULL);
00229                 exit(0);
00230           }
00231     congrd(connr, (double*)(const Pointer<double>)x, (double*)(Pointer<double>)g, NULL);
00232   }
00233 
00234   void grad(UserVector<double>& g, const UserVector<double>& x) const {
00235         delete asl->i.err_jmp_;
00236         asl->i.err_jmp_=new Jmp_buf;
00237           if (setjmp(asl->i.err_jmp_->jb)) {
00238                 out_log << "ASL-error computing gradient of constraint " << connr << ": " << con_name(connr) << endl;
00239                 out_log << "Last point: " << endl;
00240                 for (int i=0; i<x.dim(); i++) out_log << var_name(i) << ": " << x(i) << endl;
00241                 char* str=new char[200]; sprintf(str, "ASL-error computing gradient of constraint %i: %s", connr, con_name(connr));
00242             write_sol(str, (double*)(const Pointer<double>)x, NULL, NULL);
00243                 exit(0);
00244           }
00245     double* gr=new double[g.size()];
00246     congrd(connr, (double*)(const Pointer<double>)x, gr, NULL);
00247     g.set(gr, g.dim());
00248     delete gr;
00249   }
00250         
00251         using Func::grad;
00252 
00258   void HessMult(dvector& y, const dvector& x, const dvector& z) const {
00259     eval(x);
00260     hvcomp((double*)(Pointer<double>)y, (double*)(const Pointer<double>)z, -1, NULL, con_select);
00261   }
00262 
00263   void HessMult(UserVector<double>& y, const UserVector<double>& x, const UserVector<double>& z) const {
00264     eval(x);
00265     double* y0=new double[y.size()];
00266     hvcomp(y0, (double*)(const Pointer<double>)z, -1, NULL, con_select);
00267     y.set(y0, y.dim());
00268     delete y0;
00269   }
00270         
00271         using Func::HessMult;
00272 
00273         void set_curvature(CurvatureType ct) { curv_type=ct; };
00274         CurvatureType get_curvature() const { return curv_type; };
00275         
00280   void print(ostream& out) const {
00281     out << "amplCon " << connr << ": dim=" << dim() << ": " << con_name(connr) << endl;
00282   }
00283 };
00284 
00285 
00286 #endif // COIN_HAS_ASL
00287 #endif // AMPL_H

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