-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_started.cpp
182 lines (155 loc) · 4.53 KB
/
get_started.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright 2014 National ICT Australia Limited (NICTA)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <madopt/ipopt_model.hpp>
//#include <madopt/bonmin_model.hpp>
#include <vector>
#include <math.h>
using namespace std;
int main(){
int N = pow(10, 4);
//create IpoptModel
MadOpt::IpoptModel m;
//create BonminModel
//MadOpt::BonminModel m;
// Options
//
//
//m.setIntegerOption("print_level", 5);
//m.setStringOption("print_timing_statistics", "yes");
//m.setStringOption("hessian_approximation", "limited-memory");
// shortcut flags
// set flag if solver output should be printed
m.show_solver = true;
// timelimit, a negative value disables the time limit
m.timelimit = 12;
// Variables
//
//
// create continuous variable x
// lower bound = -1.5
// upper bound = 0
// initial value = -0.5
// MadOpt::Var x = m.addVar(-1.5, 0, -0.5, "x");
// to set a bound to infinity use MadOpt::INF
// MadOpt::Var v = m.addVar(MadOpt::INF, 0, -0.5, "v");
// create integer variable y
// lower bound = 1
// upper bound = 10
// initial value = 4
// MadOpt::Var y = m.addIVar(1, 10, 4, "y");
// create binary variable z
// initial value = 1
// MadOpt::Var z = m.addBVar(1, "z");
vector<MadOpt::Var> x(N);
for (int i=0; i<N; i++){
x[i] = m.addVar(-1.5, 0, -0.5, "x" + to_string(i));
}
// Parameters
//
//
// create Parameter
// value = 1
// name = p
// MadOpt::Param p = m.addParam(1, "p");
vector<MadOpt::Param> p(N-2);
for (int i=0; i<N-2; i++){
double a = double(i+2)/(double)N;
p[i] = m.addParam(a, "p"+to_string(i));
}
// Constraints
//
//
// expr = expression build from constants, variables MadOpt::Var, parameters MadOpt::Param
// lb = lower bound, double
// ub = upper bound, double
//
// add constraint of type:
//
// lb <= expr <= ub
// m.addConstr(lb, expr, ub);
//
// lb <= expr <= INF
// m.addConstr(lb, expr);
//
// INF <= expr <= ub
// m.addConstr(expr, ub);
//
// expr == eq
// m.addEqConstr(expr, eq);
vector<MadOpt::Constraint> c(N-2);
for (int i=0; i<N-2; i++){
c[i] = m.addEqConstr((MadOpt::pow(x[i+1], 2) + 1.5*x[i+1] - p[i])*MadOpt::cos(x[i+2]) - x[i], 0);
}
// Objective
//
//
MadOpt::Expr obj(0);
for (int i=0; i<N; i++)
obj += MadOpt::pow(x[i] - 1, 2);
// set objective
m.setObj(obj);
// calling the solver
//
//
m.solve();
// check if a solution exists, or in other words if the solver succeeded
//
//
if (m.hasSolution()){
// access objective value
cout<<"Objective:"<<m.objValue()<<endl;
// access solver status == Ipopt status values
cout<<"Solver status:"<<m.status()<<endl;
//print solution value of first variable
cout<<"Solution value for x0:"<<x[0].x()<<endl;
//print lambda value of constraint, only available for IpoptModel
cout<<"Lambda value of constraint 0:"<<c[0].lam()<<endl;
}
//changing the model
//
//
// Variables
//change variable bounds and initial value
x[1].lb(-12);
x[1].ub(1);
x[1].init(0);
//access bounds and initial values
cout<<"Variable values: lb="<<x[1].lb()<<" ub="<<x[1].ub()<<" init="<<x[1].init()<<endl;
//Constraints
//change constraint bounds
c[0].lb(-1);
c[0].ub(2);
//access bounds
cout<<"Constraint bounds: lb="<<c[0].lb()<<" ub="<<c[0].ub()<<endl;
//Parameter
// change value
p[0].value(12);
// access value
cout<<"Parameter value:"<<p[0].value()<<endl;
// resolve
//
//
// set solution as initial values
m.solAsInit();
m.show_solver = false;
m.solve();
if (not m.hasSolution())
cout<<"no solution"<<endl;
// test for specific solver status
if (m.status()==MadOpt::Solution::LOCAL_INFEASIBILITY)
cout<<"Infeasible"<<endl;
return 0;
}