-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearprogramming.py
198 lines (130 loc) · 3.83 KB
/
linearprogramming.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from pulp import *
import math, csv, sys
from datetime import date
pi = math.pi
d0 = date(1975, 11, 30)
tmax = []
tmin = []
average = []
day = []
def days_passed(element):
start = count = 0
while True:
if count == 10:
break
if element[start] == ',':
count += 1
start += 1
end = start + 4
yr = int(element[start:end])
start += 4
end += 2
mn = int(element[start:end])
start += 2
end += 2
dy = int(element[start:end])
d1 = date(yr, mn, dy)
delta = d1 - d0
day.append(int(delta.days))
def avgs():
count = 0
for x in tmax:
tmp1 = float(x / 10)
tmp2 = float(tmin[count] / 10)
average.append((tmp1 + tmp2) / 2)
count += 1
def parse2(element):
start = count = 0
while True:
if count == 11:
break
if element[start] == ',':
count += 1
start += 1
end = start
while True:
if element[end] == ',':
break
end += 1
tmax.append(int(element[start:end]))
start = end + 1
tmin.append(int(element[start:len(element)]))
def parse(element):
start = end = 27
while True:
if element[end] == ';':
break
end += 1
tmax.append(int(element[start:end]))
start = end = end + 1
while True:
if element[end] == ';':
break
end += 1
tmin.append(int(element[start:end]))
end = end + 6
count = 0
#skip over date
while True:
if element[end] == ';':
count += 1
if count == 2:
break
end += 1
start = end = end + 1
while True:
if element[end] == ';':
break
end += 1
average.append(float(element[start:end]))
end = end + 1
day.append(int(element[end:len(element)]))
def solve():
prob = LpProblem("min abs dev", LpMinimize)
tvar = LpVariable("tvar")
dvar = LpVariable("dvar")
x0var = LpVariable("x0var")
x1var = LpVariable("x1var")
x2var = LpVariable("x2var")
x3var = LpVariable("x3var")
x4var = LpVariable("x4var")
x5var = LpVariable("x5var")
prob += tvar
count = 0
for d in day:
prob += average[count] - x0var - x1var * d - x2var * math.cos((2 * pi * d) / 365.25) - x3var * math.sin((2 * pi * d) / 365.25) - x4var * math.cos((2 * pi * d) / (365.25 * 10.7)) - x5var * math.sin((2 * pi * d) / (365.25 * 10.7)) <= tvar
prob += average[count] - x0var - x1var * d - x2var * math.cos((2 * pi * d) / 365.25) - x3var * math.sin((2 * pi * d) / 365.25) - x4var * math.cos((2 * pi * d) / (365.25 * 10.7)) - x5var * math.sin((2 * pi * d) / (365.25 * 10.7)) >= -tvar
count += 1
status = prob.solve()
if prob.status == 1:
print("Optimal solution found.")
print("Objective: " + str(value(prob.objective)))
print("x0: " + str(value(x0var)))
print("x1: " + str(value(x1var)))
print("x2: " + str(value(x2var)))
print("x3: " + str(value(x3var)))
print("x4: " + str(value(x4var)))
print("x5: " + str(value(x5var)))
elif prob.status == 0:
print("Not solved.")
elif prob.status == -1:
print("Infeasible.")
elif prob.status == -2:
print("Unbounded.")
elif prob.status == -3:
print("Undefined.")
def main():
if sys.argv[1] == "1":
corvallis = csv.reader(open('Corvallis.csv'), delimiter=' ', quotechar='|')
next(corvallis)
for row in corvallis:
parse(', '.join(row))
elif sys.argv[1] == "2":
ny = csv.reader(open('NewYork.csv'), delimiter=' ', quotechar='|')
next(ny)
for row in ny:
parse2(', '.join(row))
days_passed(', '.join(row))
avgs()
solve()
main()