0% found this document useful (0 votes)
2 views37 pages

02b - Optimasi II - Algorithm and Python - 2024

The document outlines a training schedule for optimization techniques, including modeling in Python and various case studies such as gritting roads. It covers the minimum cost network flow problem and provides an overview of Python programming concepts, data types, control structures, and how to build and optimize models using Python-MIP. The training includes practical assignments and discussions to reinforce learning.

Uploaded by

wisnu fitraddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

02b - Optimasi II - Algorithm and Python - 2024

The document outlines a training schedule for optimization techniques, including modeling in Python and various case studies such as gritting roads. It covers the minimum cost network flow problem and provides an overview of Python programming concepts, data types, control structures, and how to build and optimize models using Python-MIP. The training includes practical assignments and discussions to reinforce learning.

Uploaded by

wisnu fitraddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Optimization II

Part02b – algorithm and python


[email protected]
Topics and schedule
1. Day 01a – intro to optimization 4. Day 02b – Modeling in Python
and geometric analysis • Python review
• [Pre-test] • Python practice: Assignment and
• Types of optimization problems transportation
• Motivation and case studies • Case 2: gritting roads
• Mathematical Notation • [Assignment 2]
2. Day 01b – Modeling 5. Day 03a – Modeling in Python
techniques • Python practice: Optimizer
• Modeling in Excel • [Assignment 3]
• Excel practice: Assignment and 6. Day 03b – Wrap-up
transportation
• [Post-test]
• Case 1: Optimizer
• Modeling techniques (1)
• [Assignment 1]
3. Day 02a – Modeling in Excel
• Discussion on Case 1
• Modeling techniques (2)
Case 2: Gritting roads
• In the case of ice, all the streets of a village need to be gritted. A
schematic map of the streets is given in Figure below. The nodes
correspond to street intersections, the arcs to the streets that need
to be gritted. The arcs are labeled with the length of the street in
meters.
• The highway maintenance depot with the gritting truck is located at
intersection number 1. The truk has a sufficiently large capacity to
grit all the streets during a single tour. Due to the one-way streets, it
may be forced to pass several times through the same street that has
already been gritted.
• Determine a tour for the gritting truck of minimum total length that
passes through all streets. For bidirectional streets, both directions
need to be gritted separately.
Case 6: Gritting roads
Modeling options

• The sequence to grit the roads


• Combinatorics → n!

• The number of times the roads getting gritted


• # decision variables: n

• Integrality constraint?
• Minimum cost network flow (MCNF) problem
Minimum cost network flow problem

▪ Consider a weighted capacitated network 𝐺 = (𝑉, 𝐸).


▪ 𝐺 is the network, 𝑉 is the set of nodes, and 𝐸 is the set of arcs
▪ For node 𝑖 ∈ 𝑉, there is a supply quantity 𝑏𝑖 .
▪ 𝑏𝑖 > 0: 𝑖 is a supply node.
▪ 𝑏𝑖 < 0: 𝑖 is a demand node.
▪ 𝑏𝑖 = 0: 𝑖 is a transshipment node.
▪ σ𝑖∈𝑉 𝑏𝑖 = 0: total supplies equal total demands.
▪ For arc (𝑖, 𝑗) ∈ 𝐸, the weight 𝑐𝑖𝑗 ≥ 0 is the cost of each unit of flow.
▪ How to satisfy all demands by sending a minimum-cost flow from
supplies?
▪ This is called the minimum cost network flow (MCNF) problem.
OR III: NETWORK FLOW

6
Python language and how to run it

Basic data types and syntax

Review Control structures

python
Anaconda Jyupiter, Google
Important data structures in Python:
Colabs, Spyder Python, etc lists (strings), tuples, and dictionaries

Function declarations
The Python Programming Language

• Dynamically vs. statically typed


• Automated memory management
• General purpose programming / scripting language
• Thanks to a ton of modules and libraries
• https://pypi.python.org/
Python Interactive Shell using Jupyter
Python on command line

• Collect Python code into a text file


• Save it with .py suffix, say script.py
• Open a terminal on your machine
• Then type: python script.py Arg1 Arg2 …
Python Data Types & Syntax
• Numerical data types
• a = 34 (integer)
• a = 34.0 (floating point numbers – single & double precision)
• a = True (boolean)

• Characters are strings of length 1 (More on strings later!)

• c = “u” OR c = ‘u’

• Converting (casting) from one type to another


• int(), float(), double(), boolean(), str()
• i = int(“3”)

• Check type of variables with type()


• type(34) = int
Python Data Types & Syntax (cont’d)
• Whitespace matters!
• Think curly braces ( ‘{‘ ) in C or Java
• All lines with the same indentation are part of the same
block/scope
• Example:
if check == 1:
do something...
else:
if check2 == 1:
do something...

• Comments (start with ‘#’)


• Example:
if check == 1:
# comments can be on their own line
do something... # or inline with code
Mathematical & Logical Operations
• Addition, subtraction, multiplication
o 2 + 2, 42 – 6, 4*3
• Division (type matters)
o 3/2=?
• Modulus
o 3%2=1
• Exponentiation
o 3 ** 2 = 9
• Logical operators
o and, or, not
o ==, !=, <, <=, >, >=
print Statement
• Places output on the screen
i = 34
print(i)

• Use comma between variables you want


printed on the same line (i.e., comma will
suppress a newline)
print(i)
print(i,j)

• Useful for debugging!


Control Structures
• if-else statement:
if check == 1:
do something...
elif (check == 2) and (not embarrassed):
do something...
else:
at least do something...

• while statement
while i < 40:
do something
Control Structures (cont’d)
• for statement
for i in [1,2,4]:
print(i)

• break and continue


for i in range(50):
if (i == 0) or (i == 1):
continue

for i in range(50):
if i == 10:
break

• http://docs.python.org/2/tutorial/controlflow.html
Lists
• Creating a list
list0 = [] # create empty list manually

list1 = ['a', 1, 324.3] # create with values

list2 = list(list1) # creates a copy of list1

• Lists are mutable – can be changed in place


list1 = ['a', 1, 324.3]
list1[0] = 'b'
del list1[1]

• Can iterate over lists with for loops


for item in list1:
print(item)

• List can group many different data types

a = [99, 'bottles of love', ['on', 'the', 'wall']]


Lists (cont’d)
• List comprehension
[str(x) for x in [1,2,3]]
→ ['1', '2', '3']

• Slicing
a = [99, 'bottles of love', ['on', 'the', 'wall']]
print(a[0:2])
→ [99, 'bottles of love']
print(a[1:])
→ ['bottles of love', ['on', 'the', 'wall']]

• Reverse indexing
print(a[-1])
→ ['on', 'the', 'wall']
print(a[-3:-1])
→ [99, 'bottles of love']

• Delete elements
del a[1]
print(a)
→ [99, ['on', 'the', 'wall']]
Lists (cont’d)
a = [0, 1, 2]
b = [3, 4]
a + b →
a * 3 →

a.append(5) → [0, 1, 2, 5]
a.pop(1) → [0, 2, 5]
a.insert(1, 42) → [0, 42, 2, 5]
a.reverse() → [5, 2, 42, 0]
a.sort() → [0, 2, 5, 42]
sorted(a) → [0, 2, 5, 42]

print(len(a))
→4
Strings
A string is similar to a list of characters
a = 'hello'
print(a[0])
→ 'h'
print(a[1:4])
→ 'ell'
print(a[-1])
→ 'o'

for c in a:
print(c)
→h e l l o

• But a string is immutable


• Test: Try to change a single character in a string variable
a[0] = 'j'
Strings
To create a string:
strvar1 = 'abc'

strvar2 = str(123) # can cast objects as strings

strvar5 = ''
strvar5 += 'cr'
strvar5 += 'ude' # concatenation

String formatting
# using string formatting
strvar3 = 'Pi is about %.4f' % 3.142951
→ 'Pi is about 3.1430’

# more formatted strings


strvar4 = '%s Student #%d!' % ('Hello',42)
→ 'Hello Student #42!'
String Operations
'hello' + 'world' → 'helloworld' # concatenation

'hello' * 3 → 'hellohellohello' # repetition

'hello'[::-1] → 'olleh' # reversing by slice

len('hello') → 5 # size

'hello' < 'jello' → 1 # comparison

'e' in 'hello' → True # membership

'hello'.find('lo') → 3 # finding substrings

'hello_world'.count('o') → 2 # counting substrings

# splitting strings
'hello_world'.split('_') → ['hello','world']

# remove whitespace
'hello_world \n'.strip() → 'hello_world'
Tuples
• Create a tuple
tup = ()
tup = ('32', 4, 'yes', 3.14)

• Quite similar to a list


tup[1:4]
→ (4, 'yes', 3.14)

• But tuples are immutable


tup[1] = 12 → error

• http://docs.python.org/2/library/functions.html#tuple
Dictionaries
• Dictionary are "associative arrays" mapping keys to
values: {key: value}
d = {
'Marky':'Mark', 'Funky':'Bunch', 3:'4', (1,2):[1,2,3]
}

• Dictionary assignment & operations


print(d.keys()) → [(1, 2), 'Funky', 3, 'Marky']
print(d.values()) → [[1, 2, 3], 'Bunch', '4', 'Mark']
print(d.items())
→ [((1, 2), [1, 2, 3]), ('Funky', 'Bunch'), (3, '4'),
('Marky', 'Mark')] # a list of key-value pairs in tuples

print(d['Marky']) → 'Mark'
d['Donnie'] → error # raises KeyError exception
d['Donnie'] = 'Wahlberg' # value assignment

# check for presence of key # iterate over keys


d.has_key('Donnie') for dkey in d:
# item deletion print(dkey)
del d['Marky']
Function Declarations
def func1(arg1, arg2):
function statements
return val # Optional

• Functions are pass-by-(object)-reference

def f1(a):
a.append(4)

b = [1,2,3]
f1(b)

def f2(a):
a = 4

b = [1,2,3]
f2(b)
Pointers (or Reference vs. Copy)

• Suppose you perform the following:


list1 = [1,3,4]
list2 = list1
list3 = list(list1)
list3.append(6)
list2.append(5)

o What are list1, list2, and list3 now?

• Be careful not to confuse references with copies


o Use casting functions like str(), list() when you need to make copies
o For dictionaries, use copy() and deepcopy()
Build and optimize models
using Python-MIP
Build and optimize models using Python-MIP (1)

• The first step to enable Python-MIP in your Python code is to add:


from mip import *

• Create an empty Mixed-Integer Linear Programming problem with


default settings.
m = Model()

• Another option:
m = Model(sense=MAXIMIZE, solver_name=CBC) # use
GRB for Gurobi
Build and optimize models using Python-MIP (2)

• Decision variables are added to the model using the add_var()


method. Without parameters, a single variable with domain in R+ is
created:
x = m.add_var()

• We can create a vector variable:


n = 10
y = [ m.add_var(var_type=BINARY) for i in range(n) ]

• Additional variable types are CONTINUOUS (default) and INTEGER.


z = m.add_var(name='zCost',
var_type=INTEGER, lb=-10, ub=10)
vz = m.var_by_name('zCost')
vz.ub = 5
Build and optimize models using Python-MIP (3)

• Constraints are linear expressions involving variables, a sense of ==,


<= or >= for equal, less or equal and greater or equal, respectively,
and a constant.
m += x + y <= 10

• Summation expressions can be implemented with the function


xsum().
m += xsum(w[i]*x[i] for i in range(n)) <= c

• Conditional inclusion of variables in the summation is also easy. Let’s


say that only even indexed items are subjected to the capacity
constraint:
m += xsum(w[i]*x[i] for i in range(n) if
i%2 == 0) <= c
Build and optimize models using Python-MIP (4)

• Finally, it may be useful to name constraints.


m += xsum(w[i]*x[i] for i in range(n) if i%2 == 0) <=
c, 'even_sum'

• By default a model is created with the Minimize sense.


m.objective = xsum(c[i]*x[i] for i in range(n))

• Specify minimize of maximize


m.objective = minimize(xsum(c[i]*x[i] for i in
range(n)))
m.objective = maximize(xsum(c[i]*x[i] for i in
range(n)))
Build and optimize models using Python-MIP (5)

• Saving, Loading and Checking Model Properties.


• Supported file formats for models are the LP file format, which is
more readable and suitable for debugging, and the MPS file format,
which is recommended for extended compatibility, since it is an older
and more widely adopted format.
m.write('model.lp')

m.read('model.lp')
print('model has {} vars, {} constraints and {}
nzs'.format(m.num_cols, m.num_rows, m.num_nz))
Build and optimize models using Python-MIP (6)
• Optimizing and Querying Optimization Results
• The optimize() method, that executes the optimization of a
formulation, accepts optionally processing limits as parameters.
m.max_gap = 0.05
status = m.optimize(max_seconds=300)
if status == OptimizationStatus.OPTIMAL:
print('optimal solution cost {}
found'.format(m.objective_value))
elif status == OptimizationStatus.FEASIBLE:
print('sol.cost {} found, best possible:
{}'.format(m.objective_value, m.objective_bound))
elif status == OptimizationStatus.NO_SOLUTION_FOUND:
print('no feasible solution found, lower bound is:
{}'.format(m.objective_bound))
if status == OptimizationStatus.OPTIMAL or status ==
OptimizationStatus.FEASIBLE:
print('solution:')
for v in m.vars:
if abs(v.x) > 1e-6: # only printing non-zeros
print('{} : {}'.format(v.name, v.x))
Build and optimize models using Python-MIP (7)
The optimize() method returns the status (OptimizationStatus) of the BC
search:
• OPTIMAL if the search was concluded and the optimal solution was
found;
• FEASIBLE if a feasible solution was found but there was no time to prove
whether this solution was optimal or not;
• NO_SOLUTION_FOUND if in the truncated search no solution was found;
• INFEASIBLE or INT_INFEASIBLE if no feasible solution exists for the
model;
• UNBOUNDED if there are missing constraints or
• ERROR if some error occurred during optimization.

for k in range(model.num_solutions):
print('route {} with length {}'.format(k,
model.objective_values[k]))
for (i, j) in product(range(n), range(n)):
if x[i][j].xi(k) >= 0.98:
print('\tarc ({},{})'.format(i,j))
Hands-on python for
optimization

Knapsack problem
from mip import Model, xsum,
maximize, BINARY, CBC
p = [10, 13, 18, 31, 7, 15]
w = [11, 15, 20, 35, 10, 33]
c, I = 47, range(len(w))
m = Model("knapsack",
Knapsack solver_name=CBC)

problem x = [m.add_var(var_type=BINARY)
for i in I]

using mip m.objective =


maximize(xsum(p[i] * x[i]
for i in I))
library m += xsum(w[i] * x[i] for i in
I) <= c
m.optimize()
selected = [i for i in I if
x[i].x >= 0.99]
print("selected items:
{}".format(selected))
Optimization II

Part02b – algorithm and python


[email protected]

You might also like