Coverage for docs\source\examples\checking_result_changes.py: 0%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

12 statements  

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 

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)) 

28 

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.