Example #1
0
 def _testFantop(self, modelType):
     sln, draft_yield = _testFantop(modelType, "sample_data.sql")
     self.assertTrue(sln and nearlySame(draft_yield, 2988.61))
     sln, draft_yield = _testFantop(modelType,
                                    "sample_tweaked_most_importants.sql")
     self.assertTrue(sln and nearlySame(draft_yield, 2947.677))
     sln, draft_yield = _testFantop(modelType, "flex_constraint.sql")
     self.assertTrue(sln and nearlySame(draft_yield, 2952.252))
Example #2
0
 def _same_table(t1, t2):
     if not set(t1) == set(t2):
         return False
     for k in t1:
         for f in t1[k]:
             if not nearlySame(t1[k][f], t2[k][f]):
                 return False
     return True
Example #3
0
 def testNetflow_oplrunRequired(self):
     self.assertTrue(_can_run_oplrun_tests)
     in_tdf = TicDatFactory(**netflowSchema())
     in_tdf.enable_foreign_key_links()
     soln_tdf = TicDatFactory(flow=[["source", "destination", "commodity"],
                                    ["quantity"]],
                              parameters=[["paramKey"], ["value"]])
     dat = in_tdf.TicDat(
         **
         {t: getattr(netflowData(), t)
          for t in in_tdf.primary_key_fields})
     opl_soln = opl_run(get_testing_file_path("sample_netflow.mod"), in_tdf,
                        dat, soln_tdf)
     self.assertTrue(
         nearlySame(opl_soln.parameters["Total Cost"]["value"], 5500))
     self.assertTrue(
         nearlySame(
             opl_soln.flow["Pens", "Detroit", "New York"]["quantity"], 30))
Example #4
0
 def testNetflow_runlingoRequired(self):
     self.assertTrue(tlingo._can_run_lingo_run_tests)
     in_tdf = TicDatFactory(**netflowSchema())
     in_tdf.add_foreign_key("arcs", "nodes", ['source', 'name'])
     in_tdf.add_foreign_key("arcs", "nodes", ['destination', 'name'])
     in_tdf.add_foreign_key("cost", "nodes", ['source', 'name'])
     in_tdf.add_foreign_key("cost", "nodes", ['destination', 'name'])
     in_tdf.add_foreign_key("cost", "commodities", ['commodity', 'name'])
     in_tdf.add_foreign_key("inflow", "commodities", ['commodity', 'name'])
     in_tdf.add_foreign_key("inflow", "nodes", ['node', 'name'])
     solution_variables = TicDatFactory(
         flow=[["Commodity", "Source", "Destination"], ["quantity"]])
     dat = in_tdf.TicDat(
         **
         {t: getattr(netflowData(), t)
          for t in in_tdf.primary_key_fields})
     lingo_soln = tlingo.lingo_run(
         get_testing_file_path("sample_netflow.lng"), in_tdf, dat,
         solution_variables)
     self.assertTrue(
         nearlySame(
             lingo_soln.flow["Pens", "Detroit", "New York"]["quantity"],
             30))
Example #5
0
 def feas(sln, dat):
     one_way_price = 2.25
     self.assertTrue("One Way Price" not in dat.parameters)
     sub_totals = defaultdict(int)
     price_needed = {
         k: one_way_price * k[0]
         for k in sln.load_amount_summary
     }
     for k, v in sln.load_amount_details.items():
         price_needed[k[:2]] -= k[-1] * v.values()[0]
         sub_totals[k[:2]] += v.values()[0]
     self.assertTrue(
         sub_totals ==
         {k: v.values()[0]
          for k, v in sln.load_amount_summary.items()})
     if "Amount Leftover Constraint" in dat.parameters and\
         dat.parameters["Amount Leftover Constraint"]["Value"] == "Equality":
         self.assertTrue(
             all(nearlySame(k[1], -v) for k, v in price_needed.items()))
     else:
         self.assertTrue(
             all(k[1] >= -v and v <= 0
                 for k, v in price_needed.items()))
Example #6
0
 def _testNetflow(self, modelType):
     sln, cost = netflowSolver(modelType)
     self.assertTrue(sln)
     self.assertTrue(nearlySame(cost, 5500.0))
Example #7
0
 def _testDiet(self, modelType):
     sln, cost = dietSolver(modelType)
     self.assertTrue(sln)
     self.assertTrue(nearlySame(cost, 11.8289))
Example #8
0
 def testDiet_runlingoRequired(self):
     self.assertTrue(tlingo._can_run_lingo_run_tests)
     diet_schema = {
         "categories": (("Name", ), ["Min Nutrition", "Max Nutrition"]),
         "foods": [["Name"], ("Cost", )],
         "nutrition_quantities": (["Food", "Category"], ["Quantity"])
     }
     # Using foods as the table name to test the lingo_prepend functionality
     in_tdf = TicDatFactory(**diet_schema)
     in_tdf.lingo_prepend = "inp_"
     solution_variables = TicDatFactory(foods=[["Food"], ["Quantity"]],
                                        total_cost=[["Value"], []])
     solution_variables.lingo_prepend = "out_"  # avoid table name collisions
     makeDat = lambda: in_tdf.TicDat(categories={
         'calories': [1800, 2200],
         'protein': [91, float("inf")],
         'fat': [0, 65],
         'sodium': [0, 1779]
     },
                                     foods={
                                         'hamburger': 2.49,
                                         'chicken': 2.89,
                                         'hot dog': 1.50,
                                         'fries': 1.89,
                                         'macaroni': 2.09,
                                         'pizza': 1.99,
                                         'salad': 2.49,
                                         'milk': 0.89,
                                         'ice cream': 1.59
                                     },
                                     nutrition_quantities=
                                     [('hamburger', 'calories', 410),
                                      ('hamburger', 'protein', 24),
                                      ('hamburger', 'fat', 26),
                                      ('hamburger', 'sodium', 730),
                                      ('chicken', 'calories', 420),
                                      ('chicken', 'protein', 32),
                                      ('chicken', 'fat', 10),
                                      ('chicken', 'sodium', 1190),
                                      ('hot dog', 'calories', 560),
                                      ('hot dog', 'protein', 20),
                                      ('hot dog', 'fat', 32),
                                      ('hot dog', 'sodium', 1800),
                                      ('fries', 'calories', 380),
                                      ('fries', 'protein', 4),
                                      ('fries', 'fat', 19),
                                      ('fries', 'sodium', 270),
                                      ('macaroni', 'calories', 320),
                                      ('macaroni', 'protein', 12),
                                      ('macaroni', 'fat', 10),
                                      ('macaroni', 'sodium', 930),
                                      ('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)])
     in_tdf.add_foreign_key("nutrition_quantities", "foods",
                            ['Food', 'Name'])
     in_tdf.add_foreign_key("nutrition_quantities", "categories",
                            ['Category', 'Name'])
     lingo_soln = tlingo.lingo_run(get_testing_file_path("sample_diet.lng"),
                                   in_tdf, makeDat(), solution_variables)
     self.assertTrue(
         nearlySame(lingo_soln.foods["hamburger"]["Quantity"],
                    0.6045,
                    epsilon=0.0001))
     self.assertIsNotNone(lingo_soln.total_cost["11.82886"])
     # lingo_run should not complete when there is an infeasible solution
     dat = makeDat()
     dat.categories["calories"][
         "Min Nutrition"] = dat.categories["calories"]["Max Nutrition"] + 1
     try:
         lingo_soln = tlingo.lingo_run(
             get_testing_file_path("sample_diet.lng"), in_tdf, dat,
             solution_variables)
         self.assertTrue(False)
     except:
         self.assertTrue(True)
Example #9
0
 def testDiet_oplrunRequired(self):
     self.assertTrue(_can_run_oplrun_tests)
     diet_schema = {
         "categories": (("Name", ), ["Min Nutrition", "Max Nutrition"]),
         "foods": [["Name"], ("Cost", )],
         "nutritionQuantities": (["Food", "Category"], ["Qty"])
     }
     in_tdf = TicDatFactory(**diet_schema)
     soln_tdf = TicDatFactory(parameters=[["Parameter Name"],
                                          ["Parameter Value"]],
                              buy_food=[["Food"], ["Qty"]],
                              consume_nutrition=[["Category"], ["Qty"]])
     makeDat = lambda: in_tdf.TicDat(categories={
         'calories': [1800, 2200],
         'protein': [91, float("inf")],
         'fat': [0, 65],
         'sodium': [0, 1779]
     },
                                     foods={
                                         'hamburger': 2.49,
                                         'chicken': 2.89,
                                         'hot dog': 1.50,
                                         'fries': 1.89,
                                         'macaroni': 2.09,
                                         'pizza': 1.99,
                                         'salad': 2.49,
                                         'milk': 0.89,
                                         'ice cream': 1.59
                                     },
                                     nutritionQuantities=
                                     [('hamburger', 'calories', 410),
                                      ('hamburger', 'protein', 24),
                                      ('hamburger', 'fat', 26),
                                      ('hamburger', 'sodium', 730),
                                      ('chicken', 'calories', 420),
                                      ('chicken', 'protein', 32),
                                      ('chicken', 'fat', 10),
                                      ('chicken', 'sodium', 1190),
                                      ('hot dog', 'calories', 560),
                                      ('hot dog', 'protein', 20),
                                      ('hot dog', 'fat', 32),
                                      ('hot dog', 'sodium', 1800),
                                      ('fries', 'calories', 380),
                                      ('fries', 'protein', 4),
                                      ('fries', 'fat', 19),
                                      ('fries', 'sodium', 270),
                                      ('macaroni', 'calories', 320),
                                      ('macaroni', 'protein', 12),
                                      ('macaroni', 'fat', 10),
                                      ('macaroni', 'sodium', 930),
                                      ('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)])
     opl_soln = opl_run(get_testing_file_path("sample_diet.mod"), in_tdf,
                        makeDat(), soln_tdf)
     self.assertTrue(
         nearlySame(opl_soln.parameters["Total Cost"]["Parameter Value"],
                    11.829,
                    epsilon=0.0001))
     self.assertTrue(
         nearlySame(opl_soln.consume_nutrition["protein"]["Qty"],
                    91,
                    epsilon=0.0001))
     # opl_run should not complete when there is an infeasible solution
     dat = makeDat()
     dat.categories["calories"][
         "Min Nutrition"] = dat.categories["calories"]["Max Nutrition"] + 1
     try:
         opl_soln = opl_run(get_testing_file_path("sample_diet.mod"),
                            in_tdf, dat, soln_tdf)
         self.assertTrue(False)
     except:
         self.assertTrue(True)