genmodel 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ /*
2
+ * GenModelOsi.h
3
+ *
4
+ * Created on: 2012-10-01
5
+ * Author: mbouchard
6
+ */
7
+
8
+ #ifndef GENMODELOSI_H_
9
+ #define GENMODELOSI_H_
10
+
11
+ #if defined WIN64 || defined WIN32
12
+ #ifndef snprintf
13
+ #define snprintf sprintf_s
14
+ #endif
15
+ #endif
16
+
17
+ #include "GenModel.h"
18
+ #include "OsiClpSolverInterface.hpp"
19
+ //#include "OsiCpxSolverInterface.hpp"
20
+ //#include "OsiSpxSolverInterface.hpp"
21
+ //#include "OsiGrbSolverInterface.hpp"
22
+ //#include "OsiGlpkSolverInterface.hpp"
23
+ //#include "OsiVolSolverInterface.hpp"
24
+ #include "CbcModel.hpp"
25
+ //#include "CbcBranchUser.hpp"
26
+ //#include "CbcCompareUser.hpp"
27
+ #include "CbcCutGenerator.hpp"
28
+ #include "CbcHeuristicLocal.hpp"
29
+ #include "CbcHeuristicGreedy.hpp"
30
+ #include "CglProbing.hpp"
31
+ #include "CbcHeuristic.hpp"
32
+ #include "GenModelOsiInterface.h"
33
+ #include "CoinTime.hpp"
34
+ #include "CglGomory.hpp"
35
+ #include "CglProbing.hpp"
36
+ #include "CglKnapsackCover.hpp"
37
+ #include "CglOddHole.hpp"
38
+ #include "CglClique.hpp"
39
+ #include "CglFlowCover.hpp"
40
+ #include "CglMixedIntegerRounding.hpp"
41
+
42
+ using namespace std;
43
+
44
+ class OsiData
45
+ {
46
+ public:
47
+ OsiData();
48
+ ~OsiData();
49
+ long Reset();
50
+ long Delete();
51
+
52
+ OsiSolverInterface* model;
53
+ CbcModel* mipmodel;
54
+ CoinBigIndex* mat_beg;
55
+ int* mat_r;
56
+ double* mat_v;
57
+ double* lrhs;
58
+ double* urhs;
59
+ double* ub;
60
+ double* lb;
61
+ int* typei;
62
+ int* typec;
63
+ double* obj;
64
+ char** cname;
65
+ char** rname;
66
+
67
+ int solvertype;
68
+ long nc;
69
+ long nr;
70
+ long nz;
71
+ long nq;
72
+ double mult;
73
+ };
74
+
75
+ class GenModelOsi : public GenModel
76
+ {
77
+ public:
78
+ ~GenModelOsi() {if (solverdata != NULL) delete static_cast<OsiData*>(solverdata);}
79
+ long Init(string name);//, int type=0);
80
+ long CreateModel();
81
+ long CreateModel(string filename, int type=0, string dn="");
82
+ long AddSolverRow(vector<int>& ind, vector<double>& val, double rhs, char sense, string name);
83
+ long AddSolverCol(vector<int>& ind, vector<double>& val, double obj, double lb, double ub, string name, char type = 'C');
84
+ long AddCut(int* cols, double* vals, int nz, double rhs, char sense, const char* name);
85
+ long AddCol(int* newi, double* newcol, int nz, double obj, double lb, double ub, const char* name, char type = 'C');
86
+ long WriteProblemToLpFile(string filename);
87
+ long WriteSolutionToFile(string filename);
88
+ long Solve();
89
+ long SetSol();
90
+ long Clean();
91
+ long SetDirectParam(int whichparam, genmodel_param value, string type, string message);
92
+ long SetParam(string param, int whichparam, string type, string message, bool implemented = true);
93
+ };
94
+
95
+
96
+ #endif /* GENMODELOSI_H_ */
@@ -0,0 +1,143 @@
1
+ #include <cassert>
2
+
3
+ #include "CoinHelperFunctions.hpp"
4
+ //#include "CoinIndexedVector.hpp"
5
+ #include "ClpQuadraticObjective.hpp"
6
+ #include "ClpLinearObjective.hpp"
7
+ #include "ClpObjective.hpp"
8
+ //#include "ClpSimplex.hpp"
9
+ #include "GenModelOsiInterface.h"
10
+
11
+ long GenModelOsiInterface::setQuadraticObjective(long nc, CoinBigIndex* Q_beg, int* Q_r, double* Q_v)
12
+ {
13
+ double offset;
14
+ double csol[2];
15
+ csol[0] = 2.2;
16
+ csol[1] = 0.7;
17
+
18
+ printf("Set Quadratic\n");
19
+ //ClpObjective * saveObjective = modelPtr_->objectiveAsObject();
20
+ //modelPtr_->loadQuadraticObjective(nc, Q_beg, Q_r, Q_v);
21
+
22
+ //double* gradz = modelPtr_->objective(csol, offset, true);
23
+ //for(int i = 0; i < nc; i++)
24
+ // printf("gradz = %f (%d)\n", gradz[i], Q_beg[i]);
25
+
26
+ if(quadraticObjective_ != NULL)
27
+ delete quadraticObjective_;
28
+
29
+ quadraticObjective_ = new ClpQuadraticObjective(modelPtr_->objective(),nc,Q_beg,Q_r,Q_v);
30
+ double* buf = modelPtr_->objective();
31
+ for(int i = 0; i < nc; i++)
32
+ printf("obj = %f\n", buf[i]);
33
+
34
+ double* grad = ((ClpQuadraticObjective*)quadraticObjective_)->gradient(modelPtr_, csol, offset, true);
35
+ for(int i = 0; i < nc; i++)
36
+ printf("grad = %f\n", grad[i]);
37
+
38
+ ClpLinearObjective * linearObjective = new ClpLinearObjective(NULL,nc);
39
+ modelPtr_->setObjectivePointer(linearObjective);
40
+
41
+ return 0;
42
+ }
43
+
44
+ void GenModelOsiInterface::initialSolve()
45
+ {
46
+ // save cutoff
47
+ double cutoff = modelPtr_->dualObjectiveLimit();
48
+ modelPtr_->setDualObjectiveLimit(1.0e50);
49
+ modelPtr_->scaling(0);
50
+ //modelPtr_->setLogLevel(0);
51
+ // solve with no objective to get feasible solution
52
+ setBasis(basis_,modelPtr_);
53
+ printf("before dual\n");
54
+ modelPtr_->dual();
55
+ printf("after dual\n");
56
+ basis_ = getBasis(modelPtr_);
57
+ modelPtr_->setDualObjectiveLimit(cutoff);
58
+ if (modelPtr_->problemStatus())
59
+ {
60
+ assert (modelPtr_->problemStatus()==1);
61
+ return;
62
+ }
63
+ ClpObjective * saveObjective = modelPtr_->objectiveAsObject();
64
+ modelPtr_->setObjectivePointer(quadraticObjective_);
65
+ //modelPtr_->setLogLevel(1);
66
+ // Could load up any data into a solver
67
+ printf("before primal\n");
68
+ modelPtr_->primal();
69
+ printf("after primal\n");
70
+ modelPtr_->setDualObjectiveLimit(cutoff);
71
+ if (modelPtr_->objectiveValue()>cutoff)
72
+ modelPtr_->setProblemStatus(1);
73
+ // zero reduced costs
74
+ // Should not have to as convex
75
+ //CoinZeroN(modelPtr_->dualRowSolution(),modelPtr_->numberRows());
76
+ //CoinZeroN(modelPtr_->dualColumnSolution(),modelPtr_->numberColumns());
77
+ modelPtr_->setObjectivePointer(saveObjective);
78
+ }
79
+
80
+ void GenModelOsiInterface::resolve()
81
+ {
82
+ initialSolve();
83
+ }
84
+
85
+
86
+ GenModelOsiInterface::GenModelOsiInterface () : OsiClpSolverInterface()
87
+ {
88
+ quadraticObjective_ = NULL;
89
+ }
90
+
91
+
92
+ OsiSolverInterface* GenModelOsiInterface::clone(bool CopyData) const
93
+ {
94
+ if (CopyData)
95
+ return new GenModelOsiInterface(*this);
96
+ else
97
+ throw string("warning GenModelOsiInterface clone with copyData false\n");
98
+ }
99
+
100
+
101
+ GenModelOsiInterface::GenModelOsiInterface (const GenModelOsiInterface & rhs) : OsiClpSolverInterface(rhs)
102
+ {
103
+ if (rhs.quadraticObjective_)
104
+ quadraticObjective_=rhs.quadraticObjective_->clone();
105
+ else
106
+ quadraticObjective_=NULL;
107
+ }
108
+
109
+ GenModelOsiInterface::~GenModelOsiInterface()
110
+ {
111
+ delete quadraticObjective_;
112
+ }
113
+
114
+ GenModelOsiInterface & GenModelOsiInterface::operator=(const GenModelOsiInterface& rhs)
115
+ {
116
+ if (this != &rhs)
117
+ {
118
+ OsiClpSolverInterface::operator=(rhs);
119
+ if (rhs.quadraticObjective_)
120
+ quadraticObjective_=rhs.quadraticObjective_->clone();
121
+ else
122
+ quadraticObjective_=NULL;
123
+ }
124
+ return *this;
125
+ }
126
+
127
+ // Real initializer
128
+ void GenModelOsiInterface::initialize ()
129
+ {
130
+ printf("!!!! @@@@@@@@@ Initialize @@@@@@@@@ !!!!!!!\n");
131
+ // Save true objective and create a fake one
132
+ delete quadraticObjective_;
133
+ quadraticObjective_ = modelPtr_->objectiveAsObject();
134
+ ClpLinearObjective * linearObjective = new ClpLinearObjective(NULL,modelPtr_->numberColumns());
135
+ modelPtr_->setObjectivePointer(linearObjective);
136
+ }
137
+
138
+ // Get objective function value (can't use default)
139
+ double GenModelOsiInterface::getObjValue() const
140
+ {
141
+ // first try easy way
142
+ return modelPtr_->objectiveValue();
143
+ }
@@ -0,0 +1,43 @@
1
+ /***************************************************************************
2
+ * GenModelOsiInterface.h
3
+ * Replacememnt for the default OsiClpSolverInterface
4
+ *
5
+ * February 14 9:32 2014
6
+ * Copyright 2014 Mathieu Bouchard
7
+ * mathbouchard@gmail.com
8
+ ****************************************************************************/
9
+
10
+ #ifndef GENMODELOSIINTERFACE_H_
11
+ #define GENMODELOSIINTERFACE_H_
12
+
13
+ #include "OsiClpSolverInterface.hpp"
14
+ #include <string>
15
+
16
+ using namespace std;
17
+
18
+ // This is to allow the user to replace initialSolve and resolve
19
+
20
+ class GenModelOsiInterface : public OsiClpSolverInterface {
21
+
22
+ public:
23
+
24
+ GenModelOsiInterface ();
25
+ GenModelOsiInterface (const GenModelOsiInterface &);
26
+ virtual ~GenModelOsiInterface ();
27
+ virtual OsiSolverInterface * clone(bool CopyData=true) const;
28
+ GenModelOsiInterface & operator=(const GenModelOsiInterface& rhs);
29
+
30
+ // Solve initial LP relaxation
31
+ virtual void initialSolve();
32
+ /// Resolve an LP relaxation after problem modification
33
+ virtual void resolve();
34
+ // Setup fake objective or somehow get nonlinear info
35
+ void initialize();
36
+ virtual double getObjValue() const;
37
+ long setQuadraticObjective(long nc, CoinBigIndex* Q_beg, int* Q_r, double* Q_v);
38
+
39
+ private:
40
+ ClpObjective * quadraticObjective_;
41
+ };
42
+
43
+ #endif //GENMODELOSIINTERFACE_H_