Skip to content

Commit

Permalink
Run black over codebase (#430)
Browse files Browse the repository at this point in the history
* Run black over codebase

* Added pre commit hook
  • Loading branch information
rimaddo committed May 2, 2021
1 parent a587e8e commit a293fff
Show file tree
Hide file tree
Showing 55 changed files with 3,548 additions and 2,280 deletions.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
40 changes: 21 additions & 19 deletions doc/KPyCon2009/code/wedding.py
Expand Up @@ -8,43 +8,45 @@

max_tables = 5
max_table_size = 4
guests = 'A B C D E F G I J K L M N O P Q R'.split()
guests = "A B C D E F G I J K L M N O P Q R".split()


def happiness(table):
"""
Find the happiness of the table
- by calculating the maximum distance between the letters
"""
return abs(ord(table[0]) - ord(table[-1]))

#create list of all possible tables
possible_tables = [tuple(c) for c in pulp.allcombinations(guests,
max_table_size)]

#create a binary variable to state that a table setting is used
x = pulp.LpVariable.dicts('table', possible_tables,
lowBound = 0,
upBound = 1,
cat = pulp.LpInteger)

# create list of all possible tables
possible_tables = [tuple(c) for c in pulp.allcombinations(guests, max_table_size)]

# create a binary variable to state that a table setting is used
x = pulp.LpVariable.dicts(
"table", possible_tables, lowBound=0, upBound=1, cat=pulp.LpInteger
)

seating_model = pulp.LpProblem("Wedding Seating Model", pulp.LpMinimize)

seating_model += sum([happiness(table) * x[table] for table in possible_tables])

#specify the maximum number of tables
seating_model += sum([x[table] for table in possible_tables]) <= max_tables, \
"Maximum_number_of_tables"
# specify the maximum number of tables
seating_model += (
sum([x[table] for table in possible_tables]) <= max_tables,
"Maximum_number_of_tables",
)

#A guest must seated at one and only one table
# A guest must seated at one and only one table
for guest in guests:
seating_model += sum([x[table] for table in possible_tables
if guest in table]) == 1, "Must_seat_%s"%guest
seating_model += (
sum([x[table] for table in possible_tables if guest in table]) == 1,
"Must_seat_%s" % guest,
)

seating_model.solve()

print("The choosen tables are out of a total of %s:"%len(possible_tables))
print("The choosen tables are out of a total of %s:" % len(possible_tables))
for table in possible_tables:
if x[table].value() == 1.0:
print(table)


28 changes: 13 additions & 15 deletions doc/KPyCon2009/code/whiskas.py
Expand Up @@ -3,36 +3,34 @@
"""
import pulp

#initialise the model
whiskas_model = pulp.LpProblem('The Whiskas Problem', pulp.LpMinimize)
# initialise the model
whiskas_model = pulp.LpProblem("The Whiskas Problem", pulp.LpMinimize)
# make a list of ingredients
ingredients = ['chicken', 'beef', 'mutton', 'rice', 'wheat', 'gel']
ingredients = ["chicken", "beef", "mutton", "rice", "wheat", "gel"]
# create a dictionary of pulp variables with keys from ingredients
# the default lower bound is -inf
x = pulp.LpVariable.dict('x_%s', ingredients, lowBound =0)
x = pulp.LpVariable.dict("x_%s", ingredients, lowBound=0)

# cost data
cost = dict(zip(ingredients, [0.013, 0.008, 0.010, 0.002, 0.005, 0.001]))
# create the objective
whiskas_model += sum( [cost[i] * x[i] for i in ingredients])
whiskas_model += sum([cost[i] * x[i] for i in ingredients])

# ingredient parameters
protein = dict(zip(ingredients, [0.100, 0.200, 0.150, 0.000, 0.040, 0.000]))
fat = dict(zip(ingredients, [0.080, 0.100, 0.110, 0.010, 0.010, 0.000]))
fibre = dict(zip(ingredients, [0.001, 0.005, 0.003, 0.100, 0.150, 0.000]))
salt = dict(zip(ingredients, [0.002, 0.005, 0.007, 0.002, 0.008, 0.000]))

#note these are constraints and not an objective as there is a equality/inequality
whiskas_model += sum([protein[i]*x[i] for i in ingredients]) >= 8.0
whiskas_model += sum([fat[i]*x[i] for i in ingredients]) >= 6.0
whiskas_model += sum([fibre[i]*x[i] for i in ingredients]) <= 2.0
whiskas_model += sum([salt[i]*x[i] for i in ingredients]) <= 0.4
# note these are constraints and not an objective as there is a equality/inequality
whiskas_model += sum([protein[i] * x[i] for i in ingredients]) >= 8.0
whiskas_model += sum([fat[i] * x[i] for i in ingredients]) >= 6.0
whiskas_model += sum([fibre[i] * x[i] for i in ingredients]) <= 2.0
whiskas_model += sum([salt[i] * x[i] for i in ingredients]) <= 0.4

#problem is then solved with the default solver
# problem is then solved with the default solver
whiskas_model.solve()

#print the result
# print the result
for ingredient in ingredients:
print('The mass of %s is %s grams per can'%(ingredient,
x[ingredient].value()))

print("The mass of %s is %s grams per can" % (ingredient, x[ingredient].value()))
70 changes: 37 additions & 33 deletions doc/source/_static/plotter.py
Expand Up @@ -2,51 +2,55 @@
# -*- encoding: utf-8 -*-

from matplotlib import rc
rc('text', usetex=True)
rc('font', family='serif')



def plot_interval(a,c,x_left, x_right,i, fbound):
lh = c*(1-a[0])
rh = c*(1+a[1])
x=arange(x_left, x_right+1)
y=0*x
arrow_r = Arrow(c,0, c*a[1],0,0.2)
arrow_l = Arrow(c,0,-c*a[0],0,0.2)
plot(x,y)
text((x_left+lh)/2.0,0.1,'freebound interval [%s, %s] is penalty-free' % (lh,rh))
text((x_left+lh)/2.0, 0.2, 'rhs=%s, %s' % (c, fbound))
rc("text", usetex=True)
rc("font", family="serif")


def plot_interval(a, c, x_left, x_right, i, fbound):
lh = c * (1 - a[0])
rh = c * (1 + a[1])
x = arange(x_left, x_right + 1)
y = 0 * x
arrow_r = Arrow(c, 0, c * a[1], 0, 0.2)
arrow_l = Arrow(c, 0, -c * a[0], 0, 0.2)
plot(x, y)
text(
(x_left + lh) / 2.0,
0.1,
"freebound interval [%s, %s] is penalty-free" % (lh, rh),
)
text((x_left + lh) / 2.0, 0.2, "rhs=%s, %s" % (c, fbound))
cur_ax = gca()
cur_ax.add_patch(arrow_l)
cur_ax.add_patch(arrow_r)
axis([x_left,x_right,-0.1,0.3])
axis([x_left, x_right, -0.1, 0.3])
yticks([])
title('Elasticized constraint\_%s $C(x)= %s $' % (i, c))
title("Elasticized constraint\_%s $C(x)= %s $" % (i, c))


figure()
subplots_adjust(hspace=0.5)

fbound = 'proportionFreeBound'
i=1
subplot(2,1,i)
a=[0.01,0.01]
fbound = "proportionFreeBound"
i = 1
subplot(2, 1, i)
a = [0.01, 0.01]
c = 200
x_left = 0.97*c
x_right = 1.03*c
fb_string = '%s%s = %s' %(fbound,'', a[0])
plot_interval(a,c,x_left, x_right,i, fb_string)
x_left = 0.97 * c
x_right = 1.03 * c
fb_string = "%s%s = %s" % (fbound, "", a[0])
plot_interval(a, c, x_left, x_right, i, fb_string)

i += 1
subplot(2,1,i)
a=[0.02, 0.05]
subplot(2, 1, i)
a = [0.02, 0.05]
c = 500
x_left = 0.9*c #scale of window
x_right = 1.2*c #scale of window
fb_string = '%s%s = [%s,%s]' % (fbound,'List', a[0],a[1])
plot_interval(a,c,x_left, x_right,i, fb_string)
savefig('freebound.jpg')
savefig('freebound.pdf')
x_left = 0.9 * c # scale of window
x_right = 1.2 * c # scale of window
fb_string = "%s%s = [%s,%s]" % (fbound, "List", a[0], a[1])
plot_interval(a, c, x_left, x_right, i, fb_string)
savefig("freebound.jpg")
savefig("freebound.pdf")

# vim: fenc=utf-8: ft=python:sw=4:et:nu:fdm=indent:fdn=1:syn=python

0 comments on commit a293fff

Please sign in to comment.