Skip to content

Commit 86b8de4

Browse files
committed
m2 ex
1 parent 934f146 commit 86b8de4

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"# Exercise Set 2\n",
21+
"**Q1.** Re-write the **Charge/discharge:** constraints using indicator constraints. Remember to consider what happens when $z_{b,t} = 1$ and when $z_{b,t} = 0$\n",
22+
"\n",
23+
"Define $z_t \\in \\{0,1\\}, t \\in T$.\n",
24+
"$$\n",
25+
"\\begin{align*}\n",
26+
"f^{in}_{b,t} &\\leq 20*z_{b,t} &\\forall b \\in B, t \\in T \\\\\n",
27+
"f^{out}_{b,t} &\\leq 20*(1-z_{b,t}) &\\forall b \\in B, t \\in T\n",
28+
"\\end{align*}\n",
29+
"$$"
30+
],
31+
"metadata": {
32+
"id": "0UEwW_kxbBan"
33+
}
34+
},
35+
{
36+
"cell_type": "code",
37+
"source": [
38+
"m.addConstrs((((z[b,t] == 0) >> (flow_in[b,t] == 0)) for b in batteries for t in time_periods), name = \"zis0\")\n",
39+
"m.addConstrs((((z[b,t] == 1) >> (flow_out[b,t] == 0)) for b in batteries for t in time_periods), name = \"zis1\")\n",
40+
"\n"
41+
],
42+
"metadata": {
43+
"id": "Hv_YnclGhw5x"
44+
},
45+
"execution_count": null,
46+
"outputs": []
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"source": [
51+
"The multiple scenarios API is restricted. For example, it is not possible to explicitly\n",
52+
"- Add/remove variables or constraints\n",
53+
"- Change the variable types\n",
54+
"- Change the sense of constraints\n",
55+
"\n",
56+
"However, we can get around some of the restrictions using some tricks.\n",
57+
"\n",
58+
"Assume `ScenarioNumber` and `ScenNName` parameters are already set for each of the below.\n",
59+
"______\n",
60+
"To remove a variable, set its bounds to zero.\n",
61+
"\n",
62+
"**Q3.** Write a `for` loop a `gurobipy` code to remove the flow in and flow out for `Battery0` and time periods 0 to 49.\n",
63+
"______\n",
64+
"To add a variable to a scenario, add it to the base model with bounds set to zero and then change the bounds accordingly.\n",
65+
"\n",
66+
"**Q4.** Write `gurobipy` code to add a continuous variable $w_t, t\\in T$ to the base model `m` such that it's \"not in\" this model as described above. Then write a `for` loop to add it to a scenario with an *upper bound* of 100.\n",
67+
"______\n",
68+
"\n",
69+
"To remove a constraint, change its RHS values to GRB.INFINITY/-GRB.INFINITY.\n",
70+
"\n",
71+
"**Q5.** We defined $v_{b,t} = 1$ when battery $b$ is below a depth of discharge α (say 0.3) and 0 otherwise. Supposed we had a hard constraint on the number of times either battery could go below it's depth, with that limit to be 15. We stored (set the constraint equal to) this constraint as `depth_limit`.\n",
72+
"\n",
73+
"$$\n",
74+
"\\begin{equation}\n",
75+
"\\sum_{b,t} v_{b,t} \\le C\n",
76+
"\\end{equation}\n",
77+
"$$\n",
78+
"\n",
79+
"Add a constraint to the base model `m` for the number of times either battery could go below its depth. Write the `gurobipy` code to remove this constraint.\n",
80+
"______"
81+
],
82+
"metadata": {
83+
"id": "YH5CEz4wdUEh"
84+
}
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"id": "AlS6ui8R8BFj"
91+
},
92+
"outputs": [],
93+
"source": [
94+
"#Q3.\n",
95+
"for t in range(50):\n",
96+
" flow_in['Battery0',t].ScenNUB = 0\n",
97+
" flow_out['Battery0',t].ScenNUB = 0\n",
98+
"\n",
99+
"#Q4.\n",
100+
"w = m.addVars(time_periods, ub = 0, name = 'w')\n",
101+
"#....\n",
102+
"for t in time_periods:\n",
103+
" w[t].ScenNUB = 100\n",
104+
"\n",
105+
"#Q5.\n",
106+
"v = m.addVars(time_periods, vtype=GRB.BINARY, name = 'v')\n",
107+
"# define a linear expression for total depth count\n",
108+
"total_depth_count = v.sum() # this is optional!\n",
109+
"depth_limit = m.addConstr(total_depth_count <= 15, name = 'depth_limit')\n",
110+
"\n",
111+
"# remove the constraint by setting the RHS to infinity\n",
112+
"depth_limit.ScenNRhs = GRB.INFINITY"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"source": [],
118+
"metadata": {
119+
"id": "f4w2OIzUe7_Y"
120+
},
121+
"execution_count": null,
122+
"outputs": []
123+
}
124+
]
125+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"# Exercise Set 2\n",
21+
"**Q1.** Re-write the **Charge/discharge:** constraints using indicator constraints. Remember to consider what happens when $z_{b,t} = 1$ and when $z_{b,t} = 0$\n",
22+
"\n",
23+
"Define $z_t \\in \\{0,1\\}, t \\in T$.\n",
24+
"$$\n",
25+
"\\begin{align*}\n",
26+
"f^{in}_{b,t} &\\leq 20*z_{b,t} &\\forall b \\in B, t \\in T \\\\\n",
27+
"f^{out}_{b,t} &\\leq 20*(1-z_{b,t}) &\\forall b \\in B, t \\in T\n",
28+
"\\end{align*}\n",
29+
"$$"
30+
],
31+
"metadata": {
32+
"id": "0UEwW_kxbBan"
33+
}
34+
},
35+
{
36+
"cell_type": "code",
37+
"source": [
38+
"m.addConstrs(( ??????????? ), name = \"zis0\")\n",
39+
"m.addConstrs(( ??????????? ), name = \"zis1\")\n",
40+
"\n"
41+
],
42+
"metadata": {
43+
"id": "Hv_YnclGhw5x"
44+
},
45+
"execution_count": null,
46+
"outputs": []
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"source": [
51+
"The multiple scenarios API is restricted. For example, it is not possible to explicitly\n",
52+
"- Add/remove variables or constraints\n",
53+
"- Change the variable types\n",
54+
"- Change the sense of constraints\n",
55+
"\n",
56+
"However, we can get around some of the restrictions using some tricks.\n",
57+
"\n",
58+
"Assume `ScenarioNumber` and `ScenNName` parameters are already set for each of the below.\n",
59+
"______\n",
60+
"To remove a variable, set its bounds to zero.\n",
61+
"\n",
62+
"**Q3.** Write a `for` loop a `gurobipy` code to remove the flow in and flow out for `Battery0` and time periods 0 to 49.\n",
63+
"______\n",
64+
"To add a variable to a scenario, add it to the base model with bounds set to zero and then change the bounds accordingly.\n",
65+
"\n",
66+
"**Q4.** Write `gurobipy` code to add a continuous variable $w_t, t\\in T$ to the base model `m` such that it's \"not in\" this model as described above. Then write a `for` loop to add it to a scenario with an *upper bound* of 100.\n",
67+
"______\n",
68+
"\n",
69+
"To remove a constraint, change its RHS values to GRB.INFINITY/-GRB.INFINITY.\n",
70+
"\n",
71+
"**Q5.** We defined $v_{b,t} = 1$ when battery $b$ is below a depth of discharge α (say 0.3) and 0 otherwise. Supposed we had a hard constraint on the number of times either battery could go below it's depth, with that limit to be 15. We stored (set the constraint equal to) this constraint as `depth_limit`.\n",
72+
"\n",
73+
"$$\n",
74+
"\\begin{equation}\n",
75+
"\\sum_{b,t} v_{b,t} \\le C\n",
76+
"\\end{equation}\n",
77+
"$$\n",
78+
"\n",
79+
"Add a constraint to the base model `m` for the number of times either battery could go below its depth. Write the `gurobipy` code to remove this constraint.\n",
80+
"______"
81+
],
82+
"metadata": {
83+
"id": "YH5CEz4wdUEh"
84+
}
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"id": "AlS6ui8R8BFj"
91+
},
92+
"outputs": [],
93+
"source": [
94+
"#Q3.\n",
95+
"\n",
96+
"\n",
97+
"\n",
98+
"#Q4.\n",
99+
"\n",
100+
"\n",
101+
"\n",
102+
"#Q5.\n",
103+
"v = m.addVars(time_periods, vtype=GRB.BINARY, name = 'v')\n",
104+
"\n",
105+
"\n"
106+
]
107+
}
108+
]
109+
}

0 commit comments

Comments
 (0)