Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions data_generation/create_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from ema_workbench import (Model, RealParameter, ScalarOutcome)
import pandas as pd
from model.dps_lake_model import lake_model

model = Model('lakeproblem', function=lake_model)

#specify uncertainties
model.uncertainties = [RealParameter('b', 0.1, 0.45),
RealParameter('q', 2.0, 4.5),
RealParameter('mean', 0.01, 0.05),
RealParameter('stdev', 0.001, 0.005),
RealParameter('delta', 0.93, 0.99)]

# set levers
model.levers = [RealParameter("c1", -2, 2),
RealParameter("c2", -2, 2),
RealParameter("r1", 0, 2),
RealParameter("r2", 0, 2),
RealParameter("w1", 0, 1)]

#specify outcomes
# note how we need to explicitely indicate the direction
model.outcomes = [ScalarOutcome('max_P', kind=ScalarOutcome.MINIMIZE),
ScalarOutcome('utility', kind=ScalarOutcome.MAXIMIZE),
ScalarOutcome('inertia', kind=ScalarOutcome.MAXIMIZE),
ScalarOutcome('reliability', kind=ScalarOutcome.MAXIMIZE)]




#do the optimization-simulation
from ema_workbench import MultiprocessingEvaluator, ema_logging
from ema_workbench.em_framework.evaluators import BaseEvaluator

ema_logging.log_to_stderr(ema_logging.INFO)

with MultiprocessingEvaluator(model) as evaluator:
results1 = evaluator.optimize(nfe=5e5, searchover='levers',
epsilons=[0.1,]*len(model.outcomes))


#save the results - only objective values
results1.to_csv('results.csv')

#get the policies - values of parameters for each of the rbfs
policies = results1.iloc[:, :5]



from model.dps_lake_model import get_antropogenic_release
import numpy as np
import math



#create empty data frame fro the time series of release decisions
release_decisions = pd.DataFrame()



#for each policy, simulate the decisions over time
for policy in range(len(policies)):
c1 = policies.iloc[policy, 0]
c2 = policies.iloc[policy, 1]
r1 = policies.iloc[policy, 2]
r2 = policies.iloc[policy, 3]
w1 = policies.iloc[policy, 4]

myears = 100

b=0.42
q=2.0
mean=0.02
stdev=0.001
delta=0.98
alpha=0.4
nsamples=100
myears=100
X = np.zeros((myears,))

X[0] = 0.0
decision = 0.1
decisions = np.zeros(myears,)
decisions[0] = decision
natural_inflows = np.random.lognormal(
math.log(mean**2 / math.sqrt(stdev**2 + mean**2)),
math.sqrt(math.log(1.0 + stdev**2 / mean**2)),
size=myears)

for t in range(1, myears):

# here we use the decision rule
decision = get_antropogenic_release(X[t-1], c1, c2, r1, r2, w1)
decisions[t] = decision

X[t] = (1-b)*X[t-1] + X[t-1]**q/(1+X[t-1]**q) + decision +\
natural_inflows[t-1]

release_decisions[str(policy)] = pd.DataFrame(decisions)


#save release decisions as time series for each of the policy
release_decisions.to_csv('release_decisions.csv')

101 changes: 101 additions & 0 deletions data_generation/release_decisions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
,0,1,2,3,4,5,6,7
0,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
1,0.1,0.06557875165232617,0.03309879128122532,0.1,0.049565267100535965,0.02185296093697179,0.08569149270420635,0.1
2,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
3,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
4,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
5,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
6,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
7,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
8,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
9,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
10,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
11,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
12,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
13,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
14,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
15,0.1,0.1,0.1,0.1,0.1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,0.1
76,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
77,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
78,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
79,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
80,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
81,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
82,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
83,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
84,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
85,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
86,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
87,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
88,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
89,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
90,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
91,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
92,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
93,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
94,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
95,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
96,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
97,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
98,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
99,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1
9 changes: 9 additions & 0 deletions data_generation/results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
,c1,c2,r1,r2,w1,max_P,utility,inertia,reliability
0,0.05422634381052014,-1.3829692124724173,0.0006180037012011863,1.3180615770463042,0.9487422654053417,2.060713668881164,1.7000436121480709,0.0519,0.5536
1,-0.04342079540283349,-1.2131637037821896,0.12817785893471817,1.4825306991966192,0.9475426849035579,1.8799544352620934,1.5028891651180087,0.0197,0.5889
2,-0.0165640435728814,1.1883445038586118,0.10214973885067827,1.4617018004259805,0.9459083436246151,1.3666683170869716,1.300086331187123,0.0197,0.7626
3,0.054993827185723605,-0.6177370031804852,0.002308100662628938,1.211018235404881,0.9455220076474259,2.060616310880478,1.4432930857929192,0.2027,0.5536
4,0.02008913171794783,-0.7874109771928711,0.06707445133161025,0.9763389017306744,0.9543930211452343,1.7463670088417103,1.4029019726736158,0.0197,0.6253
5,-0.03847870004852087,-0.10088119825376962,0.12922382479171207,1.0906667232814582,0.822383921107195,0.1552007742978849,1.2298680723696087,0.0197,1.0
6,0.0244456886843499,-0.6205744310780451,0.10036982209437494,1.1613880661849278,0.4841718944361656,1.9933616252886157,1.6284718578564088,0.0001,0.5629
7,0.054993827185723605,-1.1593201946757956,0.0017663454196513453,1.1594794826157335,0.9482664000969719,2.060454410759735,1.643987930822998,0.1335,0.5536
Binary file added model/__pycache__/dps_lake_model.cpython-39.pyc
Binary file not shown.
122 changes: 122 additions & 0 deletions plotly_dash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import plotly.express as px
from dash import Dash, dcc, html

import pandas as pd
from dash import Dash, dcc, html, Input, Output
import pandas as pd

from dash import Dash, dcc, html, Input, Output, dash_table

import plotly.express as px

import pandas as pd
import plotly.graph_objects as go



results = pd.read_csv('data_generation/results.csv').iloc[:, -4:]
release_decisions = pd.read_csv('data_generation/release_decisions.csv').iloc[:,1:]
fig = px.parallel_coordinates(results)

fig_decisions = px.line(release_decisions)

app = Dash()



app.layout = html.Div([

dcc.Graph(

id='parallel_coordinates',

figure=fig

),



dash_table.DataTable(

id='datatable',

columns=[{"name": i, "id": i, "deletable": True, "selectable": True} for i in results.columns],

data=results.to_dict('records'),

editable=True,

filter_action="native",

sort_action="native",

sort_mode="multi",

column_selectable="single",

row_selectable="multi",

row_deletable=True,

selected_columns=[],

selected_rows=[],

page_action="native",

page_current=0,

page_size=10,

),
dcc.Graph(

id='time_series_decisions',

figure=fig_decisions

),

])



# Handle row selections:




@app.callback(
Output('parallel_coordinates', 'figure'),
[Input('datatable', 'selected_rows')]
)


def update_graph(selected_rows):
if sum(results.index.isin(selected_rows).astype(int)) == 0:
res = results
else:
res = results[results.index.isin(selected_rows)]
fig = px.parallel_coordinates(
res
)
return fig

@app.callback(
Output('time_series_decisions', 'figure'),
[Input('datatable', 'selected_rows')]
)


def update_graph_release(selected_rows):
if sum(results.index.isin(selected_rows).astype(int)) == 0:
res = release_decisions
else:
res = release_decisions.iloc[:, selected_rows]
fig_decisions = px.line(res)
return fig_decisions



if __name__ == '__main__':
app.run_server(debug=True, use_reloader=False)