Reputation: 203
I am implementing several optimization programs with pyomo.
So far I have written some integration tests with unittest
on optimal objective value and optimal variable values.
I would like to write unit tests for each objective and constraints to make sure they are correctly implemented. What's the correct way to do it?
Upvotes: 0
Views: 533
Reputation: 11938
There are a couple strategies here that might be useful, and I'd be curious if other folks have other contributions.
If you know the objective value for a particular scenario/dataset, that is a no-brainer to unit-test that value +/- some delta against your solution
If you have some constraint that should be binding, you can make up a set of inputs to ensure that it binds. Say for example you have a toy model that has 2 inputs: number of trucks and gallons of gas and the objective is to move some packages in the trucks, based on some logical constraints on truck capacity, number of trucks, gas for trips. You could:
as a baseline, give the model 1 truck and enough gas for 1 trip and
ensure the solution delivers 1 truckload of pkgs
add in a bunch of extra trucks and ensure the objective remains the same (bound by gas constraint)
put in 0 trucks / 0 gas and see that nothing happens. <-- Always good idea! :)
pkgs_delivered = sum(...)
trip_count = sum(...)
gas_used = sum(...)
w_1, w_2 = # some weights for penalties...
mdl.obj = pyo.Objective(expr= pkgs_delivered - w_1 * trip_count - w_2 * gas_used, sense=pyo.maximize)
Then you can peel apart these elements in your unit tests (if it makes sense) and check their values as they are pyomo
expressions
assertEqual(trip_count.value(), 8, 'this setup should have 8 trips')
Upvotes: 2