|
| 1 | +import numpy as np |
| 2 | +import highspy |
| 3 | + |
| 4 | +hscb = highspy.cb |
| 5 | + |
| 6 | +h = highspy.Highs() |
| 7 | +h.setOptionValue("output_flag", False); |
| 8 | + |
| 9 | +inf = highspy.kHighsInf |
| 10 | +lp = highspy.HighsLp() |
| 11 | +lp.num_col_ = 2 |
| 12 | +lp.num_row_ = 3 |
| 13 | +lp.col_cost_ = np.array([0, 0], dtype=np.double) |
| 14 | +lp.col_lower_ = np.array([0, 0], dtype=np.double) |
| 15 | +lp.col_upper_ = np.array([inf, inf], dtype=np.double) |
| 16 | +lp.row_lower_ = np.array([-inf, -inf, -inf], dtype=np.double) |
| 17 | +lp.row_upper_ = np.array([18, 8, 14], dtype=np.double) |
| 18 | +lp.a_matrix_.start_ = np.array([0, 3, 6]) |
| 19 | +lp.a_matrix_.index_ = np.array([0, 1, 2, 0, 1, 2]) |
| 20 | +lp.a_matrix_.value_ = np.array([3, 1, 1, 1, 1, 2], dtype=np.double) |
| 21 | +h.passModel(lp) |
| 22 | +# LP constraints are |
| 23 | +# |
| 24 | +# 3x + y <= 18 |
| 25 | +# |
| 26 | +# x + y <= 8 |
| 27 | +# |
| 28 | +# x + 2y <= 14 |
| 29 | +linear_objective0 = highspy.HighsLinearObjective() |
| 30 | +linear_objective0.weight = -1 |
| 31 | +linear_objective0.offset = -1 |
| 32 | +linear_objective0.coefficients = np.array([1, 1], dtype=np.double) |
| 33 | +linear_objective0.abs_tolerance = 0 |
| 34 | +linear_objective0.rel_tolerance = 0 |
| 35 | +linear_objective0.priority = 10 |
| 36 | + |
| 37 | +linear_objective1 = highspy.HighsLinearObjective() |
| 38 | +linear_objective1.weight = 1e-4 |
| 39 | +linear_objective1.offset = 0 |
| 40 | +linear_objective1.coefficients = np.array([1, 0], dtype=np.double) |
| 41 | +linear_objective1.abs_tolerance = -1 |
| 42 | +linear_objective1.rel_tolerance = -1 |
| 43 | +linear_objective1.priority = 0 |
| 44 | + |
| 45 | +h.addLinearObjective(linear_objective0) |
| 46 | +h.addLinearObjective(linear_objective1) |
| 47 | +# Objectives |
| 48 | +# |
| 49 | +# f0: x + y - 1 (weight -1) |
| 50 | +# |
| 51 | +# f1: x (weight 1e-4) |
| 52 | +# |
| 53 | +# With objective min -f0 (since its weight is -1) the LP has nonunique |
| 54 | +# optimal solutions on the line joining (2, 6) and (5, 3) |
| 55 | +# |
| 56 | +# Blending -f0 + 1e-4 f1 minimizes x along the line joining (2, 6) and |
| 57 | +# (5, 3) to give optimal solution at (2, 6) with objective -7 |
| 58 | +h.run() |
| 59 | +solution = h.getSolution() |
| 60 | +print(f"Solution: ({solution.col_value[0]}, {solution.col_value[1]}) for min -f0 + 1e-4 f1") |
| 61 | + |
| 62 | +# Switch to lexicographic optimization |
| 63 | +h.setOptionValue("blend_multi_objectives", False); |
| 64 | + |
| 65 | +linear_objective0.coefficients = np.array([1.0001, 1], dtype=np.double) |
| 66 | +linear_objective0.abs_tolerance = 1e-5; |
| 67 | +linear_objective0.rel_tolerance = 0.05; |
| 68 | +linear_objective1.weight = 1e-3 |
| 69 | + |
| 70 | +h.clearLinearObjectives() |
| 71 | +h.addLinearObjective(linear_objective0) |
| 72 | +h.addLinearObjective(linear_objective1) |
| 73 | +# Objectives |
| 74 | +# |
| 75 | +# f0 = 1.0001 x + y - 1 (with priority 10 and absolute tolerance 1e-5) |
| 76 | +# |
| 77 | +# f1 = x (with priority 0) |
| 78 | +# |
| 79 | +# Lexicographically: HiGHS |
| 80 | +# |
| 81 | +# minimizes f0 (to give objective 7.0005) |
| 82 | +# |
| 83 | +# adds a constraint that |
| 84 | +# |
| 85 | +# 1.0001 x + y - 1 >= 7.0005 - 1e-5 => 1.0001 x + y >= 8.00049 |
| 86 | +# |
| 87 | +# to ensure that the initial objective is within 1e-5 of its optimal |
| 88 | +# value |
| 89 | +# |
| 90 | +# minimizes f1 to give optimal solution at (4.90000, 3.10000) - where |
| 91 | +# x + y = 8 and 1.0001 x + y = 8.00049 |
| 92 | + |
| 93 | +h.run() |
| 94 | +solution = h.getSolution() |
| 95 | +print(f"Solution: ({solution.col_value[0]}, {solution.col_value[1]}) for min f1 and 1.0001 x + y >= 8.00049") |
| 96 | + |
| 97 | +linear_objective0.abs_tolerance = -1; |
| 98 | +h.clearLinearObjectives() |
| 99 | +h.addLinearObjective(linear_objective0) |
| 100 | +h.addLinearObjective(linear_objective1) |
| 101 | +# Objectives as before, but absolute tolerence for f0 now -1 (negative |
| 102 | +# => ignored) so relative tolerance of 0.05 is used |
| 103 | +# |
| 104 | +# Lexicographically: HiGHS |
| 105 | +# |
| 106 | +# minimizes f0 (to give objective 7.0005) |
| 107 | +# |
| 108 | +# adds a constraint that |
| 109 | +# |
| 110 | +# 1.0001 x + y - 1 >= 7.0005 * 0.95 => 1.0001 x + y >= 7.650475 |
| 111 | +# |
| 112 | +# to ensure that the initial objective 95% of its optimal value |
| 113 | +# |
| 114 | +# minimizes f1 to give optimal solution at (1.30069, 6.34966) - where |
| 115 | +# x + y = 8 and 1.0001 x + y = 7.650475 |
| 116 | +h.run() |
| 117 | +solution = h.getSolution() |
| 118 | +print(f"Solution: ({solution.col_value[0]}, {solution.col_value[1]}) for min f1 and 1.0001 x + y = 7.650475") |
| 119 | + |
| 120 | + |
0 commit comments