In [7]:
from hybridpy.learning import dynamicprogramming, ensemblepredictor
from hybridpy.dataset import triploader
from hybridpy.models import batteries, vehicles
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import glob
import math
sns.set_style('whitegrid')

In [8]:
%matplotlib inline

In [36]:
# Supercapacitor - battery setup for all-electric vehicle
# Cost is current squared on the battery, integrated over time
# Control is battery power,
cc_cf = lambda current, supercap_power, duration_s: (current**2)*duration_s 
bat_controls = [0, 5000, 10000, 20000, 30000, 75000]
supercap = batteries.IdealBattery(max_energy_wh=100)
vehicle = vehicles.ElectricCar(battery=batteries.QuadraticBattery())

In [37]:
dname = '/Users/astyler/projects/ChargeCarData/illah/'
trips = []
fnames = glob.glob(dname+'*.csv')
for fname in fnames:
    trips.append(triploader.load(fname))
    
len(trips)


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-37-2f1790ee6ccd> in <module>()
      3 fnames = glob.glob(dname+'*.csv')
      4 for fname in fnames:
----> 5     trips.append(triploader.load(fname))
      6 
      7 len(trips)

/Users/astyler/anaconda/lib/python2.7/site-packages/hybridpy/dataset/triploader.pyc in load(fname)
     20 
     21     # smooth noisy elevation measurements
---> 22     b, a = butter(4, 0.05)
     23     trip['ElevationFilt'] = filtfilt(b, a, trip.Elevation)
     24 

/Users/astyler/anaconda/lib/python2.7/site-packages/scipy/signal/filter_design.pyc in butter(N, Wn, btype, analog, output)
   1206     """
   1207     return iirfilter(N, Wn, btype=btype, analog=analog,
-> 1208                      output=output, ftype='butter')
   1209 
   1210 

KeyboardInterrupt: 

In [ ]:
v, q, p, d = dynamicprogramming.compute(trip=trips[0], controls=bat_controls, battery=supercap, vehicle=vehicle, cost_function=cc_cf, soc_states=50)

In [14]:


In [24]:
for i, trip in enumerate(trips[0:2]):
    v, q, p,d  = dynamicprogramming.compute(trip=trip, controls=bat_controls, battery=supercap, vehicle=vehicle, cost_function=cc_cf, soc_states=50)
    with open('pickles/v%03d.pickle' % i, 'wb') as f:
        pickle.dump(v, f)


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-24-86bf0d756077> in <module>()
      1 for i, trip in enumerate(trips[0:2]):
----> 2     v, q, p,d  = dynamicprogramming.compute(trip=trip, controls=bat_controls, battery=supercap, vehicle=vehicle, cost_function=cc_cf, soc_states=50)
      3     with open('pickles/v%03d.pickle', 'wb') as f:
      4         pickle.dump(v, f)

/Users/astyler/anaconda/lib/python2.7/site-packages/hybridpy/learning/dynamicprogramming.pyc in compute(trip, controls, soc_states, gamma, cost_function, vehicle, battery)
     56             # control is power supplied from the ICE, battery makes up the difference
     57             costs_to_go = [cost_to_go(soc + battery.compute_delta_soc_and_current(soc, power_demand - control, duration)[0]) for
---> 58                            control in controls]
     59             q_function[t][i] = [
     60                 cost_function(vehicle.compute_fuel_rate(control, soc), power_demand - control, duration) + (gamma * ctg) for

/Users/astyler/anaconda/lib/python2.7/site-packages/hybridpy/learning/dynamicprogramming.pyc in cost_to_go(soc)
     51                 return value_function[t + 1][-1] # can't charge above max, return value at max
     52             else:
---> 53                 return next_value_slice(soc) # return cost to go of next slice
     54 
     55         for (i, soc) in enumerate(socs):

/Users/astyler/anaconda/lib/python2.7/site-packages/scipy/interpolate/polyint.pyc in __call__(self, x)
     77         """
     78         x, x_shape = self._prepare_x(x)
---> 79         y = self._evaluate(x)
     80         return self._finish_y(y, x_shape)
     81 

/Users/astyler/anaconda/lib/python2.7/site-packages/scipy/interpolate/interpolate.pyc in _evaluate(self, x_new)
    497         x_new = asarray(x_new)
    498         out_of_bounds = self._check_bounds(x_new)
--> 499         y_new = self._call(self, x_new)
    500         if len(y_new) > 0:
    501             y_new[out_of_bounds] = self.fill_value

/Users/astyler/anaconda/lib/python2.7/site-packages/scipy/interpolate/interpolate.pyc in _call_linear(self, x_new)
    460         x_hi = self.x[hi]
    461         y_lo = self._y[lo]
--> 462         y_hi = self._y[hi]
    463 
    464         # Note that the following two expressions rely on the specifics of the

KeyboardInterrupt: 

In [35]:
plt.plot(p)


Out[35]:
[<matplotlib.lines.Line2D at 0x1132bb5d0>]

In [32]:
with open('pickles/v001.pickle', 'r') as f:
    v = pickle.load(f)

In [33]:
plt.figure(figsize=(18,8))
sns.heatmap(v.T[::-1], linewidths=0)#.imshow(v.T[::-1])#cmap='RdBu_r')


Out[33]:
<matplotlib.axes._subplots.AxesSubplot at 0x10f87e210>

In [33]:
ice_controls = [0, 20000, 40000, 60000, 80000]
ice_cf = lambda control, battery_power, duration_s: control * duration
battery = batteries.QuadraticBattery()

In [11]:
x = [1,2,3,4,5]

In [12]:
y = [10,20,30,np.nan, 50]

In [13]:
from scipy.interpolate import interp1d
f = interp1d(x,y, assume_sorted=True)

In [30]:
t=np.random.rand(*v.shape)

In [34]:
sns.heatmap(np.random.rand(50,30),linewidths=0)


Out[34]:
<matplotlib.axes._subplots.AxesSubplot at 0x11f99efd0>

In [16]:
((40000.0/3600)/50.)*100


Out[16]:
22.22222222222222

In [ ]: