Source code for tests.test_energy_units

#! usr/bin/env python3
#  -*- coding: utf-8 -*-

""""
..

    Copyright 2018 G2ELab / MAGE

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

import unittest
import random
from omegalpes.general.time import TimeUnit
from omegalpes.energy.units.energy_units import *
from omegalpes.energy.units.production_units import ProductionUnit
from omegalpes.energy.units.consumption_units import ConsumptionUnit
from omegalpes.energy.units.reversible_units import ReversibleUnit


[docs] class TestInitEnergyUnit(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5) self.p_min = random.randint(1, 1000) self.p_max = random.randint(self.p_min, 2000)
[docs] def test_p_lb_int_float(self): """ Test if the expected lower bound is created for p with an int or a float """ p_min = random.choice([self.p_min, self.p_min / 1]) energy_unit = EnergyUnit(self.time, 'energy_unit', p_min=p_min) self.assertEqual(energy_unit.p.lb, 0)
[docs] def test_p_lb_list(self): """ Test if the expected lower bound is created for p with a list """ p_min = [0, 5, -3, -9.0, 10] energy_unit = EnergyUnit(self.time, 'energy_unit', p_min=p_min) self.assertListEqual(energy_unit.p.lb, [0, 0, -3, -9.0, 0])
[docs] def test_p_ub_int_float(self): """ Test if the expected upper bound is created for p with an int or a float """ p_max = random.choice([self.p_max, self.p_max / 1]) energy_unit = EnergyUnit(self.time, 'energy_unit', p_max=p_max) self.assertEqual(energy_unit.p.ub, p_max)
[docs] def test_p_ub_list(self): """ Test if the expected upper bound is created for p with a list """ p_max = [0, 5, -3, -9.0, 10] energy_unit = EnergyUnit(self.time, 'energy_unit', p_max=p_max) self.assertListEqual(energy_unit.p.ub, [0, 5, 0, 0, 10])
[docs] class TestEnergyMinMaxValues(unittest.TestCase):
[docs] def setUp(self): self.eu_no_pmax = EnergyUnit(time=TimeUnit(periods=24, dt=1), name='energy_unit_no_pmax', p_min=10) self.eu_pmax_int = EnergyUnit(time=TimeUnit(periods=24, dt=1), name='energy_unit_pmax_int', p_min=-1e6, p_max=1e2) self.pmax_list = 12*[1e6] + 12*[1e4] self.pmin_list = 12*[1e2] + 12*[-1e4] self.eu_pmax_list = EnergyUnit(time=TimeUnit(periods=24, dt=1), name='energy_unit_pmax_list', p_min=self.pmin_list, p_max=self.pmax_list)
[docs] def test_e_tot_ub(self): self.assertEqual(self.eu_no_pmax.e_tot.ub, 1e4*24) self.assertEqual(self.eu_pmax_int.e_tot.ub, 1e2*24) self.assertEqual(self.eu_pmax_list.e_tot.ub, sum(self.pmax_list))
[docs] def test_e_tot_lb(self): self.assertEqual(self.eu_no_pmax.e_tot.lb, 0) self.assertEqual(self.eu_pmax_int.e_tot.lb, -1e6*24) plb_list = [min(0,p) for p in self.pmin_list] self.assertEqual(self.eu_pmax_list.e_tot.lb, sum(plb_list))
[docs] class TestEminEmax(unittest.TestCase):
[docs] def setUp(self): self.eu_no_emax = EnergyUnit(time=TimeUnit(periods=24, dt=1), name='eu_no_emax') self.eu_emax = EnergyUnit(time=TimeUnit(periods=24, dt=1), name='eu_emax', e_min=1e2, e_max=1e7)
[docs] def test_no_emax(self): self.assertIs(self.eu_no_emax.set_e_max, None) self.assertIs(self.eu_no_emax.set_e_min, None)
[docs] def test_e_max(self): self.assertIsInstance(self.eu_emax.set_e_max, TechnicalConstraint) exp_emax = 'eu_emax_e_tot <= {0}'.format(1e7) self.assertEqual(self.eu_emax.set_e_max.exp, exp_emax) self.assertEqual(self.eu_emax.set_e_max.name, 'set_e_max')
[docs] def test_e_min(self): self.assertIsInstance(self.eu_emax.set_e_min, TechnicalConstraint) exp_emin = 'eu_emax_e_tot >= {0}'.format(1e2) self.assertEqual(self.eu_emax.set_e_min.exp, exp_emin) self.assertEqual(self.eu_emax.set_e_min.name, 'set_e_min')
[docs] class TestSetOperatingTimeRange(unittest.TestCase):
[docs] def setUp(self): self.prod_u = ProductionUnit(name='prod_u', time=TimeUnit( periods=24*2*2+8, dt=1/2)) self.prod_u.set_operating_time_range([["08:00", "12:30"], ["14:30", "22:30"]])
[docs] def test_beg_time_range(self): """Asserting the set_start_time_range is properly created for the 3 days""" self.assertIsInstance(self.prod_u.set_start_time_range_16, DailyDynamicConstraint) self.assertEqual(self.prod_u.set_start_time_range_16.exp_t, 'prod_u_u[t] == 0') start_range = list(range(0, 8*2))+list(range(24*2, 24*2+8*2))+list( range(24*4, 24*4+8)) self.assertEqual(self.prod_u.set_start_time_range_16.t_range, 'for t ''in {0}'.format(start_range))
[docs] def test_mid_time_range(self): """Asserting the set_time_range are properly created""" self.assertIsInstance(self.prod_u.set_time_range_25_29, DailyDynamicConstraint) self.assertEqual(self.prod_u.set_time_range_25_29.exp_t, 'prod_u_u[t] == 0') mid_range = list(range(25, 29))+list(range(24*2+25, 24*2+29)) self.assertEqual(self.prod_u.set_time_range_25_29.t_range, 'for t ''in {0}'.format(mid_range))
[docs] def test_end_time_range(self): """Asserting the set_end_time_range is properly created""" self.assertIsInstance(self.prod_u.set_end_time_range_45, DailyDynamicConstraint) self.assertEqual(self.prod_u.set_end_time_range_45.exp_t, 'prod_u_u[t] == 0') end_range = list(range(45, 24*2))+list(range(24*2+45, 24*2*2)) self.assertEqual(self.prod_u.set_end_time_range_45.t_range, 'for t ''in {0}'.format(end_range))
[docs] class TestAddEnergyLimitsOnTimePeriod(unittest.TestCase):
[docs] def setUp(self): periods = random.randint(24, 6000) self.time = TimeUnit(periods=periods) self.emin = random.randint(1, 1000) self.emax = random.randint(self.emin, 2000) self.energy_unit = EnergyUnit(self.time, 'energy_unit') self.start = '2018-01-01 3:00:00' self.end = '2018-01-01 8:00:00'
[docs] def test_empty_start(self): """ Test if the period_index is time.I[0:end] when no empty start """ self.energy_unit.add_energy_limits_on_time_period(e_min=self.emin, e_max=self.emax, end=self.end) time = self.time period_index = [0, 1, 2, 3, 4, 5, 6, 7] self.assertListEqual(list(eval(self.energy_unit.set_e_max_period.exp[ 42:52])), period_index)
[docs] def test_empty_end(self): """ Test if the period_index is time.I[start:] when no empty end """ self.energy_unit.add_energy_limits_on_time_period(e_min=self.emin, e_max=self.emax, start=self.start) time = self.time end = self.time.I[-1] period_index = list(range(3, end + 1)) self.assertListEqual(list(eval(self.energy_unit.set_e_max_period.exp[ 42:52])), period_index)
[docs] def test_e_min_0(self): """ Test if there is no min constraint added with e_min equals to 0 """ self.energy_unit.add_energy_limits_on_time_period(e_min=0) set_e_min_period = getattr(self.energy_unit, 'set_e_min_period') self.assertIsNone(set_e_min_period)
[docs] def test_no_e_max(self): """ Test if there is no max constraint added with e_max = None """ self.energy_unit.add_energy_limits_on_time_period(e_max=None) set_e_max_period = getattr(self.energy_unit, 'set_e_max_period') self.assertIsNone(set_e_max_period)
[docs] class TestWrongAssemblyUnit(unittest.TestCase):
[docs] def setUp(self): self.prod_u = ProductionUnit(name='prod_u', time=TimeUnit(periods=4, dt=1)) self.cons_u = ConsumptionUnit(name='cons_u', time=TimeUnit(periods=4, dt=1)) self.rev_u = ReversibleUnit(name='rev_u', time=TimeUnit(periods=4, dt=1))
[docs] def test_no_defined_prod_units(self): """Asserting an error is raised when no production unit is defined""" with self.assertRaises(IndexError): au_p_index = AssemblyUnit(name='au_p_index', time=TimeUnit(periods=4, dt=1), cons_units=[self.cons_u])
[docs] def test_no_list_prod_units(self): """Asserting an error is raised when no list of production unit is defined""" with self.assertRaises(TypeError): au_p_type = AssemblyUnit(name='au_p_type', time=TimeUnit(periods=4, dt=1), prod_units=self.prod_u, cons_units=[self.cons_u])
[docs] def test_wrong_prod_units(self): """Asserting an error is raised when the objects in the list of production units are not production units""" with self.assertRaises(TypeError): au_wpu = AssemblyUnit(time=TimeUnit(periods=4, dt=1), name='au_wrong_pu', prod_units=[self.cons_u], cons_units=[self.cons_u])
[docs] def test_no_defined_cons_units(self): """Asserting an error is raised when no consumption unit is defined""" with self.assertRaises(IndexError): au_c_index = AssemblyUnit(name='au_c_index', time=TimeUnit(periods=4, dt=1), prod_units=[self.prod_u])
[docs] def test_no_list_cons_units(self): """Asserting an error is raised when no list of consumption unit is defined""" with self.assertRaises(TypeError): au_c_type = AssemblyUnit(name='au_c_type', time=TimeUnit(periods=4, dt=1), prod_units=[self.prod_u], cons_units=self.cons_u)
[docs] def test_wrong_cons_units(self): """Asserting an error is raised when the objects in the list of consumption units are not consumption units""" with self.assertRaises(TypeError): au_wcu = AssemblyUnit(time=TimeUnit(periods=4, dt=1), name='au_wrong_cu', prod_units=[self.prod_u], cons_units=[self.prod_u])
[docs] def test_no_list_rev_units(self): """Asserting an error is raised when no list of reversible unit is defined""" with self.assertRaises(TypeError): au_r_type = AssemblyUnit(name='au_r_type', time=TimeUnit(periods=4, dt=1), rev_units=self.rev_u)
[docs] def test_wrong_rev_units(self): """Asserting an error is raised when the objects in the list of reversible units are not reversible units""" with self.assertRaises(TypeError): au_wru = AssemblyUnit(time=TimeUnit(periods=4, dt=1), name='au_wrong_ru', rev_units=[self.prod_u])
[docs] class TestAssemblyUnitAttributes(unittest.TestCase):
[docs] def setUp(self): self.prod_u = ProductionUnit(name='prod_u', time=TimeUnit(periods=4, dt=1)) self.cons_u = ConsumptionUnit(name='cons_u', time=TimeUnit(periods=4, dt=1)) self.au = AssemblyUnit(time=TimeUnit(periods=4, dt=1), name='au', prod_units=[self.prod_u], cons_units=[self.cons_u])
[docs] def test_attributes(self): """Checking the attributes of the Assembly unit are properly set : - operator, - prod_units - cons_units - poles """ self.assertEqual(self.au.prod_units, [self.prod_u]) self.assertEqual(self.au.cons_units, [self.cons_u]) poles_dict = {} poles_dict[1] = self.prod_u.poles[1] poles_dict[2] = self.cons_u.poles[1] self.assertDictEqual(self.au.poles, poles_dict)
[docs] class TestAddProdUnit(unittest.TestCase):
[docs] def setUp(self): self.pu = ProductionUnit(name='pu', time=TimeUnit(periods=4, dt=1)) self.pu2 = ProductionUnit(name='pu2', time=TimeUnit(periods=4, dt=1)) self.cons_u = ConsumptionUnit(name='cons_u', time=TimeUnit(periods=4, dt=1)) self.au = AssemblyUnit(time=TimeUnit(periods=4, dt=1), name='au', prod_units=[self.pu], cons_units=[self.cons_u])
[docs] def test_add_prod_unit(self): """Checking the method add_prod_unit""" self.au._add_production_unit(self.pu2) poles_dict = dict() poles_dict[1] = self.pu.poles[1] poles_dict[2] = self.cons_u.poles[1] poles_dict[3] = self.pu2.poles[1] self.assertEqual(self.au.poles, poles_dict) self.assertEqual(self.au.prod_units, [self.pu, self.pu2])
[docs] class TestAddConsUnit(unittest.TestCase):
[docs] def setUp(self): self.pu = ProductionUnit(name='pu', time=TimeUnit(periods=4, dt=1)) self.cons_u = ConsumptionUnit(name='cons_u', time=TimeUnit(periods=4, dt=1)) self.cons_u2 = ConsumptionUnit(name='cons_u2', time=TimeUnit(periods=4, dt=1)) self.au = AssemblyUnit(time=TimeUnit(periods=4, dt=1), name='au', prod_units=[self.pu], cons_units=[self.cons_u])
[docs] def test_add_cons_unit(self): """Checking the method add_cons_unit""" self.au._add_consumption_unit(self.cons_u2) poles_dict = dict() poles_dict[1] = self.pu.poles[1] poles_dict[2] = self.cons_u.poles[1] poles_dict[3] = self.cons_u2.poles[1] self.assertEqual(self.au.poles, poles_dict) self.assertEqual(self.au.cons_units, [self.cons_u, self.cons_u2])
[docs] class TestAddRevUnit(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=4, dt=1) self.ru = ReversibleUnit(name='ru', time=self.time) self.ru2 = ReversibleUnit(name='ru', time=self.time) self.au = AssemblyUnit(time=self.time, name='au', rev_units=[self.ru])
[docs] def test_add_rev_unit(self): """Checking the method add_rev_unit""" self.au._add_reversible_unit(self.ru2) poles_dict = dict() poles_dict[1] = self.ru.poles[1] poles_dict[2] = self.ru.poles[2] poles_dict[3] = self.ru2.poles[1] poles_dict[4] = self.ru2.poles[2] self.assertEqual(self.au.poles, poles_dict) self.assertEqual(self.au.rev_units, [self.ru, self.ru2])
[docs] class TestAddRevProdConsUnit(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=4, dt=1) self.pu = ProductionUnit(name='pu', time=self.time) self.cu = ConsumptionUnit(name='cons_u', time=self.time) self.ru = ReversibleUnit(name='ru', time=self.time) self.au = AssemblyUnit(time=self.time, name='au', prod_units=[self.pu], cons_units=[self.cu], rev_units=[self.ru])
[docs] def test_assembly_units_poles(self): """Checking the poles of assembly unit""" poles_dict = dict() poles_dict[1] = self.ru.poles[1] poles_dict[2] = self.ru.poles[2] poles_dict[3] = self.pu.poles[1] poles_dict[4] = self.cu.poles[1] self.assertEqual(self.au.poles, poles_dict)
[docs] def test_assembly_units_types(self): """Checking the types of the unit in AssemblyUnit""" self.assertIsInstance(self.au.rev_units[0], ReversibleUnit) self.assertIsInstance(self.au.prod_units[0], ProductionUnit) self.assertIsInstance(self.au.cons_units[0], ConsumptionUnit)
[docs] class TestAddStartingCost(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1) self.start_cost = random.uniform(1, 500)
[docs] def test_starting_cost_created(self): """starting_cost Quantity is created when starting_cost is provided""" eu = EnergyUnit(self.time, 'eu_sc', starting_cost=self.start_cost) self.assertIsNotNone(eu.starting_cost) self.assertIsInstance(eu.starting_cost, Quantity)
[docs] def test_starting_cost_none_by_default(self): """starting_cost is None when no starting_cost is provided""" eu = EnergyUnit(self.time, 'eu_no_sc') self.assertIsNone(eu.starting_cost)
[docs] def test_start_up_created_with_starting_cost(self): """start_up Quantity is created together with starting_cost""" eu = EnergyUnit(self.time, 'eu_su', starting_cost=self.start_cost) self.assertIsNotNone(eu.start_up) self.assertIsInstance(eu.start_up, Quantity)
[docs] def test_calc_start_cost_expression(self): """calc_start_cost constraint expression uses the correct cost value""" sc = 42.0 eu = EnergyUnit(self.time, 'eu_csc', starting_cost=sc) expected_exp = 'eu_csc_starting_cost[t] == {0} * eu_csc_start_up[t]'.format(sc) self.assertEqual(eu.calc_start_cost.exp_t, expected_exp)
[docs] def test_calc_start_cost_t_range(self): """calc_start_cost is defined over time.I[:-1]""" eu = EnergyUnit(self.time, 'eu_csc_tr', starting_cost=self.start_cost) self.assertEqual(eu.calc_start_cost.t_range, 'for t in time.I[:-1]')
[docs] def test_starting_cost_lb(self): """starting_cost lower bound is 0""" eu = EnergyUnit(self.time, 'eu_sc_lb', starting_cost=self.start_cost) self.assertEqual(eu.starting_cost.lb, 0)
[docs] def test_starting_cost_duplicate_raises(self): """Adding a starting cost twice raises ValueError""" eu = EnergyUnit(self.time, 'eu_sc_dup', starting_cost=self.start_cost) with self.assertRaises(ValueError): eu._add_starting_cost(self.start_cost)
# --------------------------------------------------------------------------- # Operating cost # ---------------------------------------------------------------------------
[docs] class TestAddOperatingCost(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1) self.op_cost = random.uniform(0.01, 10.0)
[docs] def test_operating_cost_created(self): """operating_cost Quantity is created when operating_cost is provided""" eu = EnergyUnit(self.time, 'eu_oc', operating_cost=self.op_cost) self.assertIsNotNone(eu.operating_cost) self.assertIsInstance(eu.operating_cost, Quantity)
[docs] def test_operating_cost_none_by_default(self): """operating_cost is None when no operating_cost is provided""" eu = EnergyUnit(self.time, 'eu_no_oc') self.assertIsNone(eu.operating_cost)
[docs] def test_calc_operating_cost_expression_float(self): """calc_operating_cost expression is correct for a float cost""" oc = 3.5 eu = EnergyUnit(self.time, 'eu_oc_f', operating_cost=oc) expected_exp = ( 'eu_oc_f_operating_cost[t] == {0} * eu_oc_f_p[t] * time.DT' .format(oc) ) self.assertEqual(eu.calc_operating_cost.exp_t, expected_exp)
[docs] def test_calc_operating_cost_expression_list(self): """calc_operating_cost expression is correct for a list cost""" oc_list = [1.0, 2.0, 3.0, 4.0, 5.0] eu = EnergyUnit(self.time, 'eu_oc_l', operating_cost=oc_list) expected_exp = ( 'eu_oc_l_operating_cost[t] == {0}[t] * eu_oc_l_p[t] * time.DT' .format(oc_list) ) self.assertEqual(eu.calc_operating_cost.exp_t, expected_exp)
[docs] def test_calc_operating_cost_t_range(self): """calc_operating_cost is defined over all time.I""" eu = EnergyUnit(self.time, 'eu_oc_tr', operating_cost=self.op_cost) self.assertEqual(eu.calc_operating_cost.t_range, 'for t in time.I')
[docs] def test_operating_cost_lb(self): """operating_cost lower bound is 0""" eu = EnergyUnit(self.time, 'eu_oc_lb', operating_cost=self.op_cost) self.assertEqual(eu.operating_cost.lb, 0)
[docs] def test_operating_cost_wrong_list_size_raises(self): """Providing a list whose length != time.LEN raises IndexError""" wrong_list = [1.0, 2.0] # length 2 != 5 with self.assertRaises(IndexError): EnergyUnit(self.time, 'eu_oc_bad', operating_cost=wrong_list)
[docs] def test_operating_cost_wrong_type_raises(self): """Providing a non-numeric, non-list operating_cost raises TypeError""" with self.assertRaises(TypeError): EnergyUnit(self.time, 'eu_oc_type', operating_cost='bad_value')
[docs] def test_operating_cost_duplicate_raises(self): """Adding an operating cost twice raises ValueError""" eu = EnergyUnit(self.time, 'eu_oc_dup', operating_cost=self.op_cost) with self.assertRaises(ValueError): eu._add_operating_cost(self.op_cost)
# --------------------------------------------------------------------------- # CO2 emissions # ---------------------------------------------------------------------------
[docs] class TestAddCo2Emissions(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1) self.co2 = random.uniform(0.1, 2.0)
[docs] def test_co2_emissions_created(self): """co2_emissions Quantity is created when co2_out is provided""" eu = EnergyUnit(self.time, 'eu_co2', co2_out=self.co2) self.assertIsNotNone(eu.co2_emissions) self.assertIsInstance(eu.co2_emissions, Quantity)
[docs] def test_co2_emissions_none_by_default(self): """co2_emissions is None when no co2_out is provided""" eu = EnergyUnit(self.time, 'eu_no_co2') self.assertIsNone(eu.co2_emissions)
[docs] def test_calc_co2_expression_float(self): """calc_co2_emissions expression is correct for a float co2_out""" co2 = 0.5 eu = EnergyUnit(self.time, 'eu_co2_f', co2_out=co2) expected_exp = ( 'eu_co2_f_co2_emissions[t] == {0} * eu_co2_f_p[t] * time.DT' .format(co2) ) self.assertEqual(eu.calc_co2_emissions.exp_t, expected_exp)
[docs] def test_calc_co2_expression_list(self): """calc_co2_emissions expression is correct for a list co2_out""" co2_list = [0.1, 0.2, 0.3, 0.4, 0.5] eu = EnergyUnit(self.time, 'eu_co2_l', co2_out=co2_list) expected_exp = ( 'eu_co2_l_co2_emissions[t] == {0}[t] * eu_co2_l_p[t] * time.DT' .format(co2_list) ) self.assertEqual(eu.calc_co2_emissions.exp_t, expected_exp)
[docs] def test_co2_wrong_list_size_raises(self): """Providing a list whose length != time.LEN raises IndexError""" with self.assertRaises(IndexError): EnergyUnit(self.time, 'eu_co2_bad', co2_out=[0.1, 0.2])
[docs] def test_co2_wrong_type_raises(self): """Providing a non-numeric, non-list co2_out raises TypeError""" with self.assertRaises(TypeError): EnergyUnit(self.time, 'eu_co2_type', co2_out='bad_value')
[docs] def test_co2_duplicate_raises(self): """Adding co2 emissions twice raises ValueError""" eu = EnergyUnit(self.time, 'eu_co2_dup', co2_out=self.co2) with self.assertRaises(ValueError): eu._add_co2_emissions(self.co2)
# --------------------------------------------------------------------------- # Objectives # ---------------------------------------------------------------------------
[docs] class TestMinimizeStartingCost(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1)
[docs] def test_min_start_cost_objective_created(self): """minimize_starting_cost creates min_start_cost Objective""" eu = EnergyUnit(self.time, 'eu_msc', starting_cost=10.0) eu.minimize_starting_cost() self.assertIsInstance(eu.min_start_cost, Objective)
[docs] def test_min_start_cost_expression(self): """minimize_starting_cost Objective has the correct expression""" eu = EnergyUnit(self.time, 'eu_msc_exp', starting_cost=10.0) eu.minimize_starting_cost() expected = 'lpSum(eu_msc_exp_starting_cost[t] for t in time.I)' self.assertEqual(eu.min_start_cost.exp, expected)
[docs] def test_min_start_cost_weight(self): """minimize_starting_cost respects the weight parameter""" eu = EnergyUnit(self.time, 'eu_msc_w', starting_cost=10.0) eu.minimize_starting_cost(weight=3) self.assertEqual(eu.min_start_cost.weight, 3)
[docs] def test_min_start_cost_no_cost_raises(self): """minimize_starting_cost raises ValueError when no starting_cost set""" eu = EnergyUnit(self.time, 'eu_msc_err') with self.assertRaises(ValueError): eu.minimize_starting_cost()
[docs] class TestMinimizeOperatingCost(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1)
[docs] def test_min_operating_cost_objective_created(self): """minimize_operating_cost creates min_operating_cost Objective""" eu = EnergyUnit(self.time, 'eu_moc', operating_cost=2.0) eu.minimize_operating_cost() self.assertIsInstance(eu.min_operating_cost, Objective)
[docs] def test_min_operating_cost_expression(self): """minimize_operating_cost Objective has the correct expression""" eu = EnergyUnit(self.time, 'eu_moc_exp', operating_cost=2.0) eu.minimize_operating_cost() expected = 'lpSum(eu_moc_exp_operating_cost[t] for t in time.I)' self.assertEqual(eu.min_operating_cost.exp, expected)
[docs] def test_min_operating_cost_weight(self): """minimize_operating_cost respects the weight parameter""" eu = EnergyUnit(self.time, 'eu_moc_w', operating_cost=2.0) eu.minimize_operating_cost(weight=5) self.assertEqual(eu.min_operating_cost.weight, 5)
[docs] def test_min_operating_cost_no_cost_raises(self): """minimize_operating_cost raises ValueError when no operating_cost set""" eu = EnergyUnit(self.time, 'eu_moc_err') with self.assertRaises(ValueError): eu.minimize_operating_cost()
[docs] class TestMinimizeCosts(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1)
[docs] def test_minimize_costs_both(self): """minimize_costs creates both objectives when both costs are set""" eu = EnergyUnit(self.time, 'eu_mc_both', starting_cost=10.0, operating_cost=2.0) eu.minimize_costs() self.assertIsInstance(eu.min_start_cost, Objective) self.assertIsInstance(eu.min_operating_cost, Objective)
[docs] def test_minimize_costs_only_starting(self): """minimize_costs creates only min_start_cost when only starting_cost is set""" eu = EnergyUnit(self.time, 'eu_mc_sc_only', starting_cost=10.0) eu.minimize_costs() self.assertIsInstance(eu.min_start_cost, Objective) self.assertFalse(hasattr(eu, 'min_operating_cost'))
[docs] def test_minimize_costs_only_operating(self): """minimize_costs creates only min_operating_cost when only operating_cost is set""" eu = EnergyUnit(self.time, 'eu_mc_oc_only', operating_cost=-2.0) eu.minimize_costs() self.assertIsInstance(eu.min_operating_cost, Objective) self.assertFalse(hasattr(eu, 'min_start_cost'))
[docs] def test_minimize_costs_none_set(self): """minimize_costs creates no objective when no cost is set""" eu = EnergyUnit(self.time, 'eu_mc_none') eu.minimize_costs() self.assertFalse(hasattr(eu, 'min_start_cost')) self.assertFalse(hasattr(eu, 'min_operating_cost'))
[docs] class TestMinimizeEnergy(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1)
[docs] def test_min_energy_objective_created(self): """minimize_energy creates min_energy Objective""" eu = EnergyUnit(self.time, 'eu_me') eu.minimize_energy() self.assertIsInstance(eu.min_energy, Objective)
[docs] def test_min_energy_expression(self): """minimize_energy Objective has the correct expression""" eu = EnergyUnit(self.time, 'eu_me_exp') eu.minimize_energy() expected = 'lpSum(eu_me_exp_p[t] for t in time.I)' self.assertEqual(eu.min_energy.exp, expected)
[docs] def test_min_energy_weight(self): """minimize_energy respects the weight parameter""" eu = EnergyUnit(self.time, 'eu_me_w') eu.minimize_energy(weight=2) self.assertEqual(eu.min_energy.weight, 2)
[docs] class TestMinimizeCo2Emissions(unittest.TestCase):
[docs] def setUp(self): self.time = TimeUnit(periods=5, dt=1)
[docs] def test_min_co2_objective_created(self): """minimize_co2_emissions creates min_co2_emissions Objective""" eu = EnergyUnit(self.time, 'eu_mco2', co2_out=0.5) eu.minimize_co2_emissions() self.assertIsInstance(eu.min_co2_emissions, Objective)
[docs] def test_min_co2_expression(self): """minimize_co2_emissions Objective has the correct expression""" eu = EnergyUnit(self.time, 'eu_mco2_exp', co2_out=0.5) eu.minimize_co2_emissions() expected = 'lpSum(eu_mco2_exp_co2_emissions[t] for t in time.I)' self.assertEqual(eu.min_co2_emissions.exp, expected)
[docs] def test_min_co2_weight(self): """minimize_co2_emissions respects the weight parameter""" eu = EnergyUnit(self.time, 'eu_mco2_w', co2_out=0.5) eu.minimize_co2_emissions(weight=4) self.assertEqual(eu.min_co2_emissions.weight, 4)
if __name__ == '__main__': unittest.main()