-
Notifications
You must be signed in to change notification settings - Fork 15
Opt Art 3: Linear Optimization and the Lego Problem
The meeting began with an abundance of drinks and snacks that everyone had kindly provided. In the time between meetings, a few people had built their own implementations of Flexible Truchet Tiles from chapter two:
Becka also presented an implementation that started from a random pattern and gradually changed to an image.
We began with our usual round of introductions and then collectively decided to spend the meeting working through the chapter together, since it contained a lot of technical material, rather than attempt to mob program an implementation.
The chapter introduced us to a "toy problem" in which two different products could be made out of "small" and "large" pieces of lego. Each product required a different number of resources and sold for a different amount. The objective was to maximise the total sales price whilst keeping withing the resource limits specified in the chapter.
We walked through a geometric representation of the problem and were introduced to the simplex algorithm which is an approach for finding the optimal solution based on some linear constraints. The chapter initially presented a two dimensional version of this problem (chairs and tables) and then introduced a third dimension (sofabordes) which made the problem three dimensional.
We then worked through a purely algorithmic/algebraic implementation of the simplex algorithm that worked for any number of dimensions, although we struggled a bit with some of the mathematical rearrangements of equations.
The last part of the chapter was only interested in integer solutions and we learned about how the branch and bound algorithm could be used to find these. Finally, we learned about the gurobi optimizer which is able to solve linear and integer programming problems and had a quick play to plug in the constraints from the chapter:
# Solve the following MIP:
# maximize
# 8 * X1 + 11 * X2 + 15 * X3
# subject to
# 2 * X1 + 2 * X2 + 2 * X3 <= 25
# 1 * X1 + 2 * X2 + 3 * X3 <= 19
# X1, X2, X3 >= 0
import gurobipy as gp
# Create a new model
m = gp.Model()
# Create variables
x1 = m.addVar(vtype=gp.GRB.INTEGER, name="X1")
x2 = m.addVar(vtype=gp.GRB.INTEGER, name="X2")
x3 = m.addVar(vtype=gp.GRB.INTEGER, name="X3")
# Set objective function
m.setObjective(8 * x1 + 11 * x2 + 15 * x3, gp.GRB.MAXIMIZE)
# Add constraints
m.addConstr(2 * x1 + 2 * x2 + 2 * x3 <= 25)
m.addConstr(x1 + 2 * x2 + 3 * x3 <= 19)
m.addConstr(x1 >= 0)
m.addConstr(x2 >= 0)
m.addConstr(x3 >= 0)
# Solve it!
m.optimize()
print(f"Optimal objective value: {m.objVal}")
print(f"Solution values: x={x1.X}, y={x2.X}, z={x3.X}")
After the meeting, Dmitry shared an interesting technique to change the direction of Truchet tiles based on a mask image:
Thanks to everyone who came along and contributed drinks, snacks and to Chris Lowis for hosting the meeting at his office.
- Home
- Documentation
- Choosing a Topic
- Shows & Tells
- Miscellaneous
- Opt Art
- Reinforcement Learning: An Introduction
- 10 Technical Papers Every Programmer Should Read (At Least Twice)
- 7 More Languages in 7 Weeks
- Lua, Day 1: The Call to Adventure
- Lua, Day 2: Tables All the Way Down
- Lua, Day 3
- Factor, Day 1: Stack On, Stack Off
- Factor, Day 2: Painting the Fence
- Factor, Day 3: Balancing on a Boat
- Elm, Day 1: Handling the Basics
- Elm, Day 2: The Elm Architecture
- Elm, Day 3: The Elm Architecture
- Elixir, Day 1: Laying a Great Foundation
- Elixir, Day 2: Controlling Mutations
- Elixir, Day 3: Spawning and Respawning
- Julia, Day 1: Resistance Is Futile
- Julia, Day 2: Getting Assimilated
- Julia, Day 3: Become One With Julia
- Minikanren, Days 1-3
- Minikanren, Einstein's Puzzle
- Idris Days 1-2
- Types and Programming Languages
- Chapter 1: Introduction
- Chapter 2: Mathematical Preliminaries
- Chapter 3: Untyped Arithmetic Expressions
- Chapter 4: An ML Implementation of Arithmetic Expressions
- Chapter 5: The Untyped Lambda-Calculus
- Chapters 6 & 7: De Bruijn Indices and an ML Implementation of the Lambda-Calculus
- Chapter 8: Typed Arithmetic Expressions
- Chapter 9: The Simply-Typed Lambda Calculus
- Chapter 10: An ML Implementation of Simple Types
- Chapter 11: Simple Extensions
- Chapter 11 Redux: Simple Extensions
- Chapter 13: References
- Chapter 14: Exceptions
- Chapter 15: Subtyping – Part 1
- Chapter 15: Subtyping – Part 2
- Chapter 16: The Metatheory of Subtyping
- Chapter 16: Implementation
- Chapter 18: Case Study: Imperative Objects
- Chapter 19: Case Study: Featherweight Java
- The New Turing Omnibus
- Errata
- Chapter 11: Search Trees
- Chapter 8: Random Numbers
- Chapter 35: Sequential Sorting
- Chapter 58: Predicate Calculus
- Chapter 27: Perceptrons
- Chapter 9: Mathematical Research
- Chapter 16: Genetic Algorithms
- Chapter 37: Public Key Cryptography
- Chapter 6: Game Trees
- Chapter 5: Gödel's Theorem
- Chapter 34: Satisfiability (also featuring: Sentient)
- Chapter 44: Cellular Automata
- Chapter 47: Storing Images
- Chapter 12: Error-Correcting Codes
- Chapter 32: The Fast Fourier Transform
- Chapter 36: Neural Networks That Learn
- Chapter 41: NP-Completeness
- Chapter 55: Iteration and Recursion
- Chapter 19: Computer Vision
- Chapter 61: Searching Strings
- Chapter 66: Church's Thesis
- Chapter 52: Text Compression
- Chapter 22: Minimum spanning tree
- Chapter 64: Logic Programming
- Chapter 60: Computer Viruses
- Show & Tell
- Elements of Computing Systems
- Archived pages