23
Optimization Workshop DeltaHacks 2017 Christopher Anand, Erdem Coskun, Curtis d’Alves

Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

Optimization Workshop DeltaHacks 2017

Christopher Anand, Erdem Coskun, Curtis d’Alves

Page 2: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

It is really simple!

• You learned how to minimize functions in Calculus.

• Find a function which represents resource use.

• Minimize it!

• We will show you some tools to handle the last step.

Page 3: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

It is incredibly complex.

• If you look under the hood, there are some impressive algorithms:

• Danzig’s Simplex Algorithm (~1947)

• Karmarkar’s Interior Point Method (1984)

• Some of the best work in any science this century.

Page 4: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

14 Hours Left• But this is a hackathon

• put something together, and hope it works

• Chances are very good if it is

• linear (not powers, sqrt, cos, etc.)

• it is convex without constraints (bowl holding water)

Page 5: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

Inspiring Example

• Not a 24h hack, but could have been.

• Beware, for every example like this, there are many failed examples.

• Goal: Automate spectral analysis to determine carbon backbone “INADEQUATE”. (MSc of Sean Watson).

Page 6: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

SUCROSE FFT to the rescue

Page 7: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

SUCROSE bonds = new dimension

Page 8: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

SUCROSE bonds = new dimension

Page 9: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

SUCROSE 6 hours of data !

Page 10: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

Likelihood = probability of bond

Page 11: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

VICTORY from a pinch of probability

Page 12: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

Likelihood = probability of bond

Page 13: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

Types Of Optimization• Unconstrained

• minimize a function

• Constrained

• + inequalities

• Integer Programming

• some numbers must be integers (includes 0/1)

Page 14: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

Three steps• Identify variables, objective and constraints

• Code in AMPL, GAMS, MATLAB, Excel, C/C++ arrays, …

• Call solver

• open source (IPOPT, Sedumi,…)

• free on NEOS (huge list)

• package in MATLAB or Excel

Page 15: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

AMPL / NEOS example

• trajectory design

• variables = positions after 0, dt, 2dt, … var x{j in -1..3*segments+1}; var y{j in -1..3*segments+1};

Page 16: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

• velocity = difference in positions

# Slow down at Huckleberry bush.

subject to slowH : (x[segments+1]-x[segments])^2 + (y[segments+1]-y[segments+1])^2 <= (Slow*dt)^2;

Page 17: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

• acceleration = difference in velocities

# Don't brake too much

subject to B1 {i in 0..segments}: - Brake * dt1^3 <= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]);

Page 18: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

• make readable code, use

• param for constants

• # for comments

param Brake = 1; # work limit on brakes (vel . accel >= - Brake) param Horsepower = 0.1; # work limit on acceleration (vel . acel <= Horsepower) param Tipping = 0.1; # limit on tipping (-Tipping <= vel . perp(accel) <= Tipping)

Page 19: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how

# # Copyright (c) 2009 by Christopher Kumar Anand. # Licenced under BSD3. # # Parameters

param Slow = 5; # maximum speed for picking

param xHuckleberry = 17; # position of Huckleberry bush param yHuckleberry = 5;

param xApple = 12; # position of Apple tree param yApple = 7;

param Brake = 1; # work limit on brakes (vel . accel >= - Brake) param Horsepower = 0.1; # work limit on acceleration (vel . acel <= Horsepower) param Tipping = 0.1; # limit on tipping (-Tipping <= vel . perp(accel) <= Tipping)

param segments = 20;

## start and stop at (0,0) and pass by Huckleberry bush and Apple tree ## with speed at most 1, so you can lean out and pick some for the pie.

# Position variables.

var x{j in -1..3*segments+1}; var y{j in -1..3*segments+1};

# time increment on way to Huckleberry Bush var dt1; # time increment on way to Apple Tree var dt2; # time increment on way home var dt3;

# Objective function to be minimized.

minimize obj:

dt1 + dt2 + dt3;

# Start at a stop.

subject to start1 : x[-1] = 0; subject to start2 : x[0] = 0; subject to start3 : y[-1] = 0; subject to start4 : y[0] = 0;

# Stop at a stop.

subject to stop1 : x[3*segments] = 0; subject to stop2 : x[3*segments+1] = 0; subject to stop3 : y[3*segments] = 0; subject to stop4 : y[3*segments+1] = 0;

# Slow down at Huckleberry bush.

subject to slowH : (x[segments+1]-x[segments])^2 + (y[segments+1]-y[segments+1])^2 <= Slow^2 * dt1*dt2; subject to atHx : (x[segments+1] + x[segments])/2 = xHuckleberry; subject to atHy : (y[segments+1] + y[segments])/2 = yHuckleberry;

# Slow down at Apple Tree.

subject to slowA : (x[2*segments+1]-x[2*segments])^2 + (y[2*segments+1]-y[2*segments+1])^2 <= Slow^2 * dt2*dt3; subject to atAx : (x[2*segments+1] + x[2*segments])/2 = xApple; subject to atAy : (y[2*segments+1] + y[2*segments])/2 = yApple;

# Don't brake too much

subject to B1 {i in 0..segments}: - Brake * dt1^3 <= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to B2 {i in segments..2*segments}: - Brake * dt2^3 <= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to B3 {i in 2*segments..3*segments}: - Brake * dt3^3 <= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]);

# Don't accelerate too much

subject to A1 {i in 0..segments}: Horsepower * dt1^3 >= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to A2 {i in segments..2*segments}: Horsepower * dt2^3 >= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to A3 {i in 2*segments..3*segments}: Horsepower * dt3^3 >= (x[i+1]-x[i]) * (x[i+1]-2*x[i]+x[i-1]) + (y[i+1]-y[i]) * (y[i+1]-2*y[i]+y[i-1]);

# Don't turn too much

subject to T1 {i in 0..segments}: - Tipping * dt1^3 <= - (y[i+1]-y[i]) * (x[i+1]-2*x[i]+x[i-1]) + (x[i+1]-x[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to T2 {i in segments..2*segments}: - Tipping * dt2^3 <= - (y[i+1]-y[i]) * (x[i+1]-2*x[i]+x[i-1]) + (x[i+1]-x[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to T3 {i in 2*segments..3*segments}: - Tipping * dt3^3 <= - (y[i+1]-y[i]) * (x[i+1]-2*x[i]+x[i-1]) + (x[i+1]-x[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to T4 {i in 0..segments}: Tipping * dt1^3 >= - (y[i+1]-y[i]) * (x[i+1]-2*x[i]+x[i-1]) + (x[i+1]-x[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to T5 {i in segments..2*segments}: Tipping * dt2^3 >= - (y[i+1]-y[i]) * (x[i+1]-2*x[i]+x[i-1]) + (x[i+1]-x[i]) * (y[i+1]-2*y[i]+y[i-1]); subject to T6 {i in 2*segments..3*segments}: Tipping * dt3^3 >= - (y[i+1]-y[i]) * (x[i+1]-2*x[i]+x[i-1]) + (x[i+1]-x[i]) * (y[i+1]-2*y[i]+y[i-1]);

option solver knitroampl; solve;

for {i in 0..3*segments} { printf "%f %f\n", x[i], y[i] >> path; } display dt1,dt2,dt3; #display A1,A2,A3; display (x[2*segments+1]+x[2*segments])/2,(y[2*segments+1]+y[2*segments])/2;

http://www.cas.mcmaster.ca/~anand/lab1/hucklberryPie.mod

Page 20: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how
Page 21: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how
Page 22: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how
Page 23: Optimization Workshop DeltaHacks 2017sqrl.mcmaster.ca/~anand/lab1/DeltaHacksOpt17.pdf · Christopher Anand, Erdem Coskun, Curtis d’Alves. It is really simple! • You learned how