Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nested classes branch from with model #2

Open
wants to merge 4 commits into
base: nested_classes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
313 changes: 86 additions & 227 deletions example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"model = Model()\n",
"\n",
"# SPF Calculations\n",
"with model.root.add_layer() as layer:\n",
" layer.add_schema({\n",
"with model.cursor.add_layer():\n",
" model.cursor.add_schema({\n",
" \"label\": \"schema1\",\n",
" \"params\": [\"x\"],\n",
" \"actions\": [\"get\"],\n",
Expand All @@ -26,8 +26,8 @@
" })\n",
"\n",
" # SPF 1\n",
" with layer.add_sequence() as seq:\n",
" seq.add_schema({\n",
" with model.cursor.add_sequence():\n",
" model.cursor.add_schema({\n",
" \"label\": \"schema2\",\n",
" \"params\": [\"x\"],\n",
" \"actions\": [\"get\"],\n",
Expand All @@ -37,13 +37,13 @@
" 2: {\"a\": 20, \"b\": 20, \"c\": 20, \"d\": 20}\n",
" }\n",
" }, hidden=True)\n",
" @seq.add_wrapped()\n",
" @model.cursor.add_wrapped(tags='af')\n",
" def process(a, b, c, d):\n",
" return a * b + c / d\n",
" \n",
" # SPF 2\n",
" with layer.add_sequence() as seq:\n",
" seq.add_schema({\n",
" with model.cursor.add_sequence():\n",
" model.cursor.add_schema({\n",
" \"label\": \"schema3\",\n",
" \"params\": [\"x\"],\n",
" \"actions\": [\"get\"],\n",
Expand All @@ -53,276 +53,135 @@
" 2: {\"a\": 20, \"b\": 20, \"c\": 20, \"d\": 20}\n",
" }\n",
" }, hidden=True)\n",
" @seq.add_wrapped()\n",
" @model.cursor.add_wrapped(tags='af')\n",
" def process(a, b, c, d):\n",
" return a * b + c / d\n",
"\n",
"# AF Calculations\n",
"with model.root.add_layer() as layer:\n",
"with model.cursor.add_layer() as layer:\n",
"\n",
" # AF 1\n",
" layer.add_wrapped()\n",
" @model.cursor.add_wrapped()\n",
" def af_lane_width(lane_width):\n",
" return lane_width / 12"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b5f1c041",
"execution_count": 3,
"id": "19660b10",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'x': 1, 'schema1': 10, 'process': 101.0}"
"{'af': [<modelsandbox.model.processors.FunctionProcessor at 0x7f288853b400>,\n",
" <modelsandbox.model.processors.FunctionProcessor at 0x7f288853b2b0>]}"
]
},
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.analyze(x=1)"
"model.tagged"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb22780f",
"execution_count": 8,
"id": "3ad8af3b",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['lane_width', 'x']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.root.hidden"
"model.params"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b5a3a05",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3640eff1-d205-46e9-97e2-8e10a19c1372",
"execution_count": 10,
"id": "b5f1c041",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'x': 1,\n",
" 'lane_width': 13,\n",
" 'schema1': 10,\n",
" 'process': 101.0,\n",
" 'af_lane_width': 1.0833333333333333}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import importlib\n",
"import modelsandbox\n",
"importlib.reload(modelsandbox)\n",
"from modelsandbox import Model"
"model.analyze(x=1, lane_width=13)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e8343d5-7941-467c-84a7-9a52b2f7a895",
"execution_count": 5,
"id": "cb22780f",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['a']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from modelsandbox import Model\n",
"\n",
"model = Model()\n",
"def params(f):\n",
" num_args = f.__code__.co_argcount\n",
" return list(f.__code__.co_varnames[:num_args])\n",
"def f(a, *b, **c):\n",
" return\n",
"\n",
"model[0].set_label('First Layer')\n",
"model[0].set_tags('layer1')\n",
"model[0].add_function(lambda a, b: a + b, tags='tag1')\n",
"model[0].add_sequence()\n",
"model[0][1]"
]
},
{
"cell_type": "markdown",
"id": "f9b134bd-bfb8-46ca-88b1-d3584a512ca6",
"metadata": {},
"source": [
"# Cost example"
"params(f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bdd3247-fad7-411d-9806-239895299f6b",
"execution_count": 14,
"id": "5b5a3a05",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'b'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import importlib\n",
"import modelsandbox\n",
"importlib.reload(modelsandbox)\n",
"from modelsandbox import Model\n",
"import inspect\n",
"\n",
"# Initialize the model class\n",
"model = Model()\n",
"\n",
"# Add a layer to the model to compute airline ticket cost\n",
"model[0].set_label('Compute ticket cost')\n",
"\n",
"# Add the process schema to the model\n",
"model[0].add_schema({\n",
" \"label\": \"ticket_cost\",\n",
" \"params\": [\"destination\", \"airline_class\"],\n",
" \"actions\": [\"get\", \"get\"],\n",
" \"data\": {\n",
" \"Chicago\": {\n",
" \"Economy\": 220,\n",
" \"Business\": 450,\n",
" \"First\": 785\n",
" },\n",
" \"Los Angeles\": {\n",
" \"Economy\": 365,\n",
" \"Business\": 520,\n",
" \"First\": 965\n",
" }\n",
" }\n",
"})\n",
"\n",
"# Add a layer to the model to compute additional costs\n",
"model[1].set_label('Compute expenses')\n",
"\n",
"# Add a processor to compute travel cost\n",
"@model[1].add_wrapped(tags=['__cost'])\n",
"def travel_cost(number_of_travelers, ticket_cost):\n",
" \"\"\"\n",
" Compute total travel cost for all travelers.\n",
" \"\"\"\n",
" return number_of_travelers * ticket_cost\n",
"\n",
"# Add processor to compute lodging cost\n",
"@model[1].add_wrapped(tags=['__cost'])\n",
"def lodging_cost(number_of_travelers, nightly_cost, number_of_nights):\n",
" \"\"\"\n",
" Compute total lodging cost for all travelers.\n",
" \"\"\"\n",
" return number_of_travelers * nightly_cost * number_of_nights\n",
"\n",
"# Add processor to compute per diem\n",
"@model[1].add_wrapped(tags=['__cost'])\n",
"def per_diem_cost(number_of_travelers, number_of_nights, per_diem):\n",
" \"\"\"\n",
" Compute total per diem cost for all travelers.\n",
" \"\"\"\n",
" return number_of_travelers * number_of_nights * per_diem\n",
"\n",
"# Add a layer to the model to aggregate costs\n",
"model[2].set_label('Aggregate expenses')\n",
"\n",
"# Add processor to compute total trip cost\n",
"@model[2].add_wrapped()\n",
"def total_trip_cost(travel_cost, lodging_cost, per_diem_cost, __cost):\n",
" \"\"\"\n",
" Compute total trip cost for all travelers.\n",
" \"\"\"\n",
" return travel_cost + lodging_cost + per_diem_cost\n",
"\n",
"model.structure"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1eba3535-f095-42be-8a4c-959b0ebe38bd",
"metadata": {},
"outputs": [],
"source": [
"model.tagged"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e95c00e-ca1a-4c2c-b630-d7ae32dd5fe7",
"metadata": {},
"outputs": [],
"source": [
"model.analyze(\n",
" airline_class=\"Business\",\n",
" destination=\"Chicago\",\n",
" nightly_cost=185,\n",
" number_of_nights=4,\n",
" number_of_travelers=3,\n",
" per_diem=72,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "676a253a-10db-4fe2-9634-1c531daa15c4",
"metadata": {},
"source": [
"# Scratch"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c829f1d-5419-4b1a-b42d-bf98f85129fd",
"metadata": {},
"outputs": [],
"source": [
"from example import model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b1c7f912-bb76-4966-998f-ec3979efaf1f",
"metadata": {},
"outputs": [],
"source": [
"model.analyze(\n",
" airline_class=\"Business\",\n",
" destination=\"Chicago\",\n",
" nightly_cost=185,\n",
" number_of_nights=4,\n",
" number_of_travelers=3,\n",
" per_diem=72,\n",
" ticket_cost=500\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "740b1f87-61a9-4406-8698-d841f7eaa011",
"metadata": {},
"outputs": [],
"source": [
"model.parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8288bbf1-9a90-4004-8f95-d4557b1fd9bf",
"metadata": {},
"outputs": [],
"source": [
"from sample_model import model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "655db299-aacf-4933-84c5-4e82f0e3ae67",
"metadata": {},
"outputs": [],
"source": [
"model.analyze(\n",
" aadt = 580,\n",
" curve_length = 0.2,\n",
" curve_radius = 800,\n",
" lane_width = 11,\n",
" length = 0.6,\n",
" observed_kabco = 3.4,\n",
" shoulder_type = 'gravel',\n",
" shoulder_width = 4,\n",
" spiral = 'both'\n",
")"
"inspect.getfullargspec(f)[1]"
]
}
],
Expand Down
Loading