-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPCsolve.py
162 lines (153 loc) · 6.1 KB
/
MPCsolve.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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 28 12:19:37 2015
@author: Satyajeet
"""
import numpy as NP
from pomodoro.problem.problem import Problem
from pomodoro.solver.solver2 import Solver2
from pomodoro.discs.expression import Expression
from casadi import *
import matplotlib.pyplot as plt
from SolACE.EstimationRoutine import EstimationRoutine
class MPCsolve(object):
def __init__(self,MPC,Estimator=None,printlevel=0,max_iter=4000,save=None):
self.MPC = MPC
self.Estimator = Estimator
self.printlevel = printlevel
self.max_iter = max_iter
if self.MPC.nmx is None:
self.nmx = list(xrange(self.MPC.nx))
self.std = self.MPC.nx*[0]
else:
self.nmx = self.MPC.nmx
self.std = self.MPC.std
self.x_final = NP.zeros((self.MPC.nx,MPC.N_sampling+1))
self.x_measured = NP.zeros((len(self.nmx),MPC.N_sampling+1))
self.u_final = NP.zeros((MPC.nu,MPC.N_sampling))
self.save = save
if save is not None and isinstance(save,str) is False:
raise IOError('File Name to save should be a string')
def solveOCP(self,prob,printlevel=0,max_iter=4000):
solver = Solver2(prob,printlevel=self.printlevel,max_iter=self.max_iter)
solver.solve()
def InitIntegrate(self,f,tfinal,Nintegrate = 1):
self.Nintegrate = Nintegrate
deltat_int = tfinal/Nintegrate
I = Integrator('cvodes',f)
I.setOption('tf',deltat_int)
I.setOption('abstol',1e-4)
I.setOption('reltol',1e-4)
I.init()
return I
def solve(self):
I = self.InitIntegrate(self.MPC.plant_model,self.MPC.t_plant)
if self.Estimator is not None:
est_P = NP.reshape(self.Estimator.P00,self.MPC.nx*self.MPC.nx)
else:
est_P = 0.0
self.x_final[:,0] = self.MPC.x0
x_plant = self.MPC.x0
self.est = EstimationRoutine(self)
for self.k in range(self.MPC.N_sampling):
i = self.k
print i
print self.MPC.N_sampling
self.MPC.x_con.init(x_plant)
self.MPC.x_init.setPVals(self.x_final[:,i])
#print self.MPC.x_init.getPVals() #TRY
self.solveOCP(self.MPC.prob)
self.u_final[:,i] = self.MPC.u_con(0,True).flatten()
I.setInput(self.u_final[:,i],'p')
#print self.u_final[:,i]
xint0 = x_plant
for j in range(self.Nintegrate):
I.setInput(xint0,'x0')
I.evaluate()
xint0 = I.getOutput('xf')
x_plant = NP.array(xint0).flatten()
x_meas = self.est.getMeasurements(x_plant)
self.x_measured[:,i+1] = x_meas
self.x_final[:,i+1],est_P = self.est.estimate(x_meas,est_P,self.x_final[:,i],self.u_final[:,i],self.MPC.t_plant)
#raw_input('TTT')
if self.save is not None:
NP.savetxt(self.save+'_states',self.x_final)
NP.savetxt(self.save+'_controls',self.u_final)
def plotStates(self,n=None,save=None):
t = NP.linspace(0.0,self.MPC.total_plant,self.MPC.N_sampling+1)
if n is None:
np = NP.ceil(NP.sqrt(self.MPC.nx))
for i in range(self.MPC.nx):
plt.subplot(np,np,i+1)
plt.plot(t,self.x_final[i,:])
plt.xlabel('t')
plt.ylabel('x'+str(i))
if save is not None:
plt.savefig(save[0]+'_states',format=save[1])
plt.show()
elif isinstance(n,list) is True:
if len(n)>self.MPC.nx:
raise IOError('List of states too long')
np = NP.ceil(NP.sqrt(NP.abs(len(n))))
j = 1
for i in n:
plt.subplot(np,np,j)
plt.plot(t,self.x_final[i,:])
plt.xlabel('t')
plt.ylabel('x'+str(i))
j+=1
if save is not None:
plt.savefig(save[0]+'_states',format=save[1])
plt.show()
elif isinstance(n,int) is True:
plt.plot(t,self.x_final[n,:])
plt.xlabel('t')
plt.ylabel('x'+str(n))
if save is not None:
plt.savefig(save[0]+'_states',format=save[1])
plt.show()
else:
raise IOError('\'n\' should be a list or integer')
def plotControls(self,n=None,save=None):
t = NP.linspace(0.0,self.MPC.total_plant-self.MPC.t_plant,self.MPC.N_sampling)
if n is None:
np = NP.ceil(NP.sqrt(NP.abs(self.MPC.nu)))
for i in range(self.MPC.nu):
plt.subplot(np,np,i+1)
plt.step(t,self.u_final[i,:])
plt.xlabel('t')
plt.ylabel('u'+str(i))
if save is not None:
plt.savefig(save[0]+'controls',format=save[1])
plt.show()
elif isinstance(n,list) is True:
if len(n)>self.MPC.nu:
raise IOError('List of controls too long')
np = NP.ceil(NP.sqrt(NP.abs(len(n))))
j = 1
for i in n:
plt.subplot(np,np,j)
plt.plot(t,self.u_final[i,:])
plt.xlabel('t')
plt.ylabel('u'+str(i))
j+=1
if save is not None:
plt.savefig(save[0]+'_controls',format=save[1])
plt.show()
elif isinstance(n,int) is True:
plt.plot(t,self.u_final[n,:])
plt.xlabel('t')
plt.ylabel('u'+str(n))
if save is not None:
plt.savefig(save[0]+'_controls',format=save[1])
plt.show()
else:
raise IOError('\'n\' should be a list or integer')
def plotMeasurements(self):
t = NP.linspace(0.0,self.MPC.total_plant,self.MPC.N_sampling+1)
i = 0
for j in self.nmx:
plt.plot(t,self.x_measured[i,:])
plt.plot(t,self.x_final[j,:])
i+=1
plt.show()