Skip to content

Instantly share code, notes, and snippets.

@jonahgeorge
Created March 5, 2015 03:11
Show Gist options
  • Save jonahgeorge/15c075762334988fb6f3 to your computer and use it in GitHub Desktop.
Save jonahgeorge/15c075762334988fb6f3 to your computer and use it in GitHub Desktop.
Project 4 PULP
import csv
import math
from pulp import *
def linear_trend(x0, x1, d):
return x0 - (x1 * d)
def seasonal_pattern(x2, x3, d):
return x2 * math.cos( (2 * math.pi * d) / 365.25) - x3 * math.sin( (2 * math.pi * d) / 565.25 )
def solar_cycle(x4, x5, d):
return x4 * math.cos( (2 * math.pi * d) / (365.25 * 10.7) ) - x5 * math.sin( (2 * math.pi * d) / (365.25 * 10.7) )
def average_temperature(y, x0, x1, x2, x3, x4, x5, d):
return y - linear_trend(x0, x1, d) - seasonal_pattern(x2, x3, d) - solar_cycle(x4, x5, d)
def main():
prob = LpProblem("Temperature", LpMinimize)
# prob += pulp.lpSum([x[n]]), "Minimize_the_maximum"
x0 = LpVariable("x0", 0)
x1 = LpVariable("x1", 0)
x2 = LpVariable("x2", 0)
x3 = LpVariable("x3", 0)
x4 = LpVariable("x4", 0)
x5 = LpVariable("x5", 0)
t = LpVariable("t", 0)
file = open('data.sample.csv')
reader = csv.reader(file, delimiter=';')
for row in reader:
prob += average_temperature(float(row[7]), x0, x1, x2, x3, x4, x5, int(row[2])) <= t
prob += average_temperature(float(row[7]), x0, x1, x2, x3, x4, x5, int(row[2])) >= (t*-1)
prob.writeLP("Temperature.lp")
prob.solve()
print "Status:", LpStatus[prob.status]
# print "Objective:", pulp.value(prob.objective)
for v in prob.variables():
print v.name, "=", v.varValue
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment