Skip to content

Commit 2b7f928

Browse files
committed
Add test for parameter in nlp
1 parent e0459dd commit 2b7f928

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/test_nlp.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,46 @@ def all_nlfuncs(vars):
134134
v = f(x_value)
135135
correct_con_values.append(v)
136136
assert con_values == pytest.approx(correct_con_values)
137+
138+
139+
def test_nlp_param():
140+
if not ipopt.is_library_loaded():
141+
pytest.skip("Ipopt library is not loaded")
142+
143+
model = ipopt.Model()
144+
145+
N = 10
146+
xs = []
147+
for i in range(N):
148+
x = model.add_variable(lb=0.0, ub=10.0, start=1.0)
149+
xs.append(x)
150+
151+
def obj(vars):
152+
return poi.exp(vars[0])
153+
154+
obj_f = model.register_function(obj, var=1, name="obj")
155+
156+
for i in range(N):
157+
model.add_nl_objective(obj_f, [xs[i]])
158+
159+
def con(vars, params):
160+
x = vars[0]
161+
p = params[0]
162+
return x * (p + 1) * (p + 1)
163+
164+
con_f = model.register_function(con, var=1, param=1, name="con")
165+
166+
for i in range(N):
167+
model.add_nl_constraint(con_f, [xs[i]], [i], poi.Geq, [1.0])
168+
169+
model.optimize()
170+
171+
assert (
172+
model.get_model_attribute(poi.ModelAttribute.TerminationStatus)
173+
== poi.TerminationStatusCode.LOCALLY_SOLVED
174+
)
175+
176+
x_values = [model.get_value(x) for x in xs]
177+
correct_x_values = [1.0 / (i + 1) / (i + 1) for i in range(N)]
178+
179+
assert x_values == pytest.approx(correct_x_values)

0 commit comments

Comments
 (0)