Lecture 02
Lecture 02
Introduction to Optimization
Lecture 2
Run! Python, Run!
‣ Anaconda
‣ Gurobi
Anaconda (for Python) and Gurobi
• Anaconda
– It includes over 330 Python and R packages
– It includes Integrated Development Environment (Spyder) and
the leading web interactive notebook for data science (Jupyter).
• Gurobi
– A commercial optimization solver for linear
programming (LP), quadratic programming (QP), quadratically
constrained programming (QCP), mixed integer linear
programming (MILP), mixed-integer quadratic programming
(MIQP), and mixed-integer quadratically constrained
programming (MIQCP).
Back to LOP Example:
‣ Meaningful names
‣ Comments
• Strings
– A contiguous set of characters represented in the quotation
marks. e.g., “Hello World!”
Basic Arithmetic
• More examples on basic arithmetic operations
Boolean Variables
• Evaluated to True or False
• Combine Boolean expression using and, or
• Flip Boolean value using not
• Membership: use in, not in
Boolean Expression
‣ Comparison operators
BEFORE TYPE
CONVERSION
Type Conversion
BEFORE TYPE
CONVERSION
AFTER TYPE
CONVERSION
Compound Types
• List
– e.g., [‘abcd’, 786, 2.23, ‘john’, 70.2]
• Tuple
– e.g., (‘abcd’, 786, 2.23, ‘john’, 70.2)
• Dictionary
– e.g., {‘cat’: ‘cute’, ‘dog’: ‘furry’}
• Set
– e.g., {‘cat’, ‘dog’}
List
• A list is the Python equivalent of an array, but is resizeable and can
contain elements of different types.
• Note that the first index is 0
‣ append, extend
1 2 3 4
the_list
0 1 2 3
Python Lists
• List Methods
‣ append, extend
‣ append, extend
‣ Slicing expression
[start:stop:step]
keys to
access values
Set
• A set is an unordered collection of distinct elements (same as the
corresponding concepts in math)
Conditional Statement
• Test if a Boolean expression is True or False and run
different code in each case
• Can split the code into more than two cases
For Loops
• More examples
While Loops
• math
• random
Vector, Matrix, and Array
• For Linear Algebra use, you need to install and import numpy
module.
If you want to rewrite the file from the beginning, use ‘w’ or ‘r+’. For
more details, see
https://www.tutorialspoint.com/python3/python_files_io.htm
https://docs.python.org/3.6/tutorial/inputoutput.html
File Input/Output
• Read CSV file