PA 2.4A: Gurobi Environment and License

No description has been provided for this image No description has been provided for this image

CEGM1000 MUDE: Week 2.4. Due: complete this PA prior to class on Friday, Dec 6, 2024.

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.

Task 1:

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 in the book.

Task 2:

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.

You don't need to understand what the cells are doing, but we wrote a few notes to explain anyway.

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.

In [1]:
import gurobipy
model = gurobipy.Model()
x = model.addVars(3000, vtype = gurobipy.GRB.CONTINUOUS, name = 'x')
model.update()
model.optimize()
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

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.

In [2]:
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!")
License succesfully found and processed!

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.

Task 3:

Commit this notebook and the license file that it created to your repository.

End of notebook.

© Copyright 2024 MUDE TU Delft. This work is licensed under a CC BY 4.0 License.