Skip to content

Commit

Permalink
Merge pull request #5 from FlanFlanagan/master
Browse files Browse the repository at this point in the history
Adds tests for ARMA and some other minor fixes
  • Loading branch information
scopatz authored Mar 23, 2018
2 parents aff100d + c862399 commit 9847969
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 70 deletions.
22 changes: 11 additions & 11 deletions d3ploy/no_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ class NOInst(Institution):

steps = ts.Int(
doc="The number of timesteps forward for ARMA or order of the MA",
tooltip="Std Dev off mean for ARMA",
uilabel="Demand Std Dev",
default=1
tooltip="The number of predicted steps forward",
uilabel="Timesteps for Prediction",
default=2
)
back_steps = ts.Int(
doc="This is the number of steps backwards from the current time step" +
"that will be used to make the prediction. If this is set to '0'" +
"then the calculation will use all values in the time series.",
tooltip="",
uilabel="",
uilabel="Back Steps",
default=10
)

Expand All @@ -120,8 +120,8 @@ def __init__(self, *args, **kwargs):
def enter_notify(self):
super().enter_notify()
lib.TIME_SERIES_LISTENERS[self.supply_commod].append(self.extract_supply)
lib.TIME_SERIES_LISTENERS[self.demand_commod].append(self.extract_demand)

lib.TIME_SERIES_LISTENERS[self.demand_commod].append(self.extract_demand)
def tick(self):
"""
This is the tock method for the institution. Here the institution determines the difference
Expand All @@ -138,11 +138,11 @@ def tick(self):
else:
print("No facility production rate available for " + proto)
number = np.ceil(-1*diff/prod_rate)
for i in range(number):
for i in range(int(number)):
self.context.schedule_build(self, proto)
i += 1
if self.record:
out_text = "Time " + str(time-1) + "Deployed " + str(len(self.children))
out_text = "Time " + str(time) + " Deployed " + str(len(self.children))
out_text += " supply " + str(self.commodity_supply[time-1])
out_text += " demand " + str(self.commodity_demand[time-1]) + "\n"
with open(self.demand_commod+".txt", 'a') as f:
Expand All @@ -164,9 +164,9 @@ def calc_diff(self, time):
demand : double
The calculated demand of the demand commodity at [time]
"""
if not self.commodity_demand:
if time not in self.commodity_demand:
self.commodity_demand[time] = self.initial_demand
if not self.commodity_supply:
if time not in self.commodity_supply:
self.commodity_supply[time] = self.initial_demand
try:
supply = CALC_METHODS[self.calc_method](self.commodity_supply, steps = self.steps,
Expand All @@ -176,7 +176,7 @@ def calc_diff(self, time):
supply = CALC_METHODS['ma'](self.commodity_supply)
if self.demand_commod == 'POWER':
demand = self.demand_calc(time+2)
self.commodity_demand[time+1] = demand
self.commodity_demand[time+2] = demand
try:
demand = CALC_METHODS[self.calc_method](self.commodity_demand, steps = self.steps,
std_dev = self.demand_std_dev,
Expand Down
96 changes: 37 additions & 59 deletions tests/no_inst_tests.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,44 @@
import json
import subprocess
import os
import nose

from nose.tools import assert_in

inputfile = {
"simulation": {
"archetypes": {
"spec": [
{"lib": "d3ploy.demand_fac", "name": "DemandFac"},
{"lib": "agents", "name": "NullRegion"},
{"lib": "d3ploy.no_inst", "name": "NOInst"}
]
},
"control": {"duration": "50", "startmonth": "1", "startyear": "2000"},
"facility": [
{
"config": {
"DemandFac": {
"demand_commod": "fuel_rx",
"demand_rate_max": "2000.",
"demand_rate_min": "2000.",
"supply_commod": "power_rx",
"supply_rate_max": "1000.",
"supply_rate_min": "1000."
}
},
"name": "Reactor"
}
],
"region": {
"config": {"NullRegion": "\n "},
"institution": [
{
"config": {
"NOInst": {
"calc_method": "arma",
"demand_commod": "POWER",
"demand_std_dev": "1.5",
"growth_rate": "0.05",
"initial_demand": "100000.0",
"prototypes": {"val": "Reactor"},
"steps": "1",
"supply_commod": "power_rx",
"record": 1
}
},
"initialfacilitylist": {"entry": {"number": "100", "prototype": "Reactor"}},
"name": "ReactorInst"
}
],
"name": "SingleRegion"
}
}
}

def test_arma():
cleaning()
env = dict(os.environ)
env['PYTHONPATH'] = "."
s = subprocess.check_output(['cyclus', '-o', 'dummy.h5', 'test.json'], universal_newlines=True, env=env)
# Testing Building Reactors
with open('POWER.txt') as f:
rx_hist = f.readlines()
reactors, power = read_info(rx_hist)
assert reactors > 0
assert power < 0
# Testing building mines
with open('uranium.txt') as f:
mine_hist = f.readlines()
mine, uranium = read_info(mine_hist)
assert mine > 0
assert uranium < 0
cleaning()

def test_demand_calc():
def cleaning():
if os.path.exists('dummy.h5'):
os.remove('dummy.h5')
with open('dummy.json', 'w') as f:
json.dump(inputfile, f)
env = dict(os.environ)
env['PYTHONPATH'] = "."
s = subprocess.check_output(['cyclus', '-o', 'dummy.h5', 'dummy.json'], universal_newlines=True, env=env)
if os.path.exists('POWER.txt'):
os.remove('POWER.txt')
if os.path.exists('uranium.txt'):
os.remove('uranium.txt')

def read_info(text):
diff = []
#check to see if facilities are deployed
first = float(text[0].split()[3])
last = float(text[-1].split()[3])
built = last > first
#check to make sure overprediction occurs
for line in text:
vals = line.split()
diff.append((float(vals[7])-float(vals[5]))/float(vals[7]))
avg = sum(diff)/len(diff)
return built, avg

0 comments on commit 9847969

Please sign in to comment.