Skip to content

Commit a22d39d

Browse files
committed
fix lp solver
1 parent 5af0cae commit a22d39d

File tree

1 file changed

+96
-32
lines changed

1 file changed

+96
-32
lines changed

1. LP Problem - DMC.ipynb

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@
730730
},
731731
{
732732
"cell_type": "code",
733-
"execution_count": 41,
733+
"execution_count": 51,
734734
"id": "110b9543",
735735
"metadata": {},
736736
"outputs": [],
@@ -807,14 +807,14 @@
807807
},
808808
{
809809
"cell_type": "code",
810-
"execution_count": 42,
810+
"execution_count": 52,
811811
"id": "b842ad4e",
812812
"metadata": {},
813813
"outputs": [
814814
{
815815
"data": {
816816
"application/vnd.jupyter.widget-view+json": {
817-
"model_id": "a6c5a1f819054a769f275840f0fb97f9",
817+
"model_id": "bd0f27ce2f1048d3b4c8d23fff809a5c",
818818
"version_major": 2,
819819
"version_minor": 0
820820
},
@@ -880,14 +880,14 @@
880880
},
881881
{
882882
"cell_type": "code",
883-
"execution_count": 43,
883+
"execution_count": 53,
884884
"id": "e2876471",
885885
"metadata": {},
886886
"outputs": [
887887
{
888888
"data": {
889889
"application/vnd.jupyter.widget-view+json": {
890-
"model_id": "83c35f21b9d54774994c17ae25e3af18",
890+
"model_id": "ee3f4dcd64ac4d78b2798e130b74482e",
891891
"version_major": 2,
892892
"version_minor": 0
893893
},
@@ -951,14 +951,14 @@
951951
},
952952
{
953953
"cell_type": "code",
954-
"execution_count": 44,
954+
"execution_count": 54,
955955
"id": "61e1f71d",
956956
"metadata": {},
957957
"outputs": [
958958
{
959959
"data": {
960960
"application/vnd.jupyter.widget-view+json": {
961-
"model_id": "90631f9d30e14e7da3cb93ff1fc18c67",
961+
"model_id": "1e57ad8657ed4ab9839680be6e63dd34",
962962
"version_major": 2,
963963
"version_minor": 0
964964
},
@@ -1023,14 +1023,14 @@
10231023
},
10241024
{
10251025
"cell_type": "code",
1026-
"execution_count": 45,
1026+
"execution_count": 55,
10271027
"id": "2670205b",
10281028
"metadata": {},
10291029
"outputs": [
10301030
{
10311031
"data": {
10321032
"application/vnd.jupyter.widget-view+json": {
1033-
"model_id": "097e234d52ab474fa93ae5dd0b98fec8",
1033+
"model_id": "5f9900f2cef14d1f9a6b6d378d3a2595",
10341034
"version_major": 2,
10351035
"version_minor": 0
10361036
},
@@ -1106,7 +1106,7 @@
11061106
},
11071107
{
11081108
"cell_type": "code",
1109-
"execution_count": 46,
1109+
"execution_count": 57,
11101110
"id": "c127fa0c",
11111111
"metadata": {
11121112
"scrolled": false
@@ -1115,7 +1115,7 @@
11151115
{
11161116
"data": {
11171117
"application/vnd.jupyter.widget-view+json": {
1118-
"model_id": "647b9af802ed4bdca3250797935e3442",
1118+
"model_id": "2a89c0152afd4008ba517d0da49309d8",
11191119
"version_major": 2,
11201120
"version_minor": 0
11211121
},
@@ -1219,28 +1219,61 @@
12191219
"ax.relim()\n",
12201220
"ax.autoscale_view()\n",
12211221
"\n",
1222-
"# # Solve the LP\n",
1223-
"# def run_pulp(cost_MV1, cost_MV2, MV1_Lo=-limits, MV2_Lo=-limits):\n",
1224-
"# def run_lp(values):\n",
1225-
"# prob = LpProblem(\"DMC_problem\",LpMinimize)\n",
1226-
"# MV1=LpVariable(\"MV1\",-limits)\n",
1227-
"# MV2=LpVariable(\"MV2\",-limits)\n",
1228-
"# prob += values[2]*MV1 + values[3]*MV2, \"Cost function of MVs\"\n",
1222+
"# Solve the LP\n",
1223+
"def run_lp(values_dict):\n",
1224+
" '''\n",
1225+
" Run pulp solver for LP problem with the same gain matrix and CV limits as the example. Returns a tuple with:\n",
1226+
" (LP solution array [MV1_solution, MV2_solution], objective_value)\n",
1227+
" ''' \n",
1228+
" prob = LpProblem(\"DMC_problem\",LpMinimize)\n",
1229+
" \n",
1230+
" nMVs = 2\n",
1231+
" nCVs = 2\n",
1232+
" \n",
1233+
" # How many MVs\n",
1234+
" MVs = []\n",
1235+
" for i in range(nMVs):\n",
1236+
" MVs.append(LpVariable(f\"MV{i+1}\",-limits))\n",
1237+
" \n",
1238+
" # the objective function\n",
1239+
" obj = 0\n",
1240+
" for indx, MV in enumerate(MVs):\n",
1241+
" obj += values_dict[f\"MV{indx+1} Cost\"]*MV\n",
1242+
" \n",
1243+
" prob += obj, \"Cost function of MVs\"\n",
1244+
" \n",
1245+
" # constraint formulation in terms of MV1 and MV2\n",
1246+
" CV_contraint_lo = []\n",
1247+
" CV_contraint_hi = []\n",
1248+
" \n",
1249+
" for i in range(nCVs):\n",
1250+
" c = 0\n",
1251+
" for indx, MV in enumerate(MVs):\n",
1252+
" c += G[i][indx]*MV\n",
1253+
" prob += c <= values_dict[f'CV{i+1} Limits'][1], f'CV{i+1} High Limit'\n",
1254+
" prob += c >= values_dict[f'CV{i+1} Limits'][0], f'CV{i+1} Low Limit'\n",
1255+
" \n",
1256+
" for indx, MV in enumerate(MVs):\n",
1257+
" prob += MV <= values_dict[f'MV{indx+1} Limits'][1], f'MV{indx+1} High Limit'\n",
1258+
" prob += MV >= values_dict[f'MV{indx+1} Limits'][0], f'MV{indx+1} Low Limit' \n",
12291259
" \n",
1230-
"# # constraint formulation in terms of MV1 and MV2\n",
1231-
"# prob += G11*MV1+G12*MV2 <= values[0][1], \"CV1 High Limit\"\n",
1232-
"# prob += G11*MV1+G12*MV2 >= values[0][0], \"CV1 Low Limit\"\n",
1233-
"# prob += G21*MV1+G22*MV2 <= values[1][1], \"CV2 High Limit\"\n",
1234-
"# prob += G21*MV1+G22*MV2 >= values[1][0], \"CV2 Low Limit\"\n",
1235-
"# if (prob.solve(PULP_CBC_CMD(msg=0)) == 1):\n",
1236-
"# return [v.varValue for v in prob.variables()], value(prob.objective)\n",
1237-
"# else:\n",
1238-
"# print(\"NOT SOLVED - Infeasibility!\")\n",
1239-
"# return [0,0], 0\n",
1260+
" if (prob.solve(PULP_CBC_CMD(msg=0)) == 1):\n",
1261+
" return [v.varValue for v in prob.variables()], value(prob.objective)\n",
1262+
" else:\n",
1263+
" print(\"NOT SOLVED - Infeasibility!\")\n",
1264+
" return np.zeros(nMVs), 0\n",
12401265
"\n",
12411266
"def handle_slider_change(change):\n",
12421267
" ## grab slider values for CV contraints\n",
12431268
" values = [slider.value for slider in sliders]\n",
1269+
" values_dict = {}\n",
1270+
" values_dict['CV1 Limits'] = values[0]\n",
1271+
" values_dict['CV2 Limits'] = values[1]\n",
1272+
" values_dict['MV1 Limits'] = values[4]\n",
1273+
" values_dict['MV2 Limits'] = values[5]\n",
1274+
" values_dict['MV1 Cost'] = values[2]\n",
1275+
" values_dict['MV2 Cost'] = values[3]\n",
1276+
" \n",
12441277
" cost_MV1 = values[2]\n",
12451278
" cost_MV2 = values[3]\n",
12461279
" \n",
@@ -1275,7 +1308,7 @@
12751308
" q.set_UVC(-cost_MV1, -cost_MV2, z_obj)\n",
12761309
" \n",
12771310
" # Find the LP soln\n",
1278-
" soln, V = run_pulp(cost_MV1, cost_MV2, MV1_Lo=values[4][0], MV1_Hi=values[4][1], MV2_Lo=values[5][0], MV2_Hi=values[5][1])\n",
1311+
" soln, V = run_lp(values_dict)\n",
12791312
" \n",
12801313
" if (V == None):\n",
12811314
" V = 0\n",
@@ -1405,7 +1438,7 @@
14051438
},
14061439
{
14071440
"cell_type": "code",
1408-
"execution_count": null,
1441+
"execution_count": 58,
14091442
"id": "94dd4426",
14101443
"metadata": {},
14111444
"outputs": [],
@@ -1460,16 +1493,47 @@
14601493
},
14611494
{
14621495
"cell_type": "code",
1463-
"execution_count": null,
1496+
"execution_count": 59,
14641497
"id": "dea575d4",
14651498
"metadata": {},
1466-
"outputs": [],
1499+
"outputs": [
1500+
{
1501+
"name": "stdout",
1502+
"output_type": "stream",
1503+
"text": [
1504+
"\n",
1505+
"\u001b[0;31mNameError: \u001b[0mname 'n_frames' is not defined\n"
1506+
]
1507+
},
1508+
{
1509+
"data": {
1510+
"application/vnd.jupyter.widget-view+json": {
1511+
"model_id": "e7cbddc90f454ac99ec1980582ed5c70",
1512+
"version_major": 2,
1513+
"version_minor": 0
1514+
},
1515+
"text/plain": [
1516+
"Button(description='Click to show stack trace', layout=Layout(height='auto', width='auto'), style=ButtonStyle(…"
1517+
]
1518+
},
1519+
"metadata": {},
1520+
"output_type": "display_data"
1521+
}
1522+
],
14671523
"source": [
14681524
"from matplotlib import animation\n",
14691525
"anim = animation.FuncAnimation(fig, drawframe, frames=n_frames, interval=200)\n",
14701526
"from IPython.display import HTML\n",
14711527
"HTML(anim.to_jshtml())"
14721528
]
1529+
},
1530+
{
1531+
"cell_type": "code",
1532+
"execution_count": null,
1533+
"id": "5ef2ab86",
1534+
"metadata": {},
1535+
"outputs": [],
1536+
"source": []
14731537
}
14741538
],
14751539
"metadata": {

0 commit comments

Comments
 (0)