def test_no_index(self): if pd is None: self.skipTest("pandas not available") df = DataFrame([], ["x", "y"]) x = [1, 2, 3] y = [4, 5, 6] df.set_column("x", x) df.set_column("y", y) with self.assertRaises(ValueError): df.to_dict() pd_df = df.to_pandas() self.assertEqual(list(pd_df["x"]), x) self.assertEqual(list(pd_df["y"]), y)
def main(argc, argv): from amplpy import AMPL, DataFrame os.chdir(os.path.dirname(__file__) or os.curdir) # Create an AMPL instance ampl = AMPL() """ # If the AMPL installation directory is not in the system search path: from amplpy import Environment ampl = AMPL( Environment('full path to the AMPL installation directory')) """ ampl.eval("set CITIES; set LINKS within (CITIES cross CITIES);") ampl.eval("param cost {LINKS} >= 0; param capacity {LINKS} >= 0;") ampl.eval("data; set CITIES := PITT NE SE BOS EWR BWI ATL MCO;") cost = [2.5, 3.5, 1.7, 0.7, 1.3, 1.3, 0.8, 0.2, 2.1] capacity = [250, 250, 100, 100, 100, 100, 100, 100, 100] links_from = ["PITT", "PITT", "NE", "NE", "NE", "SE", "SE", "SE", "SE"] links_to = ["NE", "SE", "BOS", "EWR", "BWI", "EWR", "BWI", "ATL", "MCO"] # Using amplpy.DataFrame df = DataFrame(("LINKSFrom", "LINKSTo"), ("cost", "capacity")) df.set_column("LINKSFrom", links_from) df.set_column("LINKSTo", links_to) df.set_column("cost", cost) df.set_column("capacity", capacity) print(df) ampl.set_data(df, "LINKS") ampl.display("LINKS") # Using pandas.DataFrame (recommended) df = pd.DataFrame( list(zip(links_from, links_to, cost, capacity)), columns=["LINKSFrom", "LINKSTo", "cost", "capacity"], ).set_index(["LINKSFrom", "LINKSTo"]) print(df) ampl.eval("reset data LINKS;") ampl.set_data(df, "LINKS") ampl.display("LINKS")
def test_dataframe(self): # Create first dataframe (for data indexed over NUTR) # Add data row by row df1 = DataFrame("NUTR", ("n_min", "n_max")) df1.add_row(("A", 700, 20000)) df1.add_row(("B1", 700, 20000)) df1.add_row(("B2", 700, 20000)) df1.add_row(("C", 700, 20000)) df1.add_row(("CAL", 16000, 24000)) df1.add_row(("NA", 0.0, 50000)) # Create second dataframe (for data indexed over FOOD) # Add column by column df2 = DataFrame("FOOD") foods = ["BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR"] df2.set_column("FOOD", foods) self.assertEqual(list(df2.get_column("FOOD")), foods) contents = [2] * 8 df2.add_column("f_min", contents) self.assertEqual(list(df2.get_column("f_min")), contents) contents = [10] * 8 df2.add_column("f_max", contents) self.assertEqual(list(df2.get_column("f_max")), contents) costs = [3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49] df2.add_column("cost", costs) self.assertEqual(list(df2.get_column("cost")), costs) labels = [random.choice(string.ascii_letters)] * 8 df2.add_column("labels", labels) self.assertEqual(list(df2.get_column("labels")), labels) df2.add_column("empty", []) self.assertEqual(list(df2.get_column("empty")), [None] * 8) print(df2.get_column("FOOD")) for index in df2.get_column("FOOD"): print(df2.get_row(index)) # Create third dataframe, to assign data to the AMPL entity # param amt{NUTR, FOOD}; df3 = DataFrame(("NUTR", "FOOD")) # Populate the set columns nutr_with_multiplicity = [""] * 48 food_with_multiplicity = [""] * 48 i = 0 for n in range(6): for f in range(8): print(df1.get_row_by_index(n)[0]) nutr_with_multiplicity[i] = df1.get_row_by_index(n)[0] food_with_multiplicity[i] = foods[f] i += 1 df3.set_column("NUTR", nutr_with_multiplicity) df3.set_column("FOOD", food_with_multiplicity) # Populate with all these values values = [ 60, 8, 8, 40, 15, 70, 25, 60, 10, 20, 15, 35, 15, 15, 25, 15, 15, 20, 10, 10, 15, 15, 15, 10, 20, 0, 10, 40, 35, 30, 50, 20, 295, 770, 440, 430, 315, 400, 370, 450, 968, 2180, 945, 278, 1182, 896, 1329, 1397, ] df3.add_column("amt", values)
def main(argc, argv): # You can install amplpy with "python -m pip install amplpy" from amplpy import AMPL, DataFrame os.chdir(os.path.dirname(__file__) or os.curdir) # Note: If you want to perform data transformations use pandas dataframes. # amplpy dataframes are simple dataframes for data communication only. # Create first dataframe (for data indexed over NUTR) # Add data row by row df1 = DataFrame("NUTR", ("n_min", "n_max")) df1.add_row("A", 700, 20000) df1.add_row("B1", 700, 20000) df1.add_row("B2", 700, 20000) df1.add_row("C", 700, 20000) df1.add_row("CAL", 16000, 24000) df1.add_row("NA", 0.0, 50000) # Create second dataframe (for data indexed over FOOD) # Add column by column df2 = DataFrame("FOOD") foods = ["BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR"] df2.set_column("FOOD", foods) contents = [2] * 8 df2.add_column("f_min", contents) contents = [10] * 8 df2.add_column("f_max", contents) costs = [3.19, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49] df2.add_column("cost", costs) # Create third dataframe, to assign data to the AMPL entity # param amt{NUTR, FOOD}; df3 = DataFrame(index=("NUTR", "FOOD")) # Populate the set columns nutr_with_multiplicity = [""] * 48 food_with_multiplicity = [""] * 48 i = 0 for n in range(6): for f in range(8): nutr_with_multiplicity[i] = df1.get_row_by_index(n)[0] food_with_multiplicity[i] = foods[f] i += 1 df3.set_column("NUTR", nutr_with_multiplicity) df3.set_column("FOOD", food_with_multiplicity) # Populate with all these values values = [ 60, 8, 8, 40, 15, 70, 25, 60, 10, 20, 15, 35, 15, 15, 25, 15, 15, 20, 10, 10, 15, 15, 15, 10, 20, 0, 10, 40, 35, 30, 50, 20, 295, 770, 440, 430, 315, 400, 370, 450, 968, 2180, 945, 278, 1182, 896, 1329, 1397, ] df3.add_column("amt", values) # Create an AMPL instance ampl = AMPL() if argc > 1: ampl.set_option("solver", argv[1]) # Read the model file model_directory = argv[2] if argc == 3 else os.path.join("..", "models") ampl.read(os.path.join(model_directory, "diet/diet.mod")) # Assign data to NUTR, n_min and n_max ampl.set_data(df1, "NUTR") # Assign data to FOOD, f_min, f_max and cost ampl.set_data(df2, "FOOD") # Assign data to amt ampl.set_data(df3) # Solve the model ampl.solve() # Print out the result print( "Objective function value: {}".format(ampl.get_objective("Total_Cost").value()) ) # Get the values of the variable Buy in a dataframe results = ampl.get_variable("Buy").get_values() # Print print(results)