CONOPT
Loading...
Searching...
No Matches
conopt.hpp
Go to the documentation of this file.
1
7
8#ifndef _CONOPT_HPP_
9#define _CONOPT_HPP_
10
11#include <algorithm>
12#include <array>
13#include <string>
14#include <vector>
15
16#include "conopt.h"
17
19class ConoptModelData;
20class ConoptSolution;
21class ConoptStatus;
22
27class Conopt {
28public:
30 static constexpr double Infinity = 1e+20;
31
38 COI_API Conopt(std::string modelName = "conopt-model");
39
45
53
57 COI_API int checkModelAndParameters();
58
66 COI_API coiHandle_t controlVector();
67
74
80 COI_API std::string getName();
81
82 /* model data methods */
83
95
96 /* optimisation status methods */
97
103
110
117
124
125 /* solution methods */
126
132 COI_API const std::vector<double>& getVariableValues();
133
139 COI_API const std::vector<double>& getVariableMarginals();
140
146 COI_API const std::vector<int>& getVariableBasisStatus();
147
153 COI_API const std::vector<int>& getVariableStatus();
154
160 COI_API const std::vector<double>& getConstraintValues();
161
167 COI_API const std::vector<double>& getConstraintMarginals();
168
174 COI_API const std::vector<int>& getConstraintBasisStatus();
175
181 COI_API const std::vector<int>& getConstraintStatus();
182
183 /* message handler methods */
184
196
204 COI_API int sendMessage(std::string msg);
205
219 COI_API void setVerbosityLevel(int verblevel);
220
221#if 0
222
223
224 virtual int Option(int ncall, double* rval, int* ival, int* lval, char* name, void* usrmem)
225 {
226 return 0;
227 }
228
230 virtual int TriOrd(int mode, int type, int status, int rowno, int colno, int inf, double value, double resid,
231 void* usrmem)
232 {
233 return 0;
234 }
235
236
237#endif
238
246 static COI_API std::array<int, 3> version();
247
255 COI_API int debugFV(int debugfv);
256
265
272 COI_API int setLicense(int licint1, int licint2, int licint3, std::string licstring);
273
280 COI_API int setItLim(int itlim);
281
288 COI_API int setErrLim(int errlim);
289
297
305
312 COI_API int setMaxSup(int maxsup);
313
320 COI_API int allowEmptyRow(int emptyrow);
321
328 COI_API int allowEmptyCol(int emptycol);
329
336 COI_API int debug2D(int debug2d);
337
344 COI_API int disCont(int discont);
345
346
353 COI_API int clearM(int clearm);
354
361 COI_API int setResLim(double reslim);
362
369 COI_API int setMaxHeap(double maxheap);
370
377 COI_API int setThreadS(int threads);
378
385 COI_API int setThreadF(int threadf);
386
393 COI_API int setThread2D(int thread2d);
394
401 COI_API int setThreadC(int threadc);
402
414
421
428
429private:
430 std::string name;
431
432 int coi_error_;
433 coiHandle_t cntVect_;
434
435 ConoptModelData* modeldata_;
436 ConoptSolution* solution_;
437 ConoptStatus* status_;
438
439 /* message handling objects. We create a default message handler, which can be replaced by the user if necessary */
440 ConoptMessageHandler* defmessage_;
441 ConoptMessageHandler* message_;
442};
443
444/*******************
445 * ConoptModelData *
446 *******************/
447
448#ifdef _MSC_VER
449#pragma warning( push )
450// disable warning on using STL (members without a DLL interface) in ConoptModelData (class with a DLL interface)
451#pragma warning( disable: 1744 )
452#endif
453
468
484
490enum class ConoptSense
491{
492 Minimize = -1,
494};
495
507
518{
519 ConoptVariable(int idx, double low, double up, double cur = 0, int varstat = -1)
520 : index(idx),
521 lower(low),
522 upper(up),
523 curr(cur),
524 varstatus(varstat)
525 {
526 consindex_.clear();
527 value_.clear();
528 nlflag_.clear();
529
530 sortorder_.clear();
531 }
532
537 void addNonzero(int conindex, double value, int nlflag)
538 {
539 sortorder_.push_back(consindex_.size());
540
541 consindex_.push_back(conindex);
542 value_.push_back(value);
543 nlflag_.push_back(nlflag);
544
545 }
546
552 {
553 std::sort(sortorder_.begin(), sortorder_.end(),
554 [&](int a, int b){
555 return consindex_[a] < consindex_[b];
556 });
557 }
558
559 int index;
560 double lower;
561 double upper;
562 double curr;
564
565 /* matrix information */
566 std::vector<int> consindex_;
567 std::vector<double> value_;
568 std::vector<int> nlflag_;
569
570 /* information required to sort the non-zeros by consindex */
571 std::vector<int> sortorder_;
572};
573
574
585{
586 ConoptConstraint(int idx, ConoptConstraintType type, double bound, int slackstat)
587 : index(idx),
588 constype(type),
589 rhs(bound),
590 slackstatus(slackstat)
591 {
592 }
593
594 int index;
596 double rhs;
598};
599
600
605public:
610
615
644 virtual int readMatrix(double lower[], double curr[], double upper[], int vsta[], int type[], double rhs[],
645 int esta[], int colsta[], int rowno[], double value[], int nlflag[], int numvar, int numcon, int numnz);
646
656 unsigned int numvar,
657 unsigned int numcons,
658 unsigned int numnz,
659 unsigned int numnlnz
660 );
661
668 ConoptConstraintType constype,
669 double rhs,
670 int slackstatus = -1
671 );
672
679 ConoptConstraintType constype,
680 double rhs,
681 const std::vector<int>& varindex,
682 const std::vector<double>& value,
683 const std::vector<int>& nlflag,
684 int slackstatus = -1
685 );
686
693 double lower,
694 double upper,
695 double curr = 0,
696 int varstatus = -1
697 );
698
705 double lower,
706 double upper,
707 const std::vector<int>& consindex,
708 const std::vector<double>& value,
709 const std::vector<int>& nlflag,
710 double curr = 0,
711 int varstatus = -1
712 );
713
720
727
757 void setInitialStatusOption(int inistat);
758
764 int numVar() const;
765
771 int numCons() const;
772
779
785 const ConoptVariable& getVariable(int index) const;
786
793
799 const ConoptConstraint& getConstraint(int index) const;
800
807
814
821 const std::vector<int>& rownum,
822 const std::vector<int>& colnum
823 );
824
826 const std::vector<int>& getSDLagrangianRowNumbers() const;
827
829 const std::vector<int>& getSDLagrangianColumnNumbers() const;
830
831 /* callback methods for function and derivative evaluations */
832
844 virtual int FDEval(const double x[], double* g, double jac[], int rowno, const int jacnum[], int mode, int ignerr,
845 int* errcnt, int numvar, int numjac, int thread)
846 {
847 return 0;
848 }
849
850 /* The optional callback routines for first derivative evaluations */
851
864 virtual int FDEvalIni(const double x[], const int rowlist[], int mode, int listsize, int numthread, int ignerr,
865 int* errcnt, int numvar)
866 {
867 return 0;
868 }
869
880 virtual int FDEvalEnd(int ignerr, int* errcnt)
881 {
882 return 0;
883 }
884
895 virtual int FDInterval(const double xmin[], const double xmax[], double* gmin, double* gmax, double jmin[],
896 double jamx[], int rowno, const int jacnum[], int mode, double pinf, int numvar, int numjac)
897 {
898 return 0;
899 }
900
901 /* The optional callback routines for the second derivative evaulations */
902
903#if 0 // not sure whether this is needed to C and C++.
906 virtual int SDLagrSize(int* nodrv, int numvar, int numcon, int* nhess, int maxhess)
907 {
908 return 0;
909 }
910#endif
911
921 virtual int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[], double hsvl[],
922 int* nodrv, int numvar, int numcon, int nhess)
923 {
924 return 0;
925 }
926
934 virtual int SDDirLagr(const double x[], const double dx[], const double u[], double d2g[], int newpt, int* nodrv,
935 int numvar, int numcon)
936 {
937 return 0;
938 }
939
947 virtual int SDDir(const double x[], const double dx[], double d2g[], int rowno, const int jacnum[], int* nodrv,
948 int numvar, int numjac, int thread)
949 {
950 return 0;
951 }
952
962 virtual int SDDirIni(const double x[], const double dx[], const int rowlist[], int listsize, int numthread,
963 int newpt, int* nodrv, int numvar)
964 {
965 return 0;
966 }
967
977 virtual int SDDirEnd(int* nodrv)
978 {
979 return 0;
980 }
981
982
983
984 friend class Conopt;
985
986private:
987
988 /* clears the model data */
989 void clear()
990 {
991 variables_.clear();
992 constraints_.clear();
993
994 hessrownum_.clear();
995 hesscolnum_.clear();
996
997 numvar_ = 0;
998 numcons_ = 0;
999 numnz_ = 0;
1000 numnlnz_ = 0;
1001 numhessnz_ = -1;
1002 optsense_ = (int)(ConoptSense::Minimize);
1003 objectivecon_ = -1;
1004 objectivevar_ = -1;
1005
1006 inistat_ = 0;
1007 }
1008
1009 unsigned int numvar_;
1010 unsigned int numcons_;
1011 unsigned int numnz_;
1012 unsigned int numnlnz_;
1013 int numhessnz_;
1014 int optsense_;
1015 int objectivecon_;
1016 int objectivevar_;
1017
1018 int inistat_;
1019
1020 bool probdimset_;
1021 ConoptSDEvaluationType sdevaltype_;
1022
1023 std::vector<ConoptVariable> variables_;
1024 std::vector<ConoptConstraint> constraints_;
1025
1026 std::vector<int> hessrownum_;
1027 std::vector<int> hesscolnum_;
1028};
1029
1030#ifdef _MSC_VER
1031#pragma warning( pop )
1032#endif
1033
1034/************************
1035 * ConoptMessageHandler *
1036 ************************/
1037
1038#ifdef _MSC_VER
1039#pragma warning( push )
1040// disable warning on using STL (members without a DLL interface) in ConoptMessageHandler (class with a DLL interface)
1041#pragma warning( disable: 1744 )
1042#endif
1043
1051enum class ConoptVerbosityLevel
1052{
1053 VerbLevelMin = 0,
1054 None,
1055 Normal,
1056 Error,
1057 Debug,
1059};
1060
1061struct ConoptAlgProgress
1062{
1063 int iteration;
1064 int phase;
1065 int numinfeas;
1067 int numnopt;
1069 int numsuper;
1070 double suminfeas;
1073 double objvalue;
1075 double rgmax;
1077 double step;
1078 int numvar;
1079};
1080
1086public:
1090 ConoptMessageHandler();
1091
1095 virtual ~ConoptMessageHandler();
1096
1107 virtual int message(
1108 int smsg,
1109 int dmsg,
1110 int nmsg,
1111 const std::vector<std::string>& msgv
1112 );
1113
1145 virtual int errorMessage(
1146 int rowno,
1147 int colno,
1148 int posno,
1149 const std::string& msg
1150 );
1151
1152 virtual int progress(
1153 const ConoptAlgProgress& progressdata
1154 )
1155 {
1156 return 0;
1157 }
1158
1164 int sendMessage(const std::string& msg);
1165
1174
1175 friend class Conopt;
1176private:
1177 ConoptVerbosityLevel verblevel_;
1178};
1179
1180#ifdef _MSC_VER
1181#pragma warning( pop )
1182#endif
1183
1184#endif // _CONOPT_HPP_
The message handler class.
Definition conopt.hpp:1604
virtual int progress(const ConoptAlgProgress &progressdata)
Definition conopt.hpp:1152
friend class Conopt
Definition conopt.hpp:1175
The Model Data class.
Definition conopt.hpp:604
const std::vector< int > & getSDLagrangianColumnNumbers() const
returns the column numbers in the second derivative of the lagrangian structure
friend class Conopt
Definition conopt.hpp:984
const std::vector< int > & getSDLagrangianRowNumbers() const
returns the row numbers in the second derivative of the lagrangian structure
static constexpr double Infinity
Definition conopt.hpp:30
public C API header file
#define COI_API
Definition conopt.h:29
program fvforall
Main program. A simple setup and call of CONOPT.
Definition fvforall.f90:20
program fvinclin
Main program. A simple setup and call of CONOPT.
Definition fvinclin.f90:20
void setMessageHandler(ConoptMessageHandler &msghandler)
sets the message handler to the user supplied handler.
int sendMessage(std::string msg)
sends a message to the message handler
void setVerbosityLevel(int verblevel)
sets the verbosity level for messaging
void loadModel(ConoptModelData &modeldata)
loads the model and stores the pointer in the interface
virtual ~ConoptModelData()
Conopt(std::string modelName="conopt-model")
ConoptObjectiveElement
the element that is used for the objective function
Definition conopt.hpp:480
int addVariable(double lower, double upper, double curr=0, int varstatus=-1)
adds a variable to the model. The non-zero coefficients are added later.
int addVariable(double lower, double upper, const std::vector< int > &consindex, const std::vector< double > &value, const std::vector< int > &nlflag, double curr=0, int varstatus=-1)
adds a variable to the problem. The matrix non-zeros are added based on the supplied constraints.
ConoptSense
the objective sense
Definition conopt.hpp:491
void setObjectiveElement(ConoptObjectiveElement elem, int elemindex)
sets the index for the objective variable or constraint
int addConstraint(ConoptConstraintType constype, double rhs, int slackstatus=-1)
adds a constraint to the problem. The non-zero coefficients are added later
ConoptConstraintType
the constraint type
Definition conopt.hpp:462
int addConstraint(ConoptConstraintType constype, double rhs, const std::vector< int > &varindex, const std::vector< double > &value, const std::vector< int > &nlflag, int slackstatus=-1)
adds a constraint to the problem. The matrix non-zeros are added based on the supplied variables
void setOptimizationSense(ConoptSense sense)
sets the optimisation direction.
virtual int readMatrix(double lower[], double curr[], double upper[], int vsta[], int type[], double rhs[], int esta[], int colsta[], int rowno[], double value[], int nlflag[], int numvar, int numcon, int numnz)
loads the structure of the model into CONOPT.
void setProblemDimension(unsigned int numvar, unsigned int numcons, unsigned int numnz, unsigned int numnlnz)
sets the problem dimension. This is called if the user wants to implement a custom readMatrix() metho...
void setInitialStatusOption(int inistat)
the setting to indicate how the initial status of the variables and slack variables will be handled.
void setSDLagrangianStructure(const std::vector< int > &rownum, const std::vector< int > &colnum)
sets the structure of the second derivatives of the Lagrangian
void setSDEvaluationType(ConoptSDEvaluationType sdevaltype)
informs CONOPT of the method for evaluating the second derivative
ConoptSDEvaluationType
the evaluation type for the directional second derivatives
Definition conopt.hpp:502
virtual int SDDirLagr(const double x[], const double dx[], const double u[], double d2g[], int newpt, int *nodrv, int numvar, int numcon)
computes the directional second derivative for the Lagrangian
Definition conopt.hpp:934
virtual int SDDir(const double x[], const double dx[], double d2g[], int rowno, const int jacnum[], int *nodrv, int numvar, int numjac, int thread)
computes the directional second derivative for a single constraint
Definition conopt.hpp:947
virtual int SDDirIni(const double x[], const double dx[], const int rowlist[], int listsize, int numthread, int newpt, int *nodrv, int numvar)
called by CONOPT before a sequence of 2DDir calls each time either the point or the direction changes...
Definition conopt.hpp:962
virtual int SDDirEnd(int *nodrv)
called by CONOPT after a sequence of 2DDir calls each time either the point or the direction changes.
Definition conopt.hpp:977
virtual int SDLagrVal(const double x[], const double u[], const int hsrw[], const int hscl[], double hsvl[], int *nodrv, int numvar, int numcon, int nhess)
Computes and returns the numerical values of the Hessian.
Definition conopt.hpp:921
virtual int FDEval(const double x[], double *g, double jac[], int rowno, const int jacnum[], int mode, int ignerr, int *errcnt, int numvar, int numjac, int thread)
defines the nonlinearities of the model by returning numerical values.
Definition conopt.hpp:844
virtual int FDEvalEnd(int ignerr, int *errcnt)
an optional function that will be called at the end of the function evaluation stage.
Definition conopt.hpp:880
virtual int FDEvalIni(const double x[], const int rowlist[], int mode, int listsize, int numthread, int ignerr, int *errcnt, int numvar)
an optional function that can be used to improve the efficiency of the function evaulations.
Definition conopt.hpp:864
virtual int FDInterval(const double xmin[], const double xmax[], double *gmin, double *gmax, double jmin[], double jamx[], int rowno, const int jacnum[], int mode, double pinf, int numvar, int numjac)
defines intervals for the nonlinearities of the model, again by returning numerical values.
Definition conopt.hpp:895
virtual int errorMessage(int rowno, int colno, int posno, const std::string &msg)
virtual method for handling error messages.
void setVerbosityLevel(ConoptVerbosityLevel verblevel)
sets the verbosity level for messaging.
int sendMessage(const std::string &msg)
sends a message to the message handler
virtual int message(int smsg, int dmsg, int nmsg, const std::vector< std::string > &msgv)
virtual method for handling the messages.
ConoptVerbosityLevel
the verbosity level of the message handler
Definition conopt.hpp:1571
int numCons() const
returns the number of constraints in the model
ConoptVariable & getVariable(int index)
returns a reference to the variable object
int numVar() const
returns the number of variables in the model
int numHessianNonzeros() const
returns the number of non-zeros in the Hessian
ConoptConstraint & getConstraint(int index)
returns a reference to the constraint object
const ConoptConstraint & getConstraint(int index) const
returns a reference to the constraint object
const ConoptVariable & getVariable(int index) const
returns a reference to the variable object
int setThreadS(int threads)
number of threads allowed internally in CONOPT.
int disCont(int discont)
allow discontinuous functions and derivatives.
int fVforAll(int fvforall)
call the FDEval for all constraints, including linear constraints.
int fVincLin(int fvinclin)
include the linear terms in function evaluations.
int allowEmptyRow(int emptyrow)
allow empty rows.
int squareModel(int square)
square models.
int setErrLim(int errlim)
define the Error Limit.
int allowEmptyCol(int emptycol)
allow empty columns.
int setThreadF(int threadf)
number of threads allowed for simultaneous FDEval calls.
int setItLim(int itlim)
define the Iteration Limit.
int setThread2D(int thread2d)
number of threads allowed for simultaneous 2DDir calls.
int debug2D(int debug2d)
turn debugging of 2nd derivatives on and off.
int setLicense(int licint1, int licint2, int licint3, std::string licstring)
define the License Information.
int setThreadC(int threadc)
check for thread compatibility.
int setResLim(double reslim)
define resource limit.
int clearM(int clearm)
ClearM.
int setMaxSup(int maxsup)
limit on superbasics.
int setMaxHeap(double maxheap)
define Limit on Heap Memory.
int debugFV(int debugfv)
turn Debugging of FDEval on and off.
const std::vector< double > & getConstraintMarginals()
returns the constraint marginals
const std::vector< int > & getVariableBasisStatus()
returns the variable basis status
const std::vector< int > & getConstraintBasisStatus()
returns the constraint basis status
const std::vector< double > & getConstraintValues()
returns the constraint values
const std::vector< double > & getVariableMarginals()
returns the variable marginals
int solutionStatus()
return the solution status
double objectiveValue()
returns the objective value
int modelStatus()
returns the model status
const std::vector< int > & getConstraintStatus()
returns the constraint status
const std::vector< int > & getVariableStatus()
returns the variable status
int iterations()
returns the number of iterations
const std::vector< double > & getVariableValues()
returns the variable values
static std::array< int, 3 > version()
returns the version number.
coiHandle_t controlVector()
returns the control vector pointer.
void printStatus()
prints the status of the optimisation
int solve()
method for starting the solving process of CONOPT.
int getRangeErrors()
returns the range errors that were encountered.
int getMaxThreads()
returns the maximum number of threads that can be used by CONOPT.
std::string getName()
returns the model name
double getMaxHeapUsed()
After a model has been solved this method will return the amount of heap memory used.
program square
Main program. A simple setup and call of CONOPT.
Definition square.f90:23
the constraint data
Definition conopt.hpp:585
ConoptConstraintType constype
Definition conopt.hpp:595
ConoptConstraint(int idx, ConoptConstraintType type, double bound, int slackstat)
Definition conopt.hpp:586
the variable data
Definition conopt.hpp:518
std::vector< int > sortorder_
Definition conopt.hpp:571
void addNonzero(int conindex, double value, int nlflag)
adds a non-zero to the variable data
Definition conopt.hpp:537
std::vector< int > consindex_
Definition conopt.hpp:566
void sortNonzeros()
sorts the non-zeros by constraint index.
Definition conopt.hpp:551
ConoptVariable(int idx, double low, double up, double cur=0, int varstat=-1)
Definition conopt.hpp:519
std::vector< int > nlflag_
Definition conopt.hpp:568
std::vector< double > value_
Definition conopt.hpp:567