Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def simple_linear_model(request):
"""
Make a simple model with a single Input and Output.
Input -> Link -> Output
"""
model = Model()
inpt = Input(model, name="Input")
lnk = Link(model, name="Link", cost=1.0)
inpt.connect(lnk)
otpt = Output(model, name="Output")
lnk.connect(otpt)
return model
def test_xml_timeseries():
"""Test serialisation/deserialisation of Timeseries"""
model = pywr.core.Model()
metadata = {
'type': 'csv',
'path': 'tests/timeseries1.csv',
'column': 'Data',
}
ts = pywr.parameters.Timeseries('test1', None, metadata)
xml = ts.xml('test1')
del(ts)
ts = pywr.parameters.Timeseries.from_xml(model, xml)
assert(ts.name == 'test1')
assert(ts.metadata['path'] == 'tests/timeseries1.csv')
assert(ts.df['2015-01-01'] == 23.92)
def three_storage_model(request):
"""
Make a simple model with three input, storage and output nodes. Also adds
an `AggregatedStorage` and `AggregatedNode`.
Input 0 -> Storage 0 -> Output 0
Input 1 -> Storage 1 -> Output 1
Input 2 -> Storage 2 -> Output 2
"""
model = pywr.core.Model(
start=pandas.to_datetime('2016-01-01'),
end=pandas.to_datetime('2016-01-05'),
timestep=datetime.timedelta(1),
)
all_res = []
all_otpt = []
for num in range(3):
inpt = Input(model, name="Input {}".format(num), max_flow=5.0*num, cost=-1)
res = Storage(model, name="Storage {}".format(num), num_outputs=1, num_inputs=1, max_volume=20, initial_volume=10+num)
otpt = Output(model, name="Output {}".format(num), max_flow=8+num, cost=-999)
inpt.connect(res)
res.connect(otpt)
def test_catchment_many_successors():
"""Test if node with fixed flow can have multiple successors. See #225"""
model = Model()
catchment = Catchment(model, "catchment", flow=100)
out1 = Output(model, "out1", max_flow=10, cost=-100)
out2 = Output(model, "out2", max_flow=15, cost=-50)
out3 = Output(model, "out3")
catchment.connect(out1)
catchment.connect(out2)
catchment.connect(out3)
model.check()
model.run()
assert_allclose(out1.flow, 10)
assert_allclose(out2.flow, 15)
assert_allclose(out3.flow, 75)
def model(solver):
model = Model(
solver=solver,
start=pandas.to_datetime("2016-01-01"),
end=pandas.to_datetime("2016-01-10"),
)
return model
def model():
return Model()
def test_virtual_storage_duplicate_route():
""" Test the VirtualStorage node """
model = pywr.core.Model()
inpt = Input(model, "Input", max_flow=20)
lnk = Link(model, "Link")
inpt.connect(lnk)
otpt = Output(model, "Output", max_flow=10, cost=-10.0)
lnk.connect(otpt)
vs = pywr.core.VirtualStorage(model, "Licence", [lnk, otpt], factors=[0.5, 1.0], initial_volume=10.0, max_volume=10.0)
model.setup()
assert_allclose(vs.volume, [10], atol=1e-7)
model.step()
assert_allclose(otpt.flow, [10/1.5], atol=1e-7)
import numpy as np
import inspyred
from ..core import Model
class InspyredOptimisationModel(Model):
""" A pywr.core.Model subclass to enable optimisation using inspyred.
This classes overloads Model.setup() to create cached variable and objective maps to use with inspyred.
A generator, bounder and evaluator method are provided to use with the inspyred algorithms.
"""
def _cache_variable_parameters(self):
variables = []
variable_map = [0, ]
for var in self.variables:
size = var.double_size + var.integer_size
if size <= 0:
raise ValueError('Variable parameter "{}" does not have a size > 0.'.format(var.name))