Example #1
0
 def test_gurobi_solve(self):
     dat = input_schema.csv.create_tic_dat('diet_sample_data')
     dat.parameters['How To Make Solution']['Value'] = "Use Gurobi"
     with self.assertRaises(
             DietException,
             msg="Gurobi should not be present, causing solve to fail"):
         solve(dat)
Example #2
0
# Read/write from Excel file. Analgous to this file. https://bit.ly/2S4Xvyo
from diet import solve, input_schema, solution_schema

dat = input_schema.xls.create_tic_dat(
    "diet.xls")  # Just look for the diet.xls file in the current directory

sln = solve(dat)

if sln:  # if the solve succeeds, write back to the current directory
    solution_schema.xls.write_file(sln, "solution.xls", allow_overwrite=True)
Example #3
0
        ('pizza', 'calories'): 320,
        ('pizza', 'protein'): 15,
        ('pizza', 'fat'): 12,
        ('pizza', 'sodium'): 820,
        ('salad', 'calories'): 320,
        ('salad', 'protein'): 31,
        ('salad', 'fat'): 12,
        ('salad', 'sodium'): 1230,
        ('milk', 'calories'): 100,
        ('milk', 'protein'): 8,
        ('milk', 'fat'): 2.5,
        ('milk', 'sodium'): 125,
        ('ice cream', 'calories'): 330,
        ('ice cream', 'protein'): 8,
        ('ice cream', 'fat'): 10,
        ('ice cream', 'sodium'): 180
    })

solution = solve(dat)

if solution:
    print('\nCost: %g' % solution.parameters['Total Cost']['Value'])
    print('\nBuy:')
    for f, b in solution.buy_food.items():
        print('%s %g' % (f, b["Quantity"]))
    print('\nNutrition:')
    for c, n in solution.consume_nutrition.items():
        print('%s %g' % (c, n["Quantity"]))
else:
    print('\nNo solution')
Example #4
0
 def test_matching_dataset(self):
     dat = input_schema.csv.create_tic_dat('diet_sample_data')
     sln = solve(dat)
     self.assertTrue(
         sln.parameters['Total Cost']['Value'] == 11.828861111111111,
         msg='The matching dataset should generate the cached solution')
Example #5
0
 def test_bad_parameter(self):
     dat = input_schema.csv.create_tic_dat('diet_sample_data')
     dat.parameters['How To Make Solution']['Value'] = "Use Pete"
     with self.assertRaises(AssertionError,
                            msg="Bad parameters should be caught"):
         solve(dat)
Example #6
0
 def test_data_integrity_errors(self):
     dat = input_schema.csv.create_tic_dat('diet_dirty_sample_data')
     with self.assertRaises(AssertionError,
                            msg="A dirty dat should fail assertions"):
         solve(dat)
Example #7
0
 def test_different_primary_keys(self):
     dat = input_schema.csv.create_tic_dat('diet_sample_data')
     dat.foods['brocolli']['Cost'] = 2.25
     with self.assertRaises(DietException,
                            msg="Different pks should be detected"):
         solve(dat)
Example #8
0
 def test_different_values(self):
     dat = input_schema.csv.create_tic_dat('diet_sample_data')
     dat.foods['ice cream']['Cost'] = 1.60
     with self.assertRaises(DietException,
                            msg="Different values should be detected"):
         solve(dat)