-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipleScenarios.py
167 lines (127 loc) · 5.51 KB
/
MultipleScenarios.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
# This script sets up the energy-management problem for a truck platoon
#
# Requirements:
# To run this script, you need at least the following programs installed on your machine:
# Python, IPOPT, CasADi
from casadi import *
from casadi.tools import *
import numpy as NP
from scipy import interpolate
from matplotlib.pyplot import plot, draw, show, ion
from classes import *
from EM_platoon_load_params import *
from Platoon import *
import random
###########################
# Scenario definition #
###########################
# Requiring solver regeneration
# Number of trucks
# Frequency content / Total height variation ?
# Not requiring solver regeneration
# Driving cycles
# Mass repartition
# Airdrag models
Ncycles = 1
NFourrierCoeff = 5
plotresults = False
#Generate random cycles
Cycles = []
for cycle in range(Ncycles):
Cycles.append([ [random.normalvariate(0,2) for i in range(NFourrierCoeff)],
[random.normalvariate(0,2) for i in range(NFourrierCoeff)] ])
#Masses: Heavy, Medium, Light
H = 4e4
M = 3e4
L = 2e4
Scenarios = [{'Ntruck' : 3, 'Masses' : [ H, H, H ] },
{'Ntruck' : 3, 'Masses' : [ M, M, M ] },
{'Ntruck' : 3, 'Masses' : [ L, L, L ] },
{'Ntruck' : 3, 'Masses' : [ H, M, M ] },
{'Ntruck' : 3, 'Masses' : [ M, H, M ] },
{'Ntruck' : 3, 'Masses' : [ M, M, H ] },
{'Ntruck' : 3, 'Masses' : [ H, M, L ] },
{'Ntruck' : 3, 'Masses' : [ L, M, H ] },
{'Ntruck' : 3, 'Masses' : [ H, L, M ] },
]
for Mass in [L,M,H]:
for Ntruck in range(4,11):
Scenarios.append(
{'Ntruck' : Ntruck, 'Masses' : [Mass for i in range(Ntruck)] }
)
Scenarios = [{'Ntruck' : 3, 'Masses' : [ H, H, H ] }]
ScenariosOut = []
counter = 0
for scenario in Scenarios:
print scenario
#Hugly hack...
if counter == 0:
print 'Regenerate solver'
MyPlatoon = SetPlatoon(Ntruck = scenario['Ntruck'], NFourrierCoeff = NFourrierCoeff)
else:
if not(scenario['Ntruck'] == Scenarios[counter-1]['Ntruck']):
print 'Regenerate solver'
MyPlatoon = SetPlatoon(Ntruck = scenario['Ntruck'], NFourrierCoeff = NFourrierCoeff)
counter += 1
########################################################
# Create cost function (this is a unique declaration) #
########################################################
Torque = np.array(MyPlatoon.Vtruck['Input',:,'TdE'])
c1 = MyPlatoon.Ptruck['Parameter','c1']
c2 = MyPlatoon.Ptruck['Parameter','c2']
fobj = 1e-6*MyPlatoon.Vtruck['Tf']*(sum(c1*Torque + c2))/float(MyPlatoon.nk)
MyPlatoon.ObjFunc = SXFunction('ObjFunc',[MyPlatoon.Vtruck,MyPlatoon.Ptruck],[fobj])
MyPlatoon.SetSolvers()
Parameters = MyPlatoon.parameters()
# SIMULATE
AllGain = []
AllProfiles = []
AllCosts = []
AllSlopeL1Norms = []
AllSlopeL2Norms = []
for cycle in range(Ncycles):
MyPlatoon.ProblemParameters['Sin'] = Cycles[cycle][0]
MyPlatoon.ProblemParameters['Cos'] = Cycles[cycle][1]
MyPlatoon.Optimize()
AllGain.append(float(100*(MyPlatoon.SolversOut['Cost'][0]-MyPlatoon.SolversOut['Cost'][1])/MyPlatoon.SolversOut['Cost'][0]))
AllCosts.append(MyPlatoon.SolversOut['Cost'])
for label in ['Sin','Cos','Distance']:
Parameters[label] = MyPlatoon.ProblemParameters[label]
HeightPlot = []
SlopePlot = []
for k in range(MyPlatoon.nk):
pos = Parameters['Distance']*k/float(MyPlatoon.nk)
[Height_d,Slope_d] = MyPlatoon.HeightFunc([Parameters,pos])
HeightPlot.append(float(Height_d))
SlopePlot.append(100*float(Slope_d))
AllProfiles.append({'Slope': SlopePlot, 'Height': HeightPlot})
AllSlopeL1Norms.append(np.sum(np.abs(AllProfiles[cycle]['Slope'])))
AllSlopeL2Norms.append(np.sum(np.array(AllProfiles[cycle]['Slope'])**2))
#assert(counter < 2)
ScenariosOut.append({'Gains':AllGain, 'Costs':AllCosts, 'Profiles':AllProfiles, 'ProfilesNorms':[AllSlopeL1Norms]})
print 'Resolution ', MyPlatoon.resolution, 's'
print 'Cost gain', AllGain ,'%'
print 'Slopes norm', AllSlopeL1Norms
####################################################################################
########################### PLOT PLOT PLOT ################################
####################################################################################
if plotresults:
MyPlatoon.HazPlot()
plt.figure(10)
plt.hold('on')
plt.plot(AllSlopeL1Norms,AllGain, linestyle = 'none', marker = '.', color = 'r')
plt.hold('on')
plt.plot(AllSlopeL2Norms,AllGain, linestyle = 'none', marker = '.', color = 'r')
for cycle in range(Ncycles):
plt.figure(7)
plt.subplot(1,2,1)
plt.hold('on')
plt.plot([1e-3*Parameters['Distance']*i/MyPlatoon.nk for i in range(MyPlatoon.nk)],AllProfiles[cycle]['Slope'])
plt.ylabel('Slope in %')
plt.xlabel('Dist. in km')
plt.subplot(1,2,2)
plt.hold('on')
plt.plot([1e-3*Parameters['Distance']*i/MyPlatoon.nk for i in range(MyPlatoon.nk)],AllProfiles[cycle]['Height'])
plt.ylabel('Height')
plt.xlabel('Dist. in km')
plt.show()