|
| 1 | +# Multi-objective binary knapsack example |
| 2 | +from highspy import Highs, HighsLinearObjective |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +# Parameters |
| 6 | +capacity = 13 |
| 7 | + |
| 8 | +# chosen such that applying each objective lexicographically gives a different solution |
| 9 | +profit = np.asarray([ |
| 10 | + [0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1], |
| 11 | + [1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0], |
| 12 | + [1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0], |
| 13 | + [1,1,1,1,0,1,0,0,1,1,0,0,0,1,1,1,0,0,0,0], |
| 14 | + |
| 15 | + ], dtype=np.float64).reshape(4, 20) |
| 16 | + |
| 17 | +OBJECTIVES = profit.shape[0] |
| 18 | +ITEMS = profit.shape[1] |
| 19 | + |
| 20 | +def pretty_print(objective_values): |
| 21 | + return '[ ' + ', '.join(map(lambda x: "{:2.0f}".format(x), objective_values)) + ' ]' |
| 22 | + |
| 23 | +# |
| 24 | +# individual optimization of each objective |
| 25 | +def individual_objectives(h, X): |
| 26 | + print('## Individual:\n') |
| 27 | + objective_values = [] |
| 28 | + |
| 29 | + for k in range(OBJECTIVES): |
| 30 | + h.maximize(np.dot(X, profit[k,:])) |
| 31 | + print(f' Obj {k+1}: [{"".join(map(lambda x: "{:1.0f}".format(x), profit[k,:]))}]') |
| 32 | + print(f' Sol {k+1}: [{"".join(map(lambda x: "{:1.0f}".format(x), abs(h.vals(X))))}]\n') |
| 33 | + objective_values.append(h.getObjectiveValue()) |
| 34 | + |
| 35 | + print(f' OBJ: {pretty_print(objective_values)}\n\n') |
| 36 | + |
| 37 | +# |
| 38 | +# manual implementation of lexicographic multi-objective optimization |
| 39 | +def manual_lexicographic(h, X): |
| 40 | + print('## Manual lexicographic:\n') |
| 41 | + cons = [] |
| 42 | + objs = np.dot(profit, X) |
| 43 | + |
| 44 | + for k in range(OBJECTIVES): |
| 45 | + h.maximize(objs[k]) |
| 46 | + print(f' Obj {k+1}: [{"".join(map(lambda x: "{:1.0f}".format(x), profit[k,:]))}]') |
| 47 | + print(f' Sol {k+1}: [{"".join(map(lambda x: "{:1.0f}".format(x), abs(h.vals(X))))}]\n') |
| 48 | + |
| 49 | + # add constraint to ensure next solution are at least as good in this objective |
| 50 | + cons.append(h.addConstr(np.dot(profit[k,:], X) >= h.getObjectiveValue())) |
| 51 | + |
| 52 | + objective_values = h.vals(objs) |
| 53 | + h.deleteRows(len(cons), cons) # clean up constraints |
| 54 | + print(f' SOL: [{"".join(map(lambda x: "{:1.0f}".format(x), abs(h.vals(X))))}]') |
| 55 | + print(f' OBJ: {pretty_print(objective_values)}\n\n') |
| 56 | + |
| 57 | + |
| 58 | +# |
| 59 | +# built-in lexicographic multi-objective optimization |
| 60 | +def highs_lexicographic(h, X): |
| 61 | + print('## Built-in lexicographic:\n') |
| 62 | + h.setOptionValue('blend_multi_objectives', False) # use lexicographic |
| 63 | + |
| 64 | + for k in range(OBJECTIVES): |
| 65 | + obj = HighsLinearObjective() |
| 66 | + obj.coefficients = profit[k,:].tolist() |
| 67 | + obj.weight = -1 # maximize |
| 68 | + obj.priority = -k # higher priority for lower k |
| 69 | + obj.abs_tolerance = 0.01 |
| 70 | + obj.rel_tolerance = 0.001 |
| 71 | + |
| 72 | + h.addLinearObjective(obj) |
| 73 | + |
| 74 | + h.solve() |
| 75 | + print(f' SOL: [{"".join(map(lambda x: "{:1.0f}".format(x), abs(h.vals(X))))}]') |
| 76 | + print(f' OBJ: {pretty_print(np.dot(profit, h.vals(X)))}\n\n') |
| 77 | + |
| 78 | + print(f' Number of objectives: {h.getNumLinearObjectives()}') |
| 79 | + for k in range(h.getNumLinearObjectives()): |
| 80 | + obj = h.getLinearObjective(k) |
| 81 | + print(f' Obj {k+1}: weight={obj.weight}, priority={obj.priority}, abs_tol={obj.abs_tolerance}, rel_tol={obj.rel_tolerance}') |
| 82 | + print('\n') |
| 83 | + |
| 84 | + h.clearLinearObjectives() |
| 85 | + |
| 86 | + |
| 87 | +# |
| 88 | +# manual implementation of weighted multi-objective optimization |
| 89 | +def manual_weighted(h, X): |
| 90 | + print('## Manual Weighted:\n') |
| 91 | + weights = np.asarray([1.0/(k+1) for k in range(OBJECTIVES)], dtype=np.float64) |
| 92 | + h.maximize(np.dot(weights[:,None] * profit, X).sum()) |
| 93 | + |
| 94 | + print(f' SOL: [{"".join(map(lambda x: "{:1.0f}".format(x), abs(h.vals(X))))}]') |
| 95 | + print(f' OBJ: {pretty_print(np.dot(profit, h.vals(X)))}\n') |
| 96 | + |
| 97 | + print(f' Obj: {h.getObjective()[0]}\n\n') |
| 98 | + |
| 99 | + |
| 100 | +# |
| 101 | +# built-in weighted multi-objective optimization |
| 102 | +def highs_weighted(h, X): |
| 103 | + print('## Built-in Weighted:\n') |
| 104 | + h.setOptionValue('blend_multi_objectives', True) # use weighted |
| 105 | + |
| 106 | + for k in range(OBJECTIVES): |
| 107 | + obj = HighsLinearObjective() |
| 108 | + obj.coefficients = profit[k,:].tolist() |
| 109 | + obj.weight = -1.0/(k+1) |
| 110 | + |
| 111 | + h.addLinearObjective(obj) |
| 112 | + |
| 113 | + h.solve() |
| 114 | + print(f' SOL: [{"".join(map(lambda x: "{:1.0f}".format(x), abs(h.vals(X))))}]') |
| 115 | + print(f' OBJ: {pretty_print(np.dot(profit, h.vals(X)))}\n') |
| 116 | + |
| 117 | + print(f' Number of objectives: {h.getNumLinearObjectives()}') |
| 118 | + for k in range(h.getNumLinearObjectives()): |
| 119 | + obj = h.getLinearObjective(k) |
| 120 | + print(f' Obj {k+1}: weight={obj.weight}, priority={obj.priority}, abs_tol={obj.abs_tolerance}, rel_tol={obj.rel_tolerance}') |
| 121 | + print('\n') |
| 122 | + |
| 123 | + h.clearLinearObjectives() |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + h = Highs() |
| 128 | + h.silent(True) |
| 129 | + |
| 130 | + X = h.addBinaries(ITEMS) |
| 131 | + h.addConstr(X.sum() <= capacity) |
| 132 | + |
| 133 | + individual_objectives(h, X) |
| 134 | + |
| 135 | + manual_lexicographic(h, X) |
| 136 | + manual_weighted(h, X) |
| 137 | + |
| 138 | + highs_lexicographic(h, X) |
| 139 | + highs_weighted(h, X) |
0 commit comments