Minizinc Placement Model Ex. for vDNS+vFW use case
[optf/osdf.git] / examples / placement-models-minizinc / vdns-plus-vfw-use-case / placement-minizinc.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "code",
5    "execution_count": 1,
6    "metadata": {},
7    "outputs": [
8     {
9      "name": "stdout",
10      "output_type": "stream",
11      "text": [
12       "int: N_CLOUD_REGIONS;  % number of cloud regions\n",
13       "% set of 1..N_CLOUD_REGIONS: C_REGIONS;\n",
14       "\n",
15       "int: N_ATTRIBUTES; %  number of capability related attributes\n",
16       "% set of 1..N_ATTRIBUTES: ATTRIBS;\n",
17       "array[1..N_ATTRIBUTES] of float: w_attributes; % weights of each attribute\n",
18       "\n",
19       "int: N_UTILIZATION_METRICS; % number of dynamic capacity metrics of interest\n",
20       "% set of 1..N_UTILIZATION_METRICS: U_METRICS;\n",
21       "array[1..N_UTILIZATION_METRICS] of float: w_metrics; % weights of each capacity metric\n",
22       "\n",
23       "int: cust_type;  % customer type, 0 = regular, 1 = silver, 2 = gold\n",
24       "int: N_VMS; % number of VMs in VNF\n",
25       "int: N_CAPACITY_METRICS; % number of metrics for cloud region capacity check\n",
26       "\n",
27       "float: C_ALLOC_THRESHOLD; % allocation threshold for cloud\n",
28       "float: CUST_ALLOC_THRESHOLD; % allocation threshold for customer in cloud\n",
29       "float: AVG_CPU_UTILIZATION_THRESHOLD;\n",
30       "float: PEAK_CPU_UTILIZATION_THRESHOLD;\n",
31       "\n",
32       "enum CUST_TYPES = { STANDARD, SILVER, GOLD };\n",
33       "enum ATTRIBUTES = { CORE_DC, DIRECT_CONN, MIN_GUARANTEE, SRIOV };\n",
34       "enum METRICS = { AVG_CPU_UTILIZATION, PEAK_CPU_UTILIZATION };\n",
35       "enum CLOUD_REGION_CAPACITY = {CPU_CLOUD, MEMORY_CLOUD};\n",
36       "enum VNFS_TYPES = { VDNS, VFW };\n",
37       "int: N_VNFS;\n",
38       "\n",
39       "% set of 1..N_CAPACITY_METRICS: CAP_METRICS;\n",
40       "\n",
41       "% whether a cloud region has the corresponding capability -- data will be customer specific\n",
42       "% array[1..N_CLOUD_REGIONS, 1..N_CLOUD_REGIONS] of float: c_dist;\n",
43       "array[1..N_CLOUD_REGIONS, 1..N_ATTRIBUTES] of int: capabilities;\n",
44       "array[1..N_CLOUD_REGIONS, 1..N_UTILIZATION_METRICS] of float: cpu_utilization;  % how much capacity is already dynamically utilized     (fraction)\n",
45       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of int: c_alloc_capacity; % how much percent is already allocated in the cloud\n",
46       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of int: c_total_capacity;  % total cloud capacity\n",
47       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of float: c_alloc_capacity_norm;\n",
48       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of int: cust_alloc_capacity; % how much percent is already allocated in the cloud for the customer\n",
49       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of int: cust_total_capacity; % total cloud capacity for customer\n",
50       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of float: cust_alloc_capacity_norm;\n",
51       "\n",
52       "% VM requirements for each type of capacity (vm cpu, memory, etc.)\n",
53       "% TODO: establish a standard for units (MB RAM, GB disk, N virtual cores, etc.)\n",
54       "array[1..N_VMS, 1..N_CAPACITY_METRICS] of int: vm_reqs;\n",
55       "array[1..N_CAPACITY_METRICS] of int: vm_reqs_sums = [ sum(k in 1..N_VMS) (vm_reqs[k,j]) | j in 1..N_CAPACITY_METRICS ];\n",
56       "array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of float: vm_reqs_sums_norm;\n",
57       "%forall(i in 1..N_CLOUD_REGIONS, j in 1..N_CAPACITY_METRICS) (\n",
58       "%  vm_reqs_sums_norm[i, j] = vm_reqs_sums[j]/c_total_capacity[i, j]\n",
59       "%)\n",
60       "%array[1..N_CLOUD_REGIONS, 1..N_CAPACITY_METRICS] of float: vm_reqs_sums_norm = [ ((vm_reqs_sums[j]/c_total_capacity[i,j]) | j in 1..N_CAPACITY_METRICS) | i in 1..N_CLOUD_REGIONS ];\n",
61       "\n",
62       "array[1..N_VNFS] of var int: s_regions;  % target cloud regions (solution to the problem)\n",
63       "\n",
64       "% custom constraints\n",
65       "constraint forall (s in s_regions) (\n",
66       "    cpu_utilization[s, AVG_CPU_UTILIZATION] <= AVG_CPU_UTILIZATION_THRESHOLD /\\    % hard constraint: need some capacity available\n",
67       "    cpu_utilization[s, PEAK_CPU_UTILIZATION] <= PEAK_CPU_UTILIZATION_THRESHOLD /\\  % hard constraint: need some capacity available\n",
68       "    cust_alloc_capacity[s, CPU_CLOUD] <= (CUST_ALLOC_THRESHOLD*(cust_total_capacity[s, CPU_CLOUD])) - (vm_reqs_sums[CPU_CLOUD]) /\\\n",
69       "    cust_alloc_capacity[s, MEMORY_CLOUD] <= (CUST_ALLOC_THRESHOLD*(cust_total_capacity[s, MEMORY_CLOUD])) - (vm_reqs_sums[MEMORY_CLOUD]) /\\\n",
70       "    c_alloc_capacity[s, CPU_CLOUD] <= (C_ALLOC_THRESHOLD*(c_total_capacity[s, CPU_CLOUD])) - (vm_reqs_sums[CPU_CLOUD]) /\\\n",
71       "    c_alloc_capacity[s, MEMORY_CLOUD] <= (C_ALLOC_THRESHOLD*(c_total_capacity[s, MEMORY_CLOUD])) - (vm_reqs_sums[MEMORY_CLOUD])\n",
72       ");\n",
73       "\n",
74       "% specific constraints based on the VNF\n",
75       "constraint capabilities[s_regions[VDNS], CORE_DC] = 1;  % hard constraint for vDNS: has to be placed in CORE DC\n",
76       "\n",
77       "% custom soft constraint for gold customers -- give a large weight to direct connection\n",
78       "var float: additional_obj = sum(s in s_regions) (bool2int(cust_type = GOLD) * capabilities[s, MIN_GUARANTEE] * 1000);\n",
79       "\n",
80       "% TODO: global constraints (such as data validation)\n",
81       "\n",
82       "% Objective for utilization\n",
83       "var float: obj_c_capacity = sum(k in 1..N_CAPACITY_METRICS, s in s_regions) (\n",
84       "\t\t  (1 - c_alloc_capacity_norm[s, k] - vm_reqs_sums_norm[s, k]) +\n",
85       "                  (1 - cust_alloc_capacity_norm[s, k] - vm_reqs_sums_norm[s, k]));\n",
86       "\n",
87       "% Objective for utilization\n",
88       "var float: obj_utilization = sum(k in 1..N_UTILIZATION_METRICS, s in s_regions) ( w_metrics[k] * (1 - cpu_utilization[s, k]) );\n",
89       "\n",
90       "% Objective for capabilities\n",
91       "var float: obj_capabilities = sum(k in 1..N_ATTRIBUTES, s in s_regions) ( w_attributes[k] * capabilities[s, k] );\n",
92       "\n",
93       "% Overall objective function\n",
94       "var float: obj = obj_c_capacity + obj_utilization + obj_capabilities + additional_obj;\n",
95       "\n",
96       "solve maximize obj;\n",
97       "\n",
98       "output [ \"Solution: \\nCloud Region for vDNS = \", show(s_regions[VDNS]) ] \n",
99       "++ [ if N_VNFS = 2 then \"\\nCloud Region for vFW = \" ++ show(s_regions[VFW]) ++ \"\\n\" else \"\\n\" endif ]\n",
100       "++ [ \"Objective function value: \", show(obj), \"\\n\", \"Customer type: \", show(cust_type), \"\\n\"];\n"
101      ]
102     }
103    ],
104    "source": [
105     "!cat placement.mzn"
106    ]
107   },
108   {
109    "cell_type": "code",
110    "execution_count": 2,
111    "metadata": {},
112    "outputs": [
113     {
114      "name": "stdout",
115      "output_type": "stream",
116      "text": [
117       "% enum CUST_TYPES = { STANDARD, SILVER, GOLD };\n",
118       "% enum ATTRIBUTES = { CORE_DC, DIRECT_CONN, MIN_GUARANTEE, SRIOV };\n",
119       "% enum METRICS = { AVG_CPU_UTILIZATION, PEAK_CPU_UTILIZATION };\n",
120       "% enum CLOUD_REGION_CAPACITY = {CPU_CLOUD, MEMORY_CLOUD};\n",
121       "\n",
122       "N_CLOUD_REGIONS = 3;  % e-dc-1, core-dc-1, core-dc-2\n",
123       "N_ATTRIBUTES = 4;\n",
124       "N_UTILIZATION_METRICS = 2;\n",
125       "N_CAPACITY_METRICS = 2;\n",
126       "N_VMS = 3;\n",
127       "C_ALLOC_THRESHOLD = 0.65;\n",
128       "CUST_ALLOC_THRESHOLD = 0.95;\n",
129       "AVG_CPU_UTILIZATION_THRESHOLD = 0.7;\n",
130       "PEAK_CPU_UTILIZATION_THRESHOLD = 0.95;\n",
131       "\n",
132       "cust_type = GOLD;\n",
133       "N_VNFS = 1;\n",
134       "\n",
135       "w_metrics = [0.9, 0.1];\n",
136       "w_attributes = [0.1, 0.9, 0.05, 0.05]; \n",
137       "\n",
138       "capabilities = [| 0, 1, 1, 1\n",
139       "                | 1, 0, 1, 1\n",
140       "                | 1, 1, 0, 1 |];\n",
141       "\n",
142       "cpu_utilization = [| 0.05, 0.9\n",
143       "                   | 0.1, 0.5\n",
144       "                   | 0.6, 0.8 |];\n",
145       "\n",
146       "c_alloc_capacity = [| 600, 1200\n",
147       "                    | 1200, 2400\n",
148       "                    | 2400, 4800 |];\n",
149       "\n",
150       "c_total_capacity = [| 1000, 2000\n",
151       "                    | 2000, 4000\n",
152       "                    | 4000, 8000 |];\n",
153       "\n",
154       "c_alloc_capacity_norm = [| 0.6, 0.6\n",
155       "                         | 0.6, 0.6\n",
156       "                         | 0.6, 0.6 |];\n",
157       "\n",
158       "cust_alloc_capacity = [| 60, 120\n",
159       "                       | 120, 240\n",
160       "                       | 240, 480 |];\n",
161       "\n",
162       "cust_total_capacity = [| 100, 200\n",
163       "                       | 200, 400\n",
164       "                       | 400, 800 |];\n",
165       "\n",
166       "cust_alloc_capacity_norm = [| 0.6, 0.6\n",
167       "                            | 0.6, 0.6\n",
168       "                            | 0.6, 0.6 |];\n",
169       "\n",
170       "vm_reqs = [| 1, 4\n",
171       "           | 2, 8\n",
172       "           | 4, 16 |];\n",
173       "\n",
174       "%vm_reqs_sums = [ 14, 56 ];\n",
175       "\n",
176       "vm_reqs_sums_norm = [| 0.007, 0.014\n",
177       "                     | 0.0035, 0.007\n",
178       "                     | 0.00175, 0.0035 |];\n"
179      ]
180     }
181    ],
182    "source": [
183     "!cat gold.dzn"
184    ]
185   },
186   {
187    "cell_type": "code",
188    "execution_count": 3,
189    "metadata": {},
190    "outputs": [
191     {
192      "name": "stdout",
193      "output_type": "stream",
194      "text": [
195       "23c23\n",
196       "<                 | 1, 0, 1, 1\n",
197       "---\n",
198       ">                 | 1, 0, 0, 1\n"
199      ]
200     }
201    ],
202    "source": [
203     "!diff gold.dzn gold-no-min-guarantee.dzn"
204    ]
205   },
206   {
207    "cell_type": "code",
208    "execution_count": 4,
209    "metadata": {},
210    "outputs": [
211     {
212      "name": "stdout",
213      "output_type": "stream",
214      "text": [
215       "23c23\n",
216       "<                 | 1, 0, 1, 1\n",
217       "---\n",
218       ">                 | 1, 0, 0, 1\n"
219      ]
220     }
221    ],
222    "source": [
223     "!diff gold.dzn gold-no-min-guarantee.dzn"
224    ]
225   },
226   {
227    "cell_type": "code",
228    "execution_count": 5,
229    "metadata": {},
230    "outputs": [
231     {
232      "name": "stdout",
233      "output_type": "stream",
234      "text": [
235       "% enum CUST_TYPES = { STANDARD, SILVER, GOLD };\n",
236       "% enum ATTRIBUTES = { CORE_DC, DIRECT_CONN, MIN_GUARANTEE, SRIOV };\n",
237       "% enum METRICS = { AVG_CPU_UTILIZATION, PEAK_CPU_UTILIZATION };\n",
238       "% enum CLOUD_REGION_CAPACITY = {CPU_CLOUD, MEMORY_CLOUD};\n",
239       "\n",
240       "N_CLOUD_REGIONS = 3;  % e-dc-1, core-dc-1, core-dc-2\n",
241       "N_ATTRIBUTES = 4;\n",
242       "N_UTILIZATION_METRICS = 2;\n",
243       "N_CAPACITY_METRICS = 2;\n",
244       "N_VMS = 3;\n",
245       "C_ALLOC_THRESHOLD = 0.65;\n",
246       "CUST_ALLOC_THRESHOLD = 0.95;\n",
247       "AVG_CPU_UTILIZATION_THRESHOLD = 0.7;\n",
248       "PEAK_CPU_UTILIZATION_THRESHOLD = 0.95;\n",
249       "\n",
250       "cust_type = SILVER;\n",
251       "N_VNFS = 2;\n",
252       "\n",
253       "w_metrics = [0.9, 0.1];\n",
254       "w_attributes = [0.1, 0.9, 0.05, 0.05]; \n",
255       "\n",
256       "capabilities = [| 0, 1, 1, 1\n",
257       "                | 1, 0, 1, 1\n",
258       "                | 1, 1, 0, 1 |];\n",
259       "\n",
260       "cpu_utilization = [| 0.05, 0.9\n",
261       "                   | 0.1, 0.5\n",
262       "                   | 0.6, 0.8 |];\n",
263       "\n",
264       "c_alloc_capacity = [| 600, 1200\n",
265       "                    | 1200, 2400\n",
266       "                    | 2400, 4800 |];\n",
267       "\n",
268       "c_total_capacity = [| 1000, 2000\n",
269       "                    | 2000, 4000\n",
270       "                    | 4000, 8000 |];\n",
271       "\n",
272       "c_alloc_capacity_norm = [| 0.6, 0.6\n",
273       "                         | 0.6, 0.6\n",
274       "                         | 0.6, 0.6 |];\n",
275       "\n",
276       "cust_alloc_capacity = [| 60, 120\n",
277       "                       | 120, 240\n",
278       "                       | 240, 480 |];\n",
279       "\n",
280       "cust_total_capacity = [| 100, 200\n",
281       "                       | 200, 400\n",
282       "                       | 400, 800 |];\n",
283       "\n",
284       "cust_alloc_capacity_norm = [| 0.6, 0.6\n",
285       "                            | 0.6, 0.6\n",
286       "                            | 0.6, 0.6 |];\n",
287       "\n",
288       "vm_reqs = [| 1, 4\n",
289       "           | 2, 8\n",
290       "           | 4, 16 |];\n",
291       "\n",
292       "%vm_reqs_sums = [ 14, 56 ];\n",
293       "\n",
294       "vm_reqs_sums_norm = [| 0.007, 0.014\n",
295       "                     | 0.0035, 0.007\n",
296       "                     | 0.00175, 0.0035 |];\n"
297      ]
298     }
299    ],
300    "source": [
301     "!cat silver.dzn"
302    ]
303   },
304   {
305    "cell_type": "code",
306    "execution_count": 6,
307    "metadata": {},
308    "outputs": [
309     {
310      "name": "stdout",
311      "output_type": "stream",
312      "text": [
313       "26c26\n",
314       "< cpu_utilization = [| 0.05, 0.9\n",
315       "---\n",
316       "> cpu_utilization = [| 0.05, 0.96\n"
317      ]
318     }
319    ],
320    "source": [
321     "!diff silver.dzn silver-high-edge-load.dzn"
322    ]
323   },
324   {
325    "cell_type": "code",
326    "execution_count": 7,
327    "metadata": {},
328    "outputs": [
329     {
330      "name": "stdout",
331      "output_type": "stream",
332      "text": [
333       "Solution: \n",
334       "Cloud Region for vDNS = 2\n",
335       "Objective function value: 1002.639\n",
336       "Customer type: 3\n",
337       "----------\n",
338       "==========\n"
339      ]
340     }
341    ],
342    "source": [
343     "!mzn-gecode -a placement.mzn gold.dzn"
344    ]
345   },
346   {
347    "cell_type": "code",
348    "execution_count": 8,
349    "metadata": {},
350    "outputs": [
351     {
352      "name": "stdout",
353      "output_type": "stream",
354      "text": [
355       "Solution: \n",
356       "Cloud Region for vDNS = 2\n",
357       "Objective function value: 2.589\n",
358       "Customer type: 3\n",
359       "----------\n",
360       "Solution: \n",
361       "Cloud Region for vDNS = 3\n",
362       "Objective function value: 3.0195\n",
363       "Customer type: 3\n",
364       "----------\n",
365       "==========\n"
366      ]
367     }
368    ],
369    "source": [
370     "!mzn-gecode -a placement.mzn gold-no-min-guarantee.dzn"
371    ]
372   },
373   {
374    "cell_type": "code",
375    "execution_count": 9,
376    "metadata": {},
377    "outputs": [
378     {
379      "name": "stdout",
380      "output_type": "stream",
381      "text": [
382       "Solution: \n",
383       "Cloud Region for vDNS = 2\n",
384       "Cloud Region for vFW = 1\n",
385       "Objective function value: 6.062\n",
386       "Customer type: 2\n",
387       "----------\n",
388       "Solution: \n",
389       "Cloud Region for vDNS = 3\n",
390       "Cloud Region for vFW = 1\n",
391       "Objective function value: 6.4425\n",
392       "Customer type: 2\n",
393       "----------\n",
394       "==========\n"
395      ]
396     }
397    ],
398    "source": [
399     "!mzn-gecode -a placement.mzn silver.dzn"
400    ]
401   },
402   {
403    "cell_type": "code",
404    "execution_count": 10,
405    "metadata": {},
406    "outputs": [
407     {
408      "name": "stdout",
409      "output_type": "stream",
410      "text": [
411       "Solution: \n",
412       "Cloud Region for vDNS = 2\n",
413       "Cloud Region for vFW = 2\n",
414       "Objective function value: 5.278\n",
415       "Customer type: 2\n",
416       "----------\n",
417       "Solution: \n",
418       "Cloud Region for vDNS = 3\n",
419       "Cloud Region for vFW = 2\n",
420       "Objective function value: 5.6585\n",
421       "Customer type: 2\n",
422       "----------\n",
423       "Solution: \n",
424       "Cloud Region for vDNS = 3\n",
425       "Cloud Region for vFW = 3\n",
426       "Objective function value: 6.039\n",
427       "Customer type: 2\n",
428       "----------\n",
429       "==========\n"
430      ]
431     }
432    ],
433    "source": [
434     "!mzn-gecode -a placement.mzn silver-high-edge-load.dzn"
435    ]
436   },
437   {
438    "cell_type": "code",
439    "execution_count": null,
440    "metadata": {},
441    "outputs": [],
442    "source": []
443   }
444  ],
445  "metadata": {
446   "kernelspec": {
447    "display_name": "Python 3",
448    "language": "python",
449    "name": "python3"
450   },
451   "language_info": {
452    "codemirror_mode": {
453     "name": "ipython",
454     "version": 3
455    },
456    "file_extension": ".py",
457    "mimetype": "text/x-python",
458    "name": "python",
459    "nbconvert_exporter": "python",
460    "pygments_lexer": "ipython3",
461    "version": "3.6.4"
462   }
463  },
464  "nbformat": 4,
465  "nbformat_minor": 2
466 }