Ejemplo n.º 1
0
# tries to balance the workload among the workers.
#

from ticdat import PanDatFactory, standard_main
try: # if you don't have amplpy installed, the code will still load and then fail on solve
    from amplpy import AMPL
except:
    AMPL = None
# ------------------------ define the input schema --------------------------------
input_schema = PanDatFactory(
    workers=[["Name"], ["Payment"]],
    shifts=[["Name"], ["Requirement"]],
    availability=[["Worker", "Shift"], []]
)
# Define the foreign key relationships
input_schema.add_foreign_key("availability", "workers", ['Worker', 'Name'])
input_schema.add_foreign_key("availability", "shifts", ['Shift', 'Name'])

# Define the data types
input_schema.set_data_type("workers", "Payment", min=0, max=float("inf"),
                           inclusive_min=True, inclusive_max=True)
input_schema.set_data_type("shifts", "Requirement", min=0, max=float("inf"),
                           inclusive_min=True, inclusive_max=True)
# ---------------------------------------------------------------------------------

# ------------------------ define the output schema -------------------------------
solution_schema = PanDatFactory(
    assignments=[["Worker", "Shift"], []],
    slacks = [["Shift"], ["Slack"]],
    total_shifts=[["Worker"], ["Total Number Of Shifts"]],
    parameters=[["Parameter"], ["Value"]]
Ejemplo n.º 2
0
    import gurobipy as gp
except:
    gp = None
from ticdat import PanDatFactory, Slicer

# ------------------------ define the input schema --------------------------------
input_schema = PanDatFactory(commodities=[["Name"], ["Volume"]],
                             nodes=[["Name"], []],
                             arcs=[["Source", "Destination"], ["Capacity"]],
                             cost=[["Commodity", "Source", "Destination"],
                                   ["Cost"]],
                             supply=[["Commodity", "Node"], ["Quantity"]],
                             demand=[["Commodity", "Node"], ["Quantity"]])

# Define the foreign key relationships
input_schema.add_foreign_key("arcs", "nodes", ['Source', 'Name'])
input_schema.add_foreign_key("arcs", "nodes", ['Destination', 'Name'])
input_schema.add_foreign_key(
    "cost", "arcs", [['Source', 'Source'], ['Destination', 'Destination']])
input_schema.add_foreign_key("cost", "commodities", ['Commodity', 'Name'])
input_schema.add_foreign_key("demand", "commodities", ['Commodity', 'Name'])
input_schema.add_foreign_key("demand", "nodes", ['Node', 'Name'])
input_schema.add_foreign_key("supply", "commodities", ['Commodity', 'Name'])
input_schema.add_foreign_key("supply", "nodes", ['Node', 'Name'])

# Define the data types
input_schema.set_data_type("commodities",
                           "Volume",
                           min=0,
                           max=float("inf"),
                           inclusive_min=False,
Ejemplo n.º 3
0
from ticdat import PanDatFactory, standard_main
try:  # if you don't have amplpy installed, the code will still load and then fail on solve
    from amplpy import AMPL
except:
    AMPL = None
# ------------------------ define the input schema --------------------------------
input_schema = PanDatFactory(commodities=[["Name"], ["Volume"]],
                             nodes=[["Name"], []],
                             arcs=[["Source", "Destination"], ["Capacity"]],
                             cost=[["Commodity", "Source", "Destination"],
                                   ["Cost"]],
                             inflow=[["Commodity", "Node"], ["Quantity"]])

# Define the foreign key relationships
input_schema.add_foreign_key("arcs", "nodes", ['Source', 'Name'])
input_schema.add_foreign_key("arcs", "nodes", ['Destination', 'Name'])
input_schema.add_foreign_key("cost", "nodes", ['Source', 'Name'])
input_schema.add_foreign_key("cost", "nodes", ['Destination', 'Name'])
input_schema.add_foreign_key("cost", "commodities", ['Commodity', 'Name'])
input_schema.add_foreign_key("inflow", "commodities", ['Commodity', 'Name'])
input_schema.add_foreign_key("inflow", "nodes", ['Node', 'Name'])

# Define the data types
input_schema.set_data_type("commodities",
                           "Volume",
                           min=0,
                           max=float("inf"),
                           inclusive_min=False,
                           inclusive_max=False)
input_schema.set_data_type("arcs",
Ejemplo n.º 4
0
                           max=float("inf"),
                           inclusive_min=True,
                           inclusive_max=False)
input_schema.set_data_type("warehouse_to_customer_costs",
                           "Cost",
                           min=0,
                           max=float("inf"),
                           inclusive_min=True,
                           inclusive_max=False)
input_schema.set_data_type("warehouse_to_customer_distances",
                           "Distance",
                           min=0,
                           max=float("inf"),
                           inclusive_min=True,
                           inclusive_max=False)
input_schema.add_foreign_key("demand", "customers", ["Customer", "Name"])
input_schema.add_foreign_key("demand", "products", ["Product", "Name"])
input_schema.add_foreign_key("supply", "plants", ["Plant", "Name"])
input_schema.add_foreign_key("supply", "products", ["Product", "Name"])
input_schema.add_foreign_key("plant_to_warehouse_costs", "plants",
                             ["Plant", "Name"])
input_schema.add_foreign_key("plant_to_warehouse_costs", "warehouses",
                             ["Warehouse", "Name"])
input_schema.add_foreign_key("plant_to_warehouse_costs", "products",
                             ["Product", "Name"])
input_schema.add_foreign_key("warehouse_to_customer_distances", "customers",
                             ["Customer", "Name"])
input_schema.add_foreign_key("warehouse_to_customer_distances", "warehouses",
                             ["Warehouse", "Name"])
input_schema.add_foreign_key("warehouse_to_customer_costs", "products",
                             ["Product", "Name"])
Ejemplo n.º 5
0
from ticdat import PanDatFactory
input_schema = PanDatFactory(cities=[["Name"],["Demand"]],
                             distances=[["Source", "Destination"], ["Distance"]],
                             parameters=[["Parameter"], ["Value"]])

input_schema.add_parameter("Number of Centroids", default_value=4, inclusive_min=False, inclusive_max=False, min=0,
                            max=float("inf"), must_be_int=True)
input_schema.set_data_type("cities", "Demand", min=0, max=float("inf"), inclusive_min=True, inclusive_max=False)
input_schema.set_data_type("distances", "Distance", min=0, max=float("inf"), inclusive_min=True, inclusive_max=False)
input_schema.add_foreign_key("distances", "cities", ['Source', 'Name'])
input_schema.add_foreign_key("distances", "cities", ['Destination', 'Name'])

# The distance matrix is bi-directionally safe. I.e. if the same source/dest and dest/source exist then the
# distances must match. If only one is present, it can fall back to the other in the code.
def _distance_matrix(dat):
    return {"distance_matrix": {tuple(row[:2]): row[2] for row in dat.distances.itertuples(index=False)}}
input_schema.add_data_row_predicate("distances", predicate_name="Check Bi-Directionally Safe",
    predicate=lambda row, distance_matrix: ((row["Destination"], row["Source"]) not in distance_matrix) or
                                            (row["Distance"] == distance_matrix[row["Destination"], row["Source"]]),
    predicate_kwargs_maker=_distance_matrix)

solution_schema = PanDatFactory(openings=[['City'],[]], assignments=[['City', 'Assigned To'],[]],
                                parameters=[["Parameter"], ["Value"]])

def solve(dat):
    assert input_schema.good_pan_dat_object(dat), "bad dat check"
    assert not input_schema.find_duplicates(dat), "duplicate row check"
    assert not input_schema.find_foreign_key_failures(dat), "foreign key check"
    assert not input_schema.find_data_type_failures(dat), "data type value check"
    assert not input_schema.find_data_row_failures(dat), "data row check"
Ejemplo n.º 6
0
                                      [
                                          'Position', 'Average Draft Position',
                                          'Expected Points', 'Draft Status'
                                      ]],
                             roster_requirements=[['Position'],
                                                  [
                                                      'Min Num Starters',
                                                      'Max Num Starters',
                                                      'Min Num Reserve',
                                                      'Max Num Reserve',
                                                      'Flex Status'
                                                  ]],
                             my_draft_positions=[['Draft Position'], []])

# add foreign key constraints (optional, but helps with preventing garbage-in, garbage-out)
input_schema.add_foreign_key("players", "roster_requirements",
                             ['Position', 'Position'])

# set data types (optional, but helps with preventing garbage-in, garbage-out)
input_schema.set_data_type("parameters",
                           "Parameter",
                           number_allowed=False,
                           strings_allowed=[
                               "Starter Weight", "Reserve Weight",
                               "Maximum Number of Flex Starters"
                           ])
input_schema.set_data_type("parameters",
                           "Value",
                           min=0,
                           max=float("inf"),
                           inclusive_min=True,
                           inclusive_max=False)
Ejemplo n.º 7
0
# to solution_data.xlsx.
#

from amplpy import AMPL
from ticdat import PanDatFactory, standard_main

# ------------------------ define the input schema --------------------------------
# There are three input tables, with 4 primary key fields and 4 data fields.
input_schema = PanDatFactory(categories=[["Name"],
                                         ["Min Nutrition", "Max Nutrition"]],
                             foods=[["Name"], ["Cost"]],
                             nutrition_quantities=[["Food", "Category"],
                                                   ["Quantity"]])

# Define the foreign key relationships
input_schema.add_foreign_key("nutrition_quantities", "foods", ["Food", "Name"])
input_schema.add_foreign_key("nutrition_quantities", "categories",
                             ["Category", "Name"])

# Define the data types
input_schema.set_data_type("categories",
                           "Min Nutrition",
                           min=0,
                           max=float("inf"),
                           inclusive_min=True,
                           inclusive_max=False)
input_schema.set_data_type("categories",
                           "Max Nutrition",
                           min=0,
                           max=float("inf"),
                           inclusive_min=True,