|
| 1 | +import pyoptinterface as poi |
| 2 | +from pyoptinterface import nl |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +def test_hs071(nlp_model_ctor): |
| 7 | + model = nlp_model_ctor() |
| 8 | + |
| 9 | + x = model.add_m_variables(4, lb=1.0, ub=5.0, name="x") |
| 10 | + |
| 11 | + model.set_variable_attribute(x[0], poi.VariableAttribute.PrimalStart, 1.0) |
| 12 | + model.set_variable_attribute(x[1], poi.VariableAttribute.PrimalStart, 5.0) |
| 13 | + model.set_variable_attribute(x[2], poi.VariableAttribute.PrimalStart, 5.0) |
| 14 | + model.set_variable_attribute(x[3], poi.VariableAttribute.PrimalStart, 1.0) |
| 15 | + |
| 16 | + with nl.graph(): |
| 17 | + model.add_nl_objective(x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]) |
| 18 | + |
| 19 | + with nl.graph(): |
| 20 | + model.add_nl_constraint(x[0] * x[1] * x[2] * x[3] >= 25.0) |
| 21 | + model.add_quadratic_constraint( |
| 22 | + x[0] ** 2 + x[1] ** 2 + x[2] ** 2 + x[3] ** 2 == 40.0 |
| 23 | + ) |
| 24 | + |
| 25 | + model.optimize() |
| 26 | + |
| 27 | + x1_val = model.get_value(x[0]) |
| 28 | + x2_val = model.get_value(x[1]) |
| 29 | + x3_val = model.get_value(x[2]) |
| 30 | + x4_val = model.get_value(x[3]) |
| 31 | + |
| 32 | + sum_sq = x1_val**2 + x2_val**2 + x3_val**2 + x4_val**2 |
| 33 | + product = x1_val * x2_val * x3_val * x4_val |
| 34 | + assert sum_sq == pytest.approx(40.0, rel=1e-6) |
| 35 | + assert 25.0 - 1e-6 <= product <= 100.0 + 1e-6 |
| 36 | + |
| 37 | + objective_value = model.get_model_attribute(poi.ModelAttribute.ObjectiveValue) |
| 38 | + assert objective_value == pytest.approx(17.014, rel=1e-3) |
0 commit comments