Input File Creation

First let's start with some tools to create input files for a given deployment schedule.


In [1]:
import os
import sys
import uuid
import json
import time
import subprocess
from math import ceil
from copy import deepcopy

import numpy as np
import pandas as pd
import cymetric as cym
%matplotlib inline
import matplotlib.pyplot as plt
import george

import dtw


/home/scopatz/miniconda/lib/python3.5/importlib/_bootstrap.py:222: QAWarning: pyne.data is not yet QA compliant.
  return f(*args, **kwds)
/home/scopatz/miniconda/lib/python3.5/importlib/_bootstrap.py:222: QAWarning: pyne.material is not yet QA compliant.
  return f(*args, **kwds)
/home/scopatz/miniconda/lib/python3.5/importlib/_bootstrap.py:222: QAWarning: pyne.enrichment is not yet QA compliant.
  return f(*args, **kwds)

In [2]:
with open('once-through.json') as f:
    BASE_SIM = json.load(f)
DURATION = BASE_SIM['simulation']['control']['duration']
YEARS = ceil(DURATION / 12)
MONTH_SHUFFLE = (1, 7, 10, 4, 8, 6, 12, 2, 5, 9, 11, 3)
NULL_SCHEDULE = {'build_times': [{'val': 1}], 
                 'n_build': [{'val': 0}], 
                 'prototypes': [{'val': 'LWR'}]}
LWR_PROTOTYPE = {'val': 'LWR'}
OPT_H5 = 'opt.h5'

In [3]:
BASE_SIM['simulation']['region']['institution']['config']['DeployInst']


Out[3]:
{'build_times': [{'val': 1}],
 'n_build': [{'val': 0}],
 'prototypes': [{'val': 'LWR'}]}

In [4]:
def deploy_inst_schedule(Θ):
    if np.sum(Θ) == 0: 
        return NULL_SCHEDULE
    sched = {'build_times': {'val': []},
             'n_build': {'val': []},
             'prototypes': {'val': []}}
    build_times = sched['build_times']['val']
    n_build = sched['n_build']['val']
    prototypes = sched['prototypes']['val']
    m = 0
    for i, θ in enumerate(Θ):
        if θ <= 0:
            continue
        build_times.append(i*12 + MONTH_SHUFFLE[m])
        n_build.append(int(θ))
        prototypes.append('LWR')
        m = (m + 1) % 12
    return sched

def make_sim(Θ, fname='sim.json'):
    sim = deepcopy(BASE_SIM)
    inst = sim['simulation']['region']['institution']
    inst['config']['DeployInst'] = deploy_inst_schedule(Θ)
    with open(fname, 'w') as f:
        json.dump(sim, f)
    return sim

In [5]:
s = make_sim([])

In [6]:
s['simulation']['region']['institution']['config']['DeployInst']


Out[6]:
{'build_times': [{'val': 1}],
 'n_build': [{'val': 0}],
 'prototypes': [{'val': 'LWR'}]}

Simulate

Now let's build some tools to run simulations and extract a GWe time series.


In [7]:
def run(fname='sim.json', out=OPT_H5):
    """Runs a simulation and returns the sim id."""
    cmd = ['cyclus', '--warn-limit', '0', '-o', out, fname]
    proc = subprocess.run(cmd, check=True, universal_newlines=True, stdout=subprocess.PIPE)
    simid = proc.stdout.rsplit(None, 1)[-1]
    return simid

ZERO_GWE = pd.DataFrame({'GWe': np.zeros(YEARS)}, index=np.arange(YEARS))
ZERO_GWE.index.name = 'Time'

def extract_gwe(simid, out=OPT_H5):
    """Computes the annual GWe for a simulation."""
    with cym.dbopen(out) as db:
        evaler = cym.Evaluator(db)
        raw = evaler.eval('TimeSeriesPower', conds=[('SimId', '==', uuid.UUID(simid))])
    ano = pd.DataFrame({'Time': raw.Time.apply(lambda x: x//12), 
                        'GWe': raw.Value.apply(lambda x: 1e-3*x/12)})
    gwe = ano.groupby('Time').sum()
    gwe = (gwe + ZERO_GWE).fillna(0.0)
    return np.array(gwe.GWe)

Distancing

Now let's build some tools to distance between a GWe time series and a demand curve.


In [8]:
DEFAULT_DEMAND = 90 * (1.01**np.arange(YEARS))  # 1% growth

In [9]:
def d(g, f=None):
    """The dynamic time warping distance between a GWe time series and a demand curve."""
    f = DEFAULT_DEMAND if f is None else f
    rtn = dtw.distance(f[:, np.newaxis], g[:, np.newaxis])
    return rtn

def αd(g, f=None, dtol=1e-5):
    """Computes the mininmal α and the DTW d."""
    f = DEFAULT_DEMAND if f is None else f
    C = dtw.cost_matrix(f[:, np.newaxis], g[:, np.newaxis])
    d = dtw.distance(cost=C)
    #d_t = np.diagonal(C) / (np.arange(1, C.shape[0] + 1) + np.arange(1, C.shape[1] + 1))
    d_t = np.diagonal(C) / np.sum(C.shape)
    #α = np.argwhere(np.cumsum(d_t <= dtol) != np.arange(1, len(d_t) + 1))[0,0]
    filt = np.argwhere(d_t <= dtol)
    α = filt[-1,-1] if len(filt) > 0 else 0
    #α = filt[0,0] if len(filt) > 0 else 0
    print("Simulation α", α)
    print("Simulation d(t)", d_t)
    return α, d

def gwed(Θ, f=None, dtol=1e-5, find_α=False):
    """For a given deployment schedule Θ, return the GWe time series and the distance 
    to the demand function f.
    """
    make_sim(Θ)
    simid = run()
    gwe = extract_gwe(simid)
    if find_α:
        rtn = αd(gwe, f=f, dtol=dtol)
    else:
        rtn = d(gwe, f=f)
    return (gwe,) + rtn

Initialize Optimization

Now let's start with a couple of simple simulations


In [10]:
N = np.asarray(np.ceil(4*(1.01)**np.arange(YEARS)), dtype=int)  # max annual deployments
Θs = [] # deployment schedules
G = []  # GWe per sim
D = []  # distances per sim
if os.path.isfile(OPT_H5):
    os.remove(OPT_H5)

In [11]:
def add_sim(Θ, f=None, dtol=1e-5):
    """Add a simulation to the known simulations by performing the simulation."""
    g_s, α, d_s = gwed(Θ, f=f, dtol=dtol, find_α=True)
    Θs.append(Θ)
    G.append(g_s)
    D.append(d_s)
    return α

First, add a schedule where nothing is deployed, leaving the initial facilities to retire.


In [12]:
add_sim(np.zeros(YEARS, dtype=int))


Simulation α 0
Simulation d(t) [  0.04833333   0.05016667   0.08492333   0.1488609    0.24540451
   0.37881355   0.54168169   0.73743684   0.97034121   1.23132462
   1.53131787   1.86041938   2.22372857   2.62467919   3.05337265
   3.51907804   4.01939882   4.55193948   5.11930554   5.72243693
   6.35810796   7.03142738   7.73583832   8.4764517    9.25337955
  10.06506835  10.91079903  11.79068702  12.70401556  13.65340238
  14.6389664   15.65916107  16.71660768  17.80892876  18.93708138
  20.10285719  21.30388243  22.54195125  23.8163591   25.12640269
  26.46638005  27.81975718  29.18666809  30.5672481   31.96163392
  33.36996359  34.79237656  36.22901366  37.68001713  39.14553063]
Out[12]:
0

Next, add a simulation that is the max deployment schedule to bound the space


In [13]:
add_sim(N)


Simulation α 0
Simulation d(t) [  1.16666667e-02   7.10000000e-02   1.34576667e-01   2.36472433e-01
   3.23885215e-01   4.41944067e-01   5.36262039e-01   6.83066193e-01
   8.06287715e-01   9.15163374e-01   1.02139344e+00   1.16770073e+00
   1.32994840e+00   1.48369971e+00   1.62257310e+00   1.78794182e+00
   1.95230050e+00   2.13143123e+00   2.29098570e+00   2.49686978e+00
   2.70072297e+00   2.89689973e+00   3.08079894e+00   3.30466910e+00
   3.54732316e+00   3.78033684e+00   4.00910261e+00   4.28936438e+00
   4.56826787e+00   4.87839020e+00   5.16057347e+00   5.49516595e+00
   5.83787507e+00   6.15723134e+00   6.48226712e+00   6.87402745e+00
   7.29335939e+00   7.69237527e+00   8.08127345e+00   8.50360275e+00
   8.93014870e+00   9.40783104e+00   9.89381006e+00   1.04962490e+01
   1.11213573e+01   1.17619059e+01   1.24124975e+01   1.31568664e+01
   1.39810109e+01   1.48024031e+01]
Out[13]:
0

Optimizer

Now let's add some tools to do the estimation phase of the optimization.


In [14]:
Γ = 285
np.random.seed(424242)

In [15]:
def gp_gwe(Θs, G, α, T=None, tol=1e-6, verbose=False):
    """Create a Gaussian process regression model for GWe."""
    S = len(G)
    T = YEARS if T is None else T
    t = np.arange(T)
    P = len(Θs[0])
    ndim = P + 1 - α
    y_mean = np.mean(G)
    y = np.concatenate(G)
    x = np.empty((S*T, ndim), dtype=int)
    for i in range(S):
        x[i*T:(i+1)*T, 0] = t
        x[i*T:(i+1)*T, 1:] = Θs[i][np.newaxis, α:]
    yerr = tol * y_mean
    #kernel = float(y_mean) * george.kernels.ExpSquaredKernel(1.0, ndim=ndim)
    #for p in range(P):
    #    kernel *= george.kernels.ExpSquaredKernel(1.0, ndim=ndim)
    #kernel = float(y_mean) * george.kernels.Matern52Kernel(1.0, ndim=ndim)
    kernel = float(y_mean) * george.kernels.Matern32Kernel(1.0, ndim=ndim)
    gp = george.GP(kernel, mean=y_mean)
    gp.compute(x, yerr=yerr, sort=False)
    gp.optimize(x, y, yerr=yerr, sort=False, verbose=verbose)
    return gp, x, y

def predict_gwe(Θ, gp, y, α, T=None):
    """Predict GWe for a deployment schedule Θ and a GP."""
    T = YEARS if T is None else T
    t = np.arange(T)
    P = len(Θ)
    ndim = P + 1 - α
    x = np.empty((T, ndim), dtype=int)
    x[:,0] = t
    x[:,1:] = Θ[np.newaxis,α:]
    mu = gp.predict(y, x, mean_only=True)
    return mu

def gp_d_inv(θ_p, D_inv, tol=1e-6, verbose=False):
    """Computes a Gaussian process model for a deployment parameter."""
    S = len(D)
    ndim = 1
    x = θ_p
    y = D_inv
    y_mean = np.mean(y)
    yerr = tol * y_mean
    kernel = float(y_mean) * george.kernels.ExpSquaredKernel(1.0, ndim=ndim)
    gp = george.GP(kernel, mean=y_mean, solver=george.HODLRSolver)
    gp.compute(x, yerr=yerr, sort=False)
    gp.optimize(x, y, yerr=yerr, sort=False, verbose=verbose)
    return gp, x, y

def weights(Θs, D, N, Nlower, α, tol=1e-6, verbose=False):
    P = len(N)
    θ_ps = np.array(Θs)
    D = np.asarray(D)
    D_inv = D**-1 
    W = [None] * α
    for p in range(α, P):
        θ_p = θ_ps[:,p]
        range_p = np.arange(Nlower[p], N[p] + 1)
        gp, _, _ = gp_d_inv(θ_p, D_inv, tol=tol, verbose=verbose)
        d_inv_np = gp.predict(D_inv, range_p, mean_only=True)
        #p_min = np.argmin(D)
        #lam = θ_p[p_min]
        #fact = np.cumprod([1.0] + list(range(1, N[p] + 1)))[Nlower[p]:N[p] + 1]
        #d_inv_np = np.exp(-lam) * (lam**range_p) / fact
        if np.all(np.isnan(d_inv_np)) or np.all(d_inv_np <= 0.0):
            # try D, instead of D^-1
            #gp, _, _ = gp_d_inv(θ_p, D, tol=tol, verbose=verbose)
            #d_np = gp.predict(D, np.arange(0, N[p] + 1), mean_only=True)
            # try setting the shortest d to 1, all others 0.
            #d_inp_np = np.zeros(N[p] + 1, dtype='f8')
            #p_min = np.argmin(D)
            #d_inv_np[np.argwhere(θ_p[p_min] == range_p)] = 1.0
            # try Poisson dist centered at min.
            p_min = np.argmin(D)
            lam = θ_p[p_min]
            fact = np.cumprod([1.0] + list(range(1, N[p] + 1)))[Nlower[p]:N[p] + 1]
            d_inv_np = np.exp(-lam) * (lam**range_p) / fact
        if np.any(d_inv_np < 0.0):
            d_inv_np[d_inv_np < 0.0] = np.min(d_inv_np[d_inv_np > 0.0])
        d_inv_np_tot = d_inv_np.sum()
        w_p = d_inv_np / d_inv_np_tot
        W.append(w_p)
    return W

def guess_scheds(Θs, W, Γ, gp, y, α, T=None):
    """Guess a new deployment schedule, given a number of samples Γ, weights W, and 
    Guassian process for the GWe.
    """
    P = len(W)
    Θ_γs = np.empty((Γ, P), dtype=int)
    Θ_γs[:, :α] = Θs[0][:α]
    for p in range(α, P):
        w_p = W[p]
        Θ_γs[:, p] = np.random.choice(len(w_p), size=Γ, p=w_p)
    Δ = []
    for γ in range(Γ):
        Θ_γ = Θ_γs[γ]
        g_star = predict_gwe(Θ_γ, gp, y, α, T=T)
        d_star = d(g_star)
        Δ.append(d_star)
    γ = np.argmin(Δ)
    Θ_γ = Θ_γs[γ]
    print('hyperparameters', gp.kernel[:])
    #print('Θ_γs', Θ_γs)
    #print('Θ_γs[γ]', Θ_γs[γ])
    #print('Predition', Δ[γ], Δ)
    return Θ_γ, Δ[γ]

def guess_scheds_loop(Θs, gp, y, N, Nlower):
    """Guess a new deployment schedule, given a number of samples Γ, weights W, and 
    Guassian process for the GWe.
    """
    P = len(N)
    Θ = np.array(Θs[0], dtype=int)
    for p in range(P):
        d_p = []
        range_p = np.arange(Nlower[p], N[p] + 1, dtype=int)
        for n_p in range_p:
            Θ[p] = n_p
            g_star = predict_gwe(Θ, gp, y, α=0, T=p+1)[:p+1]
            d_star = d(g_star, f=DEFAULT_DEMAND[:p+1])
            d_p.append(d_star)
        Θ[p] = range_p[np.argmin(d_p)]
    print('hyperparameters', gp.kernel[:])
    return Θ, np.min(d_p)

def estimate(Θs, G, D, N, Nlower, Γ, α, T=None, tol=1e-6, verbose=False, method='stochastic'):
    """Runs an estimation step, returning a new deployment schedule."""
    gp, x, y = gp_gwe(Θs, G, α, T=T, tol=tol, verbose=verbose)
    if method == 'stochastic':
        # orig
        W = weights(Θs, D, N, Nlower, α, tol=tol, verbose=verbose)
        Θ, dmin = guess_scheds(Θs, W, Γ, gp, y, α, T=T)
    elif method == 'inner-prod':
        # inner prod
        Θ, dmin = guess_scheds_loop(Θs, gp, y, N, Nlower)
    elif method == 'all':
        W = weights(Θs, D, N, Nlower, α, tol=tol, verbose=verbose)
        Θ_stoch, dmin_stoch = guess_scheds(Θs, W, Γ, gp, y, α, T=T)
        Θ_inner, dmin_inner = guess_scheds_loop(Θs, gp, y, N, Nlower)
        if dmin_stoch < dmin_inner:
            winner = 'stochastic'
            Θ = Θ_stoch
        else:
            winner = 'inner'
            Θ = Θ_inner
        print('Estimate winner is {}'.format(winner))
    else:
        raise ValueError('method {} not known'.format(method))
    return Θ

def optimize(MAX_D=0.1, MAX_S=12, T=None, tol=1e-6, dtol=1e-5, verbose=False):
    global Θs, G, D
    α = 0
    s = 2
    z = 2
    n = N
    nlower = n0 = np.zeros(len(N), dtype=int)
    dtol = np.linspace(dtol * 2.0 / len(N), dtol, len(N))
    method = 'stochastic'
    #method = 'all'
    while MAX_D < D[-1] and s < MAX_S and α + 1 < YEARS:
        print(s)
        print('-'*18)
        Gprev = np.array(G[:z])
        t0 = time.time()
        method = 'stochastic' if s%4 < 2 else 'all'
        Θ = estimate(Θs, G, D, n, nlower, Γ, α, T=T, tol=tol, verbose=verbose, method=method)
        t1 = time.time()
        α_s = add_sim(Θ, dtol=dtol)
        t2 = time.time()
        print('Estimate time:   {0} min {1} sec'.format((t1-t0)//60, (t1-t0)%60))
        print('Simulation time: {0} min {1} sec'.format((t2-t1)//60, (t2-t1)%60))
        print(D)
        sys.stdout.flush()
        idx = [int(i) for i in np.argsort(D)[:z]]
        if D[-1] == max(D):
            idx.append(-1)
        #elif len(G) == z + 2  and np.allclose(G[:z], Gprev):
        #    n = np.array([Θs[0] + 1, N], dtype=int).min(axis=0)
        #    nlower = np.array([Θs[0] - 1, n0], dtype=int).max(axis=0)
        #    print('New N-upper', n)
        #    print('New N-lower', nlower)
        #if (α < α_s) and ((len(D) - 1) in idx[:2]):
        #if (α < α_s) and (len(D) == idx[0] + 1):
        if (len(D) == idx[0] + 1):
            print('Update α: {0} -> {1}'.format(α, α_s))
            α = α_s
        #    method = 'stochastic'
        #elif method == 'stochastic' and len(D) == z + 2:
        #elif len(D) == z + 2:
        #    print('Trying inner product method')
        #    method = 'inner-prod'
        #else:
        #    print('Trying stochastic method')
        #    method = 'stochastic'
        #elif α > 0:
        #    print('Update α: {0} -> {1}'.format(α, α - 1))
        #    α -= 1
        Θs = [Θs[i] for i in idx]
        G = [G[i] for i in idx]
        D = [D[i] for i in idx]
        s += 1
        print()

Simulate

Ok! Let's try it.


In [16]:
%%time
optimize(MAX_S=25, dtol=1e-5)


2
------------------
hyperparameters [ 8.44155389  5.41756185]
hyperparameters [ 8.44155389  5.41756185]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.04833333  0.05016667  0.08492333  0.1488609   0.24540451  0.37881355
  0.54168169  0.73743684  0.97034121  1.23132462  1.53131787  1.86041938
  2.22372857  2.62467919  3.00753932  3.40657804  3.79856549  4.15610615
  4.52347221  4.85577026  5.20810796  5.51059404  5.80250498  6.10311837
  6.38587955  6.10855501  5.42453913  5.30496029  5.4124555   4.65216848
  4.55939917  4.24411294  4.36155955  3.99181954  3.94801078  3.94139337
  3.93777646  3.95849049  4.02824931  4.07816213  4.11669415  4.2323508
  4.3672495   4.57482883  4.77047313  5.07220136  5.42436825  5.77744383
  6.15926431  6.57002393]
Estimate time:   0.0 min 10.079778909683228 sec
Simulation time: 0.0 min 7.360328435897827 sec
[39.145530632616016, 14.802403115793279, 6.5700239322320666]
Update α: 0 -> 0

3
------------------
hyperparameters [ 7.8516871  4.6362031]
hyperparameters [ 7.8516871  4.6362031]
Estimate winner is stochastic
Simulation α 0
Simulation d(t) [  0.03916667   0.06266667   0.06957667   0.0801809    0.11839118
   0.18263355   0.27550169   0.39625684   0.55082787   0.73847795
   0.96013787   1.21423938   1.46588191   1.74516586   2.04052598
   2.34623138   2.67988549   2.97992615   3.30312554   3.6479236
   4.0127613    4.39358071   4.80299165   5.2127717    5.63303288
   6.04638835   6.47461903   6.86534035   7.27783556   7.69805571
   8.12195307   8.55798107   8.95959435   9.38024876   9.81173471
  10.21917719  10.64520243  11.04078459  11.45769243  10.25662225
   9.92175597   9.47901313   8.81157107   7.21067529   6.28688881
   5.93061151   5.52515577   4.93013096   4.77365259   4.69667341]
Estimate time:   0.0 min 9.952510356903076 sec
Simulation time: 0.0 min 7.455087184906006 sec
[6.5700239322320666, 14.802403115793279, 4.6966734129145085]
Update α: 0 -> 0

4
------------------
hyperparameters [ 6.95986966  4.25086576]
Simulation α 0
Simulation d(t) [ 0.03        0.05016667  0.05659     0.08052757  0.11707118  0.18464689
  0.27334836  0.38410351  0.53117454  0.70715795  0.92131787  1.14958605
  1.39956191  1.66051252  1.93670598  2.22907804  2.52606549  2.78943948
  3.06263887  3.36577026  3.69810796  4.03309404  4.39417165  4.75311837
  5.12004622  5.46673501  5.81663236  6.17152035  6.53318222  6.88590238
  7.24813307  7.6374944   8.03077435  8.38976209  8.77374804  9.15285719
  9.5555491   9.14627125  9.07092521  9.4784688   9.89011283  8.13669155
  7.01332986  6.74001109  6.40724934  5.14514033  4.73270259  4.21109901
  4.22596483  3.86226935]
Estimate time:   0.0 min 7.500619411468506 sec
Simulation time: 0.0 min 8.149144411087036 sec
[4.6966734129145085, 6.5700239322320666, 3.8622693476660661]
Update α: 0 -> 0

5
------------------
hyperparameters [ 5.79633636  3.74611183]
Simulation α 0
Simulation d(t) [ 0.04833333  0.0685      0.07242333  0.08469423  0.11207118  0.16548022
  0.20918169  0.27243684  0.31234121  0.34499129  0.43581787  0.54991938
  0.68906191  0.85751252  1.01370598  1.17191138  1.33723216  1.49560615
  1.37276374  1.54422846  1.73823283  1.95405224  2.19512985  2.42907657
  2.68100442  2.92935988  3.1759239   3.21834035  2.87259929  3.12531944
  3.4042168   3.69857813  3.41375832  3.36692623  3.02489256  3.29816838
  3.36500281  3.64807163  3.95747948  4.26918974  4.07319326  2.94476761
  2.65198948  2.62673617  1.6717359   1.5197968   1.27664023  1.18112218
  1.08814037  1.0858136 ]
Estimate time:   0.0 min 7.093541383743286 sec
Simulation time: 0.0 min 9.36409616470337 sec
[3.8622693476660661, 4.6966734129145085, 1.0858135988749464]
Update α: 0 -> 0

6
------------------
hyperparameters [ 5.77139819  3.71080984]
hyperparameters [ 5.77139819  3.71080984]
Estimate winner is stochastic
Simulation α 0
Simulation d(t) [ 0.04833333  0.0685      0.07659     0.09552757  0.09589118  0.14013355
  0.15359169  0.19601351  0.16976822  0.18408497  0.24074488  0.31234639
  0.38398892  0.33948269  0.40900948  0.49221488  0.42390217  0.43145633
  0.344704    0.43866873  0.56850643  0.72265917  0.87040345  1.0326835
  1.20294468  1.38796681  1.56869749  1.75025215  1.94858069  1.88496036
  2.08302439  2.19574319  2.22152313  2.31301088  2.5436635   2.33010429
  2.5827962   2.84169836  3.1361062   3.43698313  3.48318572  3.29363341
  3.04890549  2.10653723  2.03758971  1.25636988  1.29711618  0.95405582
  0.87357401  0.89408751]
Estimate time:   0.0 min 10.204602718353271 sec
Simulation time: 0.0 min 9.808478116989136 sec
[1.0858135988749464, 3.8622693476660661, 0.89408750966784967]
Update α: 0 -> 0

7
------------------
hyperparameters [ 5.52562856  3.40788477]
hyperparameters [ 5.52562856  3.40788477]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.01166667  0.05433333  0.07457667  0.1131391   0.12638522  0.11353502
  0.11050454  0.12459302  0.12752198  0.14183873  0.16056968  0.19550452
  0.23464705  0.18740815  0.22360161  0.27597367  0.34212779  0.29875308
  0.3052858   0.37591719  0.4932549   0.59657431  0.72348525  0.85409863
  0.98352648  1.13938195  1.33011263  1.54000062  1.71082916  1.52141152
  1.70197554  1.91217021  2.10878349  2.07176071  2.29407999  2.23759528
  2.48778718  2.74168934  3.00609719  3.29697411  3.58945147  2.88244829
  2.5580035   2.10048379  2.15653627  1.37948311  1.18882654  1.02988756
  0.96589835  0.96921124]
Estimate time:   0.0 min 9.935126304626465 sec
Simulation time: 0.0 min 10.754584789276123 sec
[0.89408750966784967, 1.0858135988749464, 0.96921124092359856]

8
------------------
hyperparameters [ 5.26477282  3.17407647]
Simulation α 0
Simulation d(t) [ 0.04833333  0.07766667  0.08291     0.08397243  0.09551604  0.11975842
  0.15179322  0.16186107  0.21726544  0.18380771  0.25046762  0.3262358
  0.43287832  0.53716228  0.46290428  0.554443    0.66226379  0.49555051
  0.58791657  0.69271462  0.81255232  0.96337174  0.83357267  0.96668605
  1.1061139   1.1795094   1.25833884  1.11598271  1.21181125  1.37203141
  1.07243386  1.23429519  1.3549412   1.52142894  1.62354628  1.52768327
  1.70204184  1.79796937  1.24526228  1.39947254  1.5644499   1.29936799
  1.10257685  0.95660524  0.8030087   0.8121717   0.70466033  0.70713077
  0.7223009   0.7467874 ]
Estimate time:   0.0 min 7.072012424468994 sec
Simulation time: 0.0 min 11.505545377731323 sec
[0.89408750966784967, 0.96921124092359856, 0.74678739894329227]
Update α: 0 -> 0

9
------------------
hyperparameters [ 5.53715058  3.19267594]
Simulation α 0
Simulation d(t) [ 0.04833333  0.08683333  0.10124333  0.1106391   0.11897243  0.13654815
  0.15262656  0.15421504  0.20045274  0.1973452   0.24650511  0.25430809
  0.27595062  0.22494792  0.22518656  0.22589195  0.25371273  0.24886385
  0.26456324  0.26221049  0.29704819  0.32703427  0.34811188  0.41872526
  0.50481978  0.51094519  0.55866713  0.49723324  0.51889511  0.5899486
  0.5113017   0.59316303  0.66210614  0.74026055  0.75591317  0.83335565
  0.88115021  0.88755237  0.74877617  0.83631976  0.87546379  0.68515072
  0.58544162  0.55856691  0.53130422  0.5391956   0.60344929  0.68014553
  0.75213119  0.85127432]
Estimate time:   0.0 min 7.528177261352539 sec
Simulation time: 0.0 min 12.722792387008667 sec
[0.74678739894329227, 0.89408750966784967, 0.85127432172343309]

10
------------------
hyperparameters [ 5.80350025  3.40563032]
hyperparameters [ 5.80350025  3.40563032]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.02083333  0.0585      0.07874333  0.1106391   0.11492882  0.12250454
  0.12774961  0.16350476  0.17231818  0.21663492  0.27412817  0.33656301
  0.36026273  0.27427112  0.31046458  0.36283664  0.26391755  0.30812487
  0.28022674  0.32502479  0.3322197   0.38053911  0.46411672  0.57806343
  0.69332462  0.79084675  0.8982441   0.74450925  0.85867112  0.75641911
  0.86031647  0.98884447  0.90998502  1.04147276  1.17712538  1.20001407
  1.34937264  1.24599172  1.16145692  1.31150051  1.13368316  1.11833763
  0.88489167  0.75266737  0.67043179  0.6920948   0.70052248  0.71305205
  0.76221872  0.80302851]
Estimate time:   0.0 min 9.865758657455444 sec
Simulation time: 0.0 min 12.920867919921875 sec
[0.74678739894329227, 0.85127432172343309, 0.80302851275755771]

11
------------------
hyperparameters [ 5.5541799   3.15111289]
hyperparameters [ 5.5541799   3.15111289]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.02083333  0.0585      0.07874333  0.1106391   0.11492882  0.12250454
  0.12774961  0.16350476  0.17731818  0.22746826  0.29412817  0.36739635
  0.44487221  0.49797263  0.56166609  0.64820482  0.39490562  0.46744627
  0.39713713  0.48026852  0.53018825  0.611841    0.72458527  0.86186532
  0.86985687  1.00071234  1.03622569  1.18028035  1.33277555  0.8948254
  1.04205609  1.17808409  1.3305307   1.23981233  1.39129829  1.57124077
  1.75893267  1.25998742  1.4227286   1.59860553  1.77941622  1.47679424
  1.23758926  1.01114574  1.08469822  0.81289839  0.78037971  0.77952413
  0.79185399  0.85050715]
Estimate time:   0.0 min 9.689013004302979 sec
Simulation time: 0.0 min 13.75478458404541 sec
[0.74678739894329227, 0.80302851275755771, 0.85050715108349506]

12
------------------
hyperparameters [ 5.3590653   3.01052406]
Simulation α 0
Simulation d(t) [ 0.04833333  0.05933333  0.08492333  0.12219423  0.16957118  0.24881355
  0.24084836  0.29493684  0.30849454  0.36364462  0.4644712   0.41775639
  0.49439892  0.58284954  0.57889158  0.6113607   0.45638132  0.54225531
  0.63712137  0.60070782  0.70721219  0.81969827  0.75501735  0.88813073
  0.53814904  0.6340045   0.74140185  0.87045651  0.9827276   0.76663352
  0.87136421  1.01239221  0.99597518  1.13329625  1.27228221  1.43805802
  1.31272756  1.10018707  1.15127118  1.31881477  1.38628733  1.18677514
  1.23462615  0.90327073  0.76261464  0.72395095  0.72071197  0.72074154
  0.7430714   0.79988467]
Estimate time:   0.0 min 7.276503324508667 sec
Simulation time: 0.0 min 15.16442346572876 sec
[0.74678739894329227, 0.80302851275755771, 0.85050715108349506, 0.79988466641279543]

13
------------------
hyperparameters [ 5.65627658  3.11420122]
Simulation α 0
Simulation d(t) [ 0.03916667  0.06266667  0.07124333  0.08397243  0.0826809   0.09275661
  0.12312475  0.1138539   0.13592494  0.17524168  0.24523493  0.32350311
  0.42014563  0.53276292  0.63978971  0.64330188  0.74612266  0.77455595
  0.90775535  1.0425534   1.18655777  1.35904385  1.53178813  1.64720739
  1.47216841  1.64052387  1.82542122  1.56200439  1.60259836  1.40307439
  1.57780508  1.04792274  1.11953602  1.21685709  1.24108732  1.14300743
  0.85509962  0.95900178  0.97924295  0.87889722  0.96220791  0.78189484
  0.7176681   0.67153954  0.70798706  0.73132406  0.78057775  0.85563689
  0.93420444  1.03883754]
Estimate time:   0.0 min 6.799630641937256 sec
Simulation time: 0.0 min 15.630383014678955 sec
[0.74678739894329227, 0.79988466641279543, 1.0388375364113469]

14
------------------
hyperparameters [ 5.75671726  3.2962867 ]
hyperparameters [ 5.75671726  3.2962867 ]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.01166667  0.06266667  0.10124333  0.14147243  0.14805188  0.14042882
  0.14163029  0.16821878  0.17281441  0.19546449  0.23379107  0.28955925
  0.28399172  0.24762093  0.27048106  0.30868645  0.28266125  0.30686857
  0.29973077  0.33702883  0.41269987  0.42101928  0.48376355  0.5510436
  0.52508266  0.47846597  0.45954144  0.51776276  0.6010913   0.50336318
  0.5764272   0.63283068  0.64361062  0.65176503  0.70658432  0.79986013
  0.90421871  0.65978025  0.71502143  0.80006502  0.8493      0.76979332
  0.69863469  0.67339953  0.68778534  0.73862234  0.81037604  0.87459452
  0.96089134  1.04984079]
Estimate time:   0.0 min 10.25171947479248 sec
Simulation time: 0.0 min 16.757467031478882 sec
[0.74678739894329227, 0.79988466641279543, 1.0388375364113469, 1.0498407940422758]

15
------------------
hyperparameters [ 5.57816585  3.07519655]
hyperparameters [ 5.57816585  3.07519655]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.02083333  0.05016667  0.05374333  0.0651809   0.08589118  0.11680022
  0.16133502  0.17361684  0.23568787  0.2041883   0.27168155  0.34578306
  0.42909225  0.31050839  0.23254869  0.28325408  0.35274153  0.26986054
  0.3172266   0.37869132  0.45102902  0.5510151   0.5520237   0.64430375
  0.66206493  0.62533909  0.64133765  0.56905023  0.65987877  0.76926559
  0.59494327  0.70847126  0.74449548  0.86514989  0.92089353  0.87416934
  1.00352791  1.04982742  0.73766691  0.84437717  0.94229853  0.78270018
  0.64256061  0.55805344  0.50861744  0.50817549  0.55659585  0.60912542
  0.64944442  0.70025421]
Estimate time:   0.0 min 9.97575068473816 sec
Simulation time: 0.0 min 17.322028875350952 sec
[0.74678739894329227, 0.79988466641279543, 1.0498407940422758, 0.70025421110294106]
Update α: 0 -> 0

16
------------------
hyperparameters [ 5.5991056   3.27658863]
Simulation α 0
Simulation d(t) [ 0.03916667  0.06683333  0.08374333  0.0956391   0.0981391   0.10889672
  0.11083787  0.14825969  0.14835522  0.1735053   0.21516521  0.26843339
  0.32424259  0.33939422  0.42308768  0.51962641  0.45769041  0.54773107
  0.50022744  0.58919216  0.70069653  0.82401594  0.97926022  1.14320693
  1.29096812  1.45349025  1.63755426  1.83744225  2.05743746  1.83279413
  2.05919148  2.30855281  2.54099943  2.81665384  2.73075716  2.95236631
  2.95448076  2.72830161  2.90020946  2.7948236   2.88845889  3.16766936
  2.93492505  2.8413384   2.27448686  2.20654289  2.4322892   1.64892727
  1.59909741  1.76961091]
Estimate time:   0.0 min 6.820049285888672 sec
Simulation time: 0.0 min 18.039384841918945 sec
[0.70025421110294106, 0.74678739894329227, 1.7696109124238273]

17
------------------
hyperparameters [ 5.38891491  3.08615698]
Simulation α 0
Simulation d(t) [ 0.04833333  0.05933333  0.07242333  0.08869423  0.08672451  0.12430022
  0.13154989  0.18480504  0.16605976  0.20954317  0.28370308  0.36697126
  0.48278045  0.56956441  0.42318513  0.50639052  0.59837797  0.48140601
  0.56627207  0.66690346  0.78424116  0.92839391  0.84115318  0.96176656
  0.8188075   0.7640214   0.73558542  0.6958773   0.79503917  0.85712793
  0.58594282  0.68447082  0.66396709  0.76545484  0.88277412  0.89884928
  1.03154118  1.17711001  1.22287567  1.24209552  1.14457288  0.87591464
  0.99032554  0.74761888  0.67446238  0.69195872  0.6928864   0.69708264
  0.7169125   0.76372576]
Estimate time:   0.0 min 6.983611345291138 sec
Simulation time: 0.0 min 18.601736068725586 sec
[0.70025421110294106, 0.74678739894329227, 1.7696109124238273, 0.76372576314654084]

18
------------------
hyperparameters [ 5.5991056   3.27658863]
hyperparameters [ 5.5991056   3.27658863]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.04833333  0.0685      0.07242333  0.08302757  0.10707118  0.15381355
  0.17916836  0.24325684  0.30199454  0.37381129  0.47963787  0.59957271
  0.75288191  0.86799919  0.76674363  0.88744903  1.01943647  0.80115111
  0.9143505   1.04248189  1.18398626  1.34563901  1.34796805  1.5127481
  1.45144683  1.46396896  1.52803297  1.36185306  1.52268159  1.69456842
  1.35690957  1.54043757  1.54740003  1.73388778  1.9195404   1.74721364
  1.94573888  2.0561454   1.52748662  1.70336355  1.78382507  1.56304513
  1.35093866  1.03949514  0.87392761  0.87633677  0.72916293  0.73163337
  0.74013684  0.73447674]
Estimate time:   0.0 min 9.562618494033813 sec
Simulation time: 0.0 min 20.46709680557251 sec
[0.70025421110294106, 0.74678739894329227, 0.73447673507367806]

19
------------------
hyperparameters [ 5.55827652  3.39335252]
hyperparameters [ 5.55827652  3.39335252]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.01166667  0.0585      0.08874333  0.1231391   0.12721855  0.13194407
  0.12368645  0.1619416   0.18484596  0.22916271  0.28582262  0.3365908
  0.27529273  0.23804032  0.26173378  0.29660584  0.26896225  0.29316957
  0.28603178  0.31916317  0.3948342   0.43990159  0.51264587  0.58659258
  0.56852043  0.48886716  0.48793117  0.55281916  0.6461477   0.5091624
  0.58639309  0.65492109  0.68417722  0.68172232  0.71987494  0.82231742
  0.93584266  0.66862894  0.71220345  0.80724704  0.89639107  0.79602103
  0.70328291  0.65412018  0.67100599  0.70934299  0.77443002  0.82945959
  0.90310852  0.96590172]
Estimate time:   0.0 min 9.517587661743164 sec
Simulation time: 0.0 min 20.59770369529724 sec
[0.70025421110294106, 0.73447673507367806, 0.96590171534728753]

20
------------------
hyperparameters [ 5.40997751  3.23990809]
Simulation α 0
Simulation d(t) [ 0.02083333  0.05016667  0.05374333  0.0651809   0.07113729  0.08204634
  0.08895808  0.13471323  0.15345093  0.20610101  0.28442759  0.3860291
  0.5093383   0.60278892  0.64990752  0.75894624  0.88426702  0.71120642
  0.81607248  0.90253721  1.02987491  1.18236099  1.16851195  1.32912533
  1.46870326  1.59705872  1.77195607  1.73681754  1.93181274  2.14036623
  1.93563053  2.15499186  2.13516884  2.35915658  2.54266933  2.31363691
  2.53549549  2.3589682   2.32286731  2.55707756  2.29073304  2.33494351
  2.51768775  1.80222773  1.7129855   1.24762497  1.24753794  0.8753109
  0.84420941  0.80823031]
Estimate time:   0.0 min 6.978076696395874 sec
Simulation time: 0.0 min 20.765759706497192 sec
[0.70025421110294106, 0.73447673507367806, 0.96590171534728753, 0.8082303119602916]

21
------------------
hyperparameters [ 5.55827652  3.39335252]
Simulation α 0
Simulation d(t) [ 0.02083333  0.05016667  0.05374333  0.06184757  0.06255784  0.08846689
  0.12633502  0.11407785  0.14781555  0.1410234   0.16101665  0.17221675
  0.19052595  0.16827354  0.18034861  0.21688734  0.26887479  0.21842693
  0.26079299  0.29428451  0.32662221  0.39910829  0.4901859   0.56150467
  0.59426586  0.5228892   0.49819044  0.36132929  0.3390298   0.39841662
  0.46731398  0.57000864  0.65828858  0.75060966  0.60632441  0.69376689
  0.52736743  0.57293625  0.5723441   0.65238769  0.72486505  0.60096956
  0.6187138   0.60253954  0.65982039  0.71149072  0.79991109  0.90207518
  1.02170534  1.1611683 ]
Estimate time:   0.0 min 6.562135934829712 sec
Simulation time: 0.0 min 21.95047926902771 sec
[0.70025421110294106, 0.73447673507367806, 1.1611682990191847]

22
------------------
hyperparameters [ 5.77297957  3.48166673]
hyperparameters [ 5.77297957  3.48166673]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.01166667  0.05016667  0.07207667  0.0881391   0.09138522  0.0946712
  0.11253934  0.15829449  0.15058564  0.15573572  0.18989564  0.23066381
  0.19109467  0.17626023  0.19245369  0.22899242  0.21122326  0.22793058
  0.219382    0.25668005  0.32901775  0.34509305  0.40617065  0.47511737
  0.50795986  0.40083443  0.36117168  0.41105967  0.48188821  0.40920435
  0.46643504  0.50010284  0.53171612  0.51987053  0.53385648  0.61129896
  0.63119616  0.51224144  0.53914929  0.59252621  0.62441305  0.56647449
  0.54996446  0.58021778  0.60749863  0.69033265  0.77458269  0.85283066
  0.95476112  1.0621549 ]
Estimate time:   0.0 min 10.10215139389038 sec
Simulation time: 0.0 min 23.460833311080933 sec
[0.70025421110294106, 0.73447673507367806, 1.1611682990191847, 1.0621549011629199]

23
------------------
hyperparameters [ 5.55827652  3.39335252]
hyperparameters [ 5.55827652  3.39335252]
Estimate winner is inner
Simulation α 0
Simulation d(t) [ 0.01166667  0.0585      0.08874333  0.1231391   0.12721855  0.13194407
  0.12368645  0.1619416   0.18484596  0.22916271  0.28582262  0.3365908
  0.27529273  0.23804032  0.26173378  0.29660584  0.26896225  0.29316957
  0.28603178  0.31916317  0.3948342   0.43990159  0.51264587  0.58659258
  0.56852043  0.48886716  0.48793117  0.55281916  0.6461477   0.5091624
  0.58639309  0.65492109  0.68417722  0.68172232  0.71987494  0.82231742
  0.93584266  0.66862894  0.71220345  0.80724704  0.89639107  0.79602103
  0.70328291  0.65412018  0.67100599  0.70934299  0.77443002  0.82945959
  0.90310852  0.96590172]
Estimate time:   0.0 min 9.583888053894043 sec
Simulation time: 0.0 min 23.41772747039795 sec
[0.70025421110294106, 0.73447673507367806, 0.96590171534728753]

24
------------------
hyperparameters [ 5.40997751  3.23990809]
Simulation α 0
Simulation d(t) [ 0.01166667  0.05433333  0.07291     0.10147243  0.10221855  0.0871712
  0.10837267  0.12829449  0.16286552  0.20884894  0.26467552  0.28832225
  0.29079811  0.36174873  0.44460886  0.36700551  0.40315963  0.30324856
  0.36478129  0.37280842  0.33373533  0.41288808  0.51896569  0.64207907
  0.60310948  0.68813161  0.80969562  0.95791695  1.09457882  1.21926193
  1.21899262  1.38668729  1.19862516  1.21180741  1.3641267   1.36942788
  0.9739475   0.73794796  0.85152248  0.99406607  1.13071009  1.27408723
  1.20841311  0.87455769  0.9839435   0.74955557  0.71664185  0.6916196
  0.68101554  0.70550204]
Estimate time:   0.0 min 6.906348705291748 sec
Simulation time: 0.0 min 24.377970457077026 sec
[0.70025421110294106, 0.73447673507367806, 0.96590171534728753, 0.70550203639517151]

CPU times: user 3min 43s, sys: 13.6 s, total: 3min 56s
Wall time: 9min 16s

In [17]:
Θs[0]


Out[17]:
array([3, 0, 2, 3, 0, 4, 2, 4, 3, 2, 3, 3, 4, 5, 4, 3, 5, 4, 3, 0, 4, 1, 4,
       5, 5, 2, 3, 3, 5, 4, 2, 5, 2, 4, 2, 4, 5, 6, 2, 2, 5, 2, 6, 2, 6, 2,
       5, 3, 2, 4])

In [18]:
G[0]


Out[18]:
array([  87.91666667,   93.83333333,   92.16666667,   91.58333333,
         91.58333333,   91.5       ,   91.08333333,   92.5       ,
         91.25      ,   94.25      ,   92.66666667,   93.        ,
         93.08333333,   97.08333333,  100.25      ,   99.41666667,
         98.58333333,  103.75      ,  102.91666667,  102.58333333,
        102.58333333,  100.91666667,  104.25      ,  103.91666667,
        105.16666667,  106.75      ,  107.66666667,  109.83333333,
        109.83333333,  109.16666667,  113.41666667,  111.16666667,
        114.08333333,  112.91666667,  114.66666667,  116.41666667,
        115.83333333,  117.25      ,  122.5       ,  122.        ,
        122.58333333,  126.        ,  129.83333333,  133.5       ,
        138.        ,  139.58333333,  147.08333333,  148.91666667,
        149.33333333,  153.08333333])

In [19]:
DEFAULT_DEMAND


Out[19]:
array([  90.        ,   90.9       ,   91.809     ,   92.72709   ,
         93.6543609 ,   94.59090451,   95.53681355,   96.49218169,
         97.45710351,   98.43167454,   99.41599129,  100.4101512 ,
        101.41425271,  102.42839524,  103.45267919,  104.48720598,
        105.53207804,  106.58739882,  107.65327281,  108.72980554,
        109.8171036 ,  110.91527463,  112.02442738,  113.14467165,
        114.27611837,  115.41887955,  116.57306835,  117.73879903,
        118.91618702,  120.10534889,  121.30640238,  122.5194664 ,
        123.74466107,  124.98210768,  126.23192876,  127.49424804,
        128.76919052,  130.05688243,  131.35745125,  132.67102577,
        133.99773602,  135.33771338,  136.69109052,  138.05800142,
        139.43858144,  140.83296725,  142.24129692,  143.66370989,
        145.10034699,  146.55135046])

In [20]:
(G[0] - DEFAULT_DEMAND) / DEFAULT_DEMAND


Out[20]:
array([-0.02314815,  0.03226989,  0.00389577, -0.01233466, -0.02211352,
       -0.03267655, -0.04661533, -0.04137311, -0.06369062, -0.04248302,
       -0.06788973, -0.07379883, -0.08214742, -0.0521834 , -0.03095791,
       -0.04852785, -0.06584486, -0.0266204 , -0.04399872, -0.05652978,
       -0.06587107, -0.09014636, -0.06939939, -0.08155934, -0.0797144 ,
       -0.07510799, -0.07640188, -0.0671441 , -0.0763803 , -0.09107573,
       -0.06503973, -0.09266119, -0.0780747 , -0.09653735, -0.09161915,
       -0.08688691, -0.1004577 , -0.09847139, -0.06743014, -0.08043222,
       -0.08518355, -0.06899565, -0.05016975, -0.03301512, -0.01031695,
       -0.00887316,  0.034041  ,  0.03656426,  0.02917282,  0.04457129])

In [25]:
np.abs(G[0] - DEFAULT_DEMAND)


Out[25]:
array([  2.08333333,   2.93333333,   0.35766667,   1.14375667,
         2.07102757,   3.09090451,   4.45348022,   3.99218169,
         6.20710351,   4.18167454,   6.74932462,   7.4101512 ,
         8.33091938,   5.34506191,   3.20267919,   5.07053932,
         6.94874471,   2.83739882,   4.73660615,   6.14647221,
         7.23377026,   9.99860796,   7.77442738,   9.22800498,
         9.1094517 ,   8.66887955,   8.90640168,   7.9054657 ,
         9.08285369,  10.93868222,   7.88973571,  11.35279974,
         9.66132773,  12.06544101,  11.56526209,  11.07758138,
        12.93585719,  12.80688243,   8.85745125,  10.67102577,
        11.41440269,   9.33771338,   6.85775718,   4.55800142,
         1.43858144,   1.24963392,   4.84203641,   5.25295677,
         4.23298634,   6.53198287])

In [22]:
1.7 / 40


Out[22]:
0.042499999999999996

In [23]:
Θs[1] - Θs[0]


Out[23]:
array([ 1,  1,  0,  1,  1, -1, -1, -1,  0,  1,  0, -1, -1, -2,  0,  1,  0,
        0,  2,  0,  0,  0, -2, -1,  0,  0, -2, -1, -1,  2, -1,  0,  0,  2,
        1,  1,  0,  0, -1,  0, -2, -2,  1, -1,  0, -1,  0,  1,  2, -2])

In [24]:
sum(N)


Out[24]:
285

In [ ]: