Coverage for docs/source/examples/checking_result_changes.py: 92%
12 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 15:33 -0500
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 15:33 -0500
1"Example code for solution saving and differencing."
2import pickle
3from gpkit import Model, Variable
5# build model (dummy)
6# decision variable
7x = Variable("x")
8y = Variable("y")
10# objective and constraints
11objective = 0.23 + x/y # minimize x and y
12constraints = [x + y <= 5, x >= 1, y >= 2]
14# create model
15m = Model(objective, constraints)
17# solve the model
18# verbosity is 0 for testing's sake, no need to do that in your code!
19sol = m.solve(verbosity=0)
21# save the current state of the model
22sol.save("last_verified.sol")
24# uncomment the line below to verify a new model
25last_verified_sol = pickle.load(open("last_verified.sol", mode="rb"))
26if not sol.almost_equal(last_verified_sol, reltol=1e-3):
27 print(last_verified_sol.diff(sol))
29# Note you can replace the last three lines above with
30# print(sol.diff("last_verified.sol"))
31# if you don't mind doing the diff in that direction.