Skip to content
Snippets Groups Projects
Commit f375b6a0 authored by Robert Lanzafame's avatar Robert Lanzafame
Browse files

PA 2.4

parent 05aa9ad8
No related branches found
No related tags found
1 merge request!25Main
%% Cell type:markdown id:c96d6259-08d6-4289-aea2-589d67cdb5ee tags:
# PA 2.4.A: Gurobi Environment and License
# PA 2.4A: Gurobi Environment and License
<h1 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; top: 60px;right: 30px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png" style="width:100px" />
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png" style="width:100px" />
</h1>
<h2 style="height: 10px">
</h2>
*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 2.4. Due: complete this PA prior to class on Friday, Dec 6, 2024.*
%% Cell type:markdown id:7a28e541-d2d0-48a9-abf6-7b73075c8fd3 tags:
## Overview of Assignment
This assignment confirms you were able to create and activate a Python environment using Anaconda from an `environment.yml` file, and that your Gurobi license has been set up properly.
**Remember:** PA 2.4 has two notebooks that must be completed (A and B). Follow the instructions in **`README.md`** if you have not already done so.
## Assignment Criteria
**You will pass this assignment as long as your repository fulfills the following criteria:**
- You have completed this notebook and it runs without errors
- You commit the file `license.lic` file to your repository (it is generated as part of this assignment to confirm you've installed Gurobi properly)
- Don't forget the criteria in the other notebook of PA 2.4.B
%% Cell type:markdown id:46929e28-a4ce-424c-b446-9f00472997dc tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%">
<p>
<b>Task 1:</b>
Apply for your personal license for Gurobi (one of the packages installed in `environment.yml`) and add the license file to your computer (in the default folder!). The instructions for this are on ENTER PLACE HERE.
</p>
</div>
%% Cell type:markdown id:ab857833 tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%">
<p>
<b>Task 2:</b>
Run the cells below. If you have correctly created the Python environment (as described in the README.md) and installed the Gurobi license, there should be no errors. If there are errors, use the Traceback to figure out what should be fixed.
<em>You don't need to understand what the cells are doing, but we wrote a few notes to explain anyway.</em>
</p>
</div>
%% Cell type:markdown id:00728bea tags:
This cell sets up an optimization model with 3000 variables. That's a lot! We will do something like this in the optimization week. Since you need a license to process this many variables, an error will be returned if you did not install it correctly.
%% Cell type:code id:7c5b1a71-7e0a-47d7-9037-c13d635fdff0 tags:
``` python
import gurobipy
model = gurobipy.Model()
x = model.addVars(3000, vtype = gurobipy.GRB.CONTINUOUS, name = 'x')
model.update()
model.optimize()
```
%% Output
Set parameter Username
Set parameter LicenseID to value 2588551
Academic license - for non-commercial use only - expires 2025-11-21
Gurobi Optimizer version 12.0.0 build v12.0.0rc1 (win64 - Windows 11.0 (22631.2))
CPU model: 13th Gen Intel(R) Core(TM) i7-1365U, instruction set [SSE2|AVX|AVX2]
Thread count: 10 physical cores, 12 logical processors, using up to 12 threads
Optimize a model with 0 rows, 3000 columns and 0 nonzeros
Model fingerprint: 0x22a73ca6
Coefficient statistics:
Matrix range [0e+00, 0e+00]
Objective range [0e+00, 0e+00]
Bounds range [0e+00, 0e+00]
RHS range [0e+00, 0e+00]
Presolve removed 0 rows and 3000 columns
Presolve time: 0.01s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 0.0000000e+00 0.000000e+00 0.000000e+00 0s
Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective 0.000000000e+00
%% Cell type:markdown id:4d6029f7 tags:
The cell below searches for the license file `gurobi.lic` on your computer, and will create a new file `license.lic` in the working directory of this notebook to confirm that you installed Gurobi correctly.
%% Cell type:code id:8b221048-69bb-46ed-8a19-311ba288345a tags:
``` python
import sys
from pathlib import Path
import os
def find_license_in_dir(directory: Path):
license = directory / "gurobi.lic"
if (license.exists()):
return license
else:
return None
def find_license():
# By default the license is installed in the home directory; this is the most likely location.
license = find_license_in_dir(Path.home())
if (license): return license
# Otherwise there are other default paths Gurobi will search for each platform.
if (sys.platform.startswith("linux")):
license = find_license_in_dir(Path("/opt/gurobi/"))
elif (sys.platform.startswith("win32")):
license = find_license_in_dir(Path("C:\\gurobi\\"))
elif (sys.platform.startswith("darwin")):
license = find_license_in_dir(Path("/Library/gurobi/"))
else:
print("WARNING: Your operating system may not be supported by this function")
if (license): return license
# If all else fails, maybe it was put somewhere strange and the GRB_LICENSE_FILE environment variable was set
file_path = os.environ.get("GRB_LICENSE_FILE")
if (file_path is not None):
file_path = Path(file_path)
if (file_path.exists()):
return file_path
# Oh nO!
raise Exception(("Could not find license. If you have an academic license and "
"it couldn't be found, copy the license into your repository and "
"remove all the info except 'TYPE' and 'VERSION'"))
license = find_license()
with open("license.lic", "w") as f:
f.write(
"".join(
filter(
lambda l: l.startswith("TYPE") or l.startswith("VERSION") or l.startswith("EXPIRATION"),
license.open().readlines()
)
)
)
print("License succesfully found and processed!")
```
%% Output
License succesfully found and processed!
%% Cell type:markdown id:133c3daf-f0b9-4f80-ae6a-32b1ceae3aeb tags:
If you ran all of the cells above, you are ready to go: you successfully created an environment from a `*.yml` file and installed the Gurobi license! Now there is only one thing left to do.
%% Cell type:markdown id:5ef8955f tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%">
<p>
<b>Task 3:</b>
Commit this notebook and the license file that it created to your repository.
</p>
</div>
%% Cell type:markdown id:80580ab9-4d79-46b1-ae6e-775af04d43ad tags:
**End of notebook.**
<h2 style="height: 60px">
</h2>
<h3 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; bottom: 60px; right: 50px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
<img alt="Creative Commons License" style="border-width:; width:88px; height:auto; padding-top:10px" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
</a>
<a rel="TU Delft" href="https://www.tudelft.nl/en/ceg">
<img alt="TU Delft" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png"/>
</a>
<a rel="MUDE" href="http://mude.citg.tudelft.nl/">
<img alt="MUDE" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png"/>
</a>
</h3>
<span style="font-size: 75%">
&copy; Copyright 2023 <a rel="MUDE Team" href="https://studiegids.tudelft.nl/a101_displayCourse.do?course_id=65595">MUDE Teaching Team</a> TU Delft. This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
......
%% Cell type:markdown id:c96d6259-08d6-4289-aea2-589d67cdb5ee tags:
# Programming Assignment 12B: [Axis of Awesome](https://youtu.be/5pidokakU4I?si=Y5ewcgPFFQ5cLmC6)
# PA 2.4B: [Axis of Awesome](https://youtu.be/5pidokakU4I?si=Y5ewcgPFFQ5cLmC6)
<h1 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; top: 60px;right: 30px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png" style="width:100px" />
<img src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png" style="width:100px" />
</h1>
<h2 style="height: 10px">
</h2>
*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 2.4. Due: complete this PA prior to class on Friday, Dec 8, 2023.*
*[CEGM1000 MUDE](http://mude.citg.tudelft.nl/): Week 2.4. Due: complete this PA prior to class on Friday, Dec 6, 2024.*
%% Cell type:markdown id:a5f54f96 tags:
## Overview of Assignment
This assignment quickly introduces you to making computations across rows and columns of 2-dimensional Numpy arrays (matrices) using the `axis` keyword argument. It also illustrates the use of a specific figure from the `statsmodels` package that will be useful during the time series analysis week.
**Remember:** PA12 has two notebooks that must be completed (A and B). Follow the instructions in **`README.md`** if you have not already done so.
## Assignment Criteria
**You will pass this assignment as long as your repository fulfills the following criteria:**
- You have completed this notebook and it runs without errors
- Don't forget the criteria in the other notebook of PA12
**Remember:** PA 2.4 has two notebooks that must be completed (A and B). Follow the instructions in **`README.md`** if you have not already done so.
%% Cell type:code id:75681df5-7a73-469a-ad83-ebd05451e0b7 tags:
``` python
import numpy as np
import matplotlib.pyplot as plt
```
%% Cell type:markdown id:b0584fa2-7f4b-4566-9217-e6ca58d0e191 tags:
## Introduction
Often when we have a long sequence of data we would like to evaluate specific sub-sets of it. For example, if we have 10 years worth of hourly measurements, but would like to evaluate the monthly or weekly characteristics. Often we can store our data in a structured way and then use some indexing capabilities of Numpy to evaluate it in a smart way. Check out the following simple tips, then try and apply it in practice.
%% Cell type:markdown id:e2ac6a6f tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 1:</b>
Read and run the cells below, making sure you understand what is happening (i.e., completing evaluations on the rows and columns of the matrix).
</p>
</div>
%% Cell type:markdown id:9130a7b8 tags:
First, let's start by collecting a "long" sequence of data.
%% Cell type:code id:2e644bc5 tags:
``` python
A = np.array([1, 20, 300, 1, 2, 3])
print(A)
```
%% Cell type:markdown id:cb0d4cb7 tags:
It is easy to restructure it into a matrix form; in this case, 2 rows and 3 columns.
%% Cell type:code id:56b43c1c tags:
``` python
B = np.reshape(A, (2, 3))
print(B)
```
%% Cell type:markdown id:17c187e4 tags:
In Numpy, "axes" are used to specify the structure of an array using the `axis` keyword argument. For this assignment, we are particularly interested in performing operations along the 0th and 1st axes of the array, which correspond to the columns and rows, respectively. Check it out:
%% Cell type:code id:b8b8875b tags:
``` python
B.mean(axis=0)
```
%% Cell type:markdown id:f472e87a tags:
Looking along the other axis:
%% Cell type:code id:c4f093fa tags:
``` python
B.mean(axis=1)
```
%% Cell type:markdown id:9f46d8a8 tags:
And you can do it for other methods too!
%% Cell type:code id:0d9a4cc6 tags:
``` python
B.std(axis=1)
```
%% Cell type:markdown id:41269037-161e-4c50-9dd7-5346bfc41b7a tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 2:</b>
Read the simple story below and use the tips described above to complete the partially completed code cell.
</p>
</div>
%% Cell type:markdown id:5692fa18 tags:
Suppose you and a group of friends would like to evaluate your financial decisions, and you decide to review how many coins you spend at PSOR, the CEG student pub, to practice your Python skills. You have assembled data on the number of coins purchased per month, for several years, starting from when you first met in September, through August, 3 years later.
%% Cell type:code id:f64b31f1 tags:
``` python
coins = [46, 28, 16, 27,
22, 24, 31, 12,
32, 36, 12, 0,
41, 27, 21, 26,
21, 19, 18, 35,
14, 34, 8, 0,
53, 34, 23, 35,
28, 26, 18, 13,
12, 14, 34, 0]
```
%% Cell type:code id:4415d492 tags:
``` python
np.set_printoptions(precision=1)
print(f'The average number of coins spent per month is:')
print('The average number of coins spent per month for each year is:')
print(f'The average number of coins spent each september:')
print(f'The average number of coins spent each january:')
print(f'Max coins spent in any month:')
print(f'Max coins spent in any year:')
```
%% Cell type:markdown id:81e30bca tags:
The answers are:
```
The average number of coins spent per month is: 23.3
The average number of coins spent per month for each year is: [23.8 22. 24.2]
The average number of coins spent in september: 46.7
The average number of coins spent in january: 23.7
Max coins spent in any month: 53.0
Max coins spent in any year: 290.0
```
%% Cell type:markdown id:adbdb77a tags:
## Correlated Behavior?
Now that we have three years of data, we want to see if there is a trend in our behavior. We can take the correlation concepts we learned in Q1 and see if there is a relationship from one month to the next. In other words: if we spend a lot of coins one month, is the _probability_ that we spend a lot of coins the next month _higher_? Evaluating correlation this way has many names: autocorrelation, autocovariance, etc...the "auto" in this case refers to evaluating the relationship between data from the same sequence (you will learn about it more in the time series reading). Luckily, there is a built-in method of `statsmodels` that does this for us automatically: the correlation is plotted on the y-axis between the distance away from any point in the series. It is important to recognize that the correlations represent any point in the series (analogous to an average), rather than any specific point)
_Note the use of reshape to put the data back into a 1D array (row/column)!_
%% Cell type:markdown id:491cc105 tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 3:</b>
Run the cells to visualize the plots, then read the interpretations.
</p>
</div>
%% Cell type:code id:12049ec2 tags:
``` python
from statsmodels.graphics.tsaplots import plot_acf
plot_acf(coins.reshape(-1));
```
%% Cell type:markdown id:3f68eaf0 tags:
**Interpretation:** it appears that there is little correlation from one month to the next in the data set, except each point is very strongly correlated with itself (that should be obvious!). Even more importantly, the points are inside the shaded blue region: that is the confidence interval, which means the values are negligible.
Now let's try an increasing series and see what happens:
%% Cell type:code id:984e38b0 tags:
``` python
increasing_series = np.arange(1, 50)
plot_acf(increasing_series);
```
%% Cell type:markdown id:ac2d7925 tags:
**Interpretation:** now there is definitely correlation! And we see that the correlation drops off with distance (which makes sense for a linear trend). However, the confidence interval is large, so the trend in correlation should not be trusted for a large distance (you can check and set the confidence interval easily, check the [documentation](https://www.statsmodels.org/stable/generated/statsmodels.graphics.tsaplots.plot_acf.html)).
%% Cell type:markdown id:f0a19416 tags:
<div style="background-color:#facb8e; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px; width: 95%"> <p>Note that we don't give a full explanation of the x-axis in these plots, and the terminology for correlation is vague; see the reading for a thorough explanation.</p></div>
%% Cell type:markdown id:51c5ff8f tags:
<div style="background-color:#AABAB2; color: black; vertical-align: middle; padding:15px; margin: 10px; border-radius: 10px">
<p>
<b>Task 4:</b>
Test your knowledge of correlation! See if you can create a sequence of data that somehow creates alternating positive and negative values of autocorrelation, as illustrated in the figure. You don't need to exactly recreate the plot, as long as there's alternating values autocorrelation.
</p>
</div>
%% Cell type:markdown id:1781bd5c tags:
![Image of alternating autocorrelation plot](alternation.svg)
%% Cell type:code id:95c7bf59 tags:
``` python
strong_autocorr_positive = np.array([YOUR_CODE_HERE])
plot_acf(strong_autocorr_positive);
```
%% Cell type:markdown id:80580ab9-4d79-46b1-ae6e-775af04d43ad tags:
**End of notebook.**
<h2 style="height: 60px">
</h2>
<h3 style="position: absolute; display: flex; flex-grow: 0; flex-shrink: 0; flex-direction: row-reverse; bottom: 60px; right: 50px; margin: 0; border: 0">
<style>
.markdown {width:100%; position: relative}
article { position: relative }
</style>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
<img alt="Creative Commons License" style="border-width:; width:88px; height:auto; padding-top:10px" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
</a>
<a rel="TU Delft" href="https://www.tudelft.nl/en/ceg">
<img alt="TU Delft" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/tu-logo/TU_P1_full-color.png"/>
</a>
<a rel="MUDE" href="http://mude.citg.tudelft.nl/">
<img alt="MUDE" style="border-width:0; width:100px; height:auto; padding-bottom:0px" src="https://gitlab.tudelft.nl/mude/public/-/raw/main/mude-logo/MUDE_Logo-small.png"/>
</a>
</h3>
<span style="font-size: 75%">
&copy; Copyright 2023 <a rel="MUDE Team" href="https://studiegids.tudelft.nl/a101_displayCourse.do?course_id=65595">MUDE Teaching Team</a> TU Delft. This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
......
......@@ -9,23 +9,26 @@ _[CEGM1000 MUDE](http://mude.citg.tudelft.nl/), Time Series Analysis, Week 4 of
This week the programming assignment is based on three files:
- `README.md`: instructions for PA12 and environment creation (this document)
- `PA_2_$_A_gurobilicious.ipynb`: set-up of Gurobi software for Week 2.5 (optimization) with a new conda environment
- `PA_2_4_A_gurobilicious.ipynb`: set-up of Gurobi software for Week 2.5 (optimization) with a new conda environment
- `PA_2_4_B_axes_of_awesome.ipynb`: Python-related tools used in Week 2.4 for Time Series Analysis
## Instructions
Read through this file (`README.md`). This `README.md` also includes a task on creating a new conda environment. Afterwards complete the tasks in notebooks `PA.2.4.A` and `PA.2.4.B`. Note that `PA.2.2.A` will refer you to the MUDE website, where you will find [instructions for setting up the Gurobi license file UPDATE LINK](https://mude.citg.tudelft.nl/software/gurobi/).
Read through this file (`README.md`). This `README.md` also includes a task on creating a new conda environment. Afterwards complete the tasks in notebooks `PA_2_4_A` and `PA_2_4_B`. Note that `PA_2_4_A` will refer you to the MUDE website, where you will find [instructions for setting up the Gurobi license file UPDATE LINK](https://mude.citg.tudelft.nl/software/gurobi/).
### Assessment criteria
## Grading Criteria
The assessment criteria for PA12 is specified in each notebook, respectively, but the general idea is to make sure both notebooks run without errors. In addition, notebook 2.2.A will generate a file `license.lic` that must be committed to your repository to confirm that the Gurobi software has been installed correctly on your computer.
You will pass this PA if:
1. Your notebook `PA_2_4_B_axis_of_awesome.ipynb` runs without errors.
2. A file `license.lic` is committed to your repository
3. The license file confirms you have installed the academic license of Gurobi version 12
## Python environments revisited
Note that we won't be checking your notebook A for errors, as this will not run on the autograder webserver as it does not have Gurobi installed.
Until now, we have been able to complete our work in MUDE with a few packages like `numpy` and `scipy` in our `mude` environment, which we create and manage with `conda`. In the previous quarter you also created a new environment, 'mude-week-8'. Please revisit the PA for week 1.7 if you need a refresher on this topic.
## Python environments revisited
This week, we will once again create a new environment in preparation for the week on Optimisation ahead. You may have noticed that the environments are always generated using a file called 'environment.yml'. This is just a text-based format (see below) where we can list the packages that we want to be included in our environment. We can then tell `conda` to create the environment based on the contents of the file!
Until now, we have been able to complete our work in MUDE with a few packages like `numpy` and `scipy` in our `mude` environment, which we create and manage with `conda`. In the previous quarter you also created a new environment, 'mude-week-8'. This week, we will once again create a new environment in preparation for the week on Optimisation ahead. You may have noticed that the environments are always generated using a file called 'environment.yml'. This is just a text-based format (see below) where we can list the packages that we want to be included in our environment. We can then tell `conda` to create the environment based on the contents of the file!
All we need to do to create an environment from a file is to write a list of what we want and then tell `conda` to read it. That's it!
......
name: mude-week-2-5
dependencies:
- python=3.11
- python=3.12
- numpy
- scipy
- matplotlib
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment