Coverage for docs/source/examples/checking_result_changes.py: 92%

13 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-07 22:56 -0500

1"Example code for solution saving and differencing." 

2import pickle 

3from gpkit import Model, Variable 

4 

5# build model (dummy) 

6# decision variable 

7x = Variable("x") 

8y = Variable("y") 

9 

10# objective and constraints 

11objective = 0.23 + x/y # minimize x and y 

12constraints = [x + y <= 5, x >= 1, y >= 2] 

13 

14# create model 

15m = Model(objective, constraints) 

16 

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) 

20 

21# save the current state of the model 

22sol.save("last_verified.sol") 

23 

24# uncomment the line below to verify a new model 

25with open("last_verified.sol", mode="rb") as f: 

26 last_verified_sol = pickle.load(f) 

27if not sol.almost_equal(last_verified_sol, reltol=1e-3): 

28 print(last_verified_sol.diff(sol)) 

29 

30# Note you can replace the last three lines above with 

31# print(sol.diff("last_verified.sol")) 

32# if you don't mind doing the diff in that direction.