forked from switch-model/switch
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutilities_test.py
83 lines (73 loc) · 3.32 KB
/
utilities_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright 2015 The Switch Authors. All rights reserved.
# Licensed under the Apache License, Version 2, which is in the LICENSE file.
import logging
import os
import shutil
import tempfile
import unittest
import switch_model.utilities as utilities
import switch_model.solve
from pyomo.environ import DataPortal
from testfixtures import compare
class UtilitiesTest(unittest.TestCase):
def test_approx_equal(self):
assert not utilities.approx_equal(1, 2)
assert not utilities.approx_equal(1, 1.02)
assert utilities.approx_equal(1, 1.01)
assert utilities.approx_equal(1, 1)
def test_save_inputs_as_dat(self):
(model, instance) = switch_model.solve.main(
args=["--inputs-dir", os.path.join('examples', '3zone_toy', 'inputs')],
return_model=True, return_instance=True, attach_data_portal=True
)
temp_dir = tempfile.mkdtemp(prefix="switch_test_")
try:
dat_path = os.path.join(temp_dir, "complete_inputs.dat")
utilities.save_inputs_as_dat(model, instance, save_path=dat_path)
reloaded_data = DataPortal(model=model)
reloaded_data.load(filename=dat_path)
# Replace 'inf' with inf since Pyomo no longer does
utilities.SwitchCSVDataManger.convert_inf_to_float(reloaded_data._data[None])
compare(reloaded_data.data(), instance.DataPortal.data())
finally:
shutil.rmtree(temp_dir)
def test_check_mandatory_components(self):
from pyomo.environ import ConcreteModel, Param, Set, Any
from switch_model.utilities import check_mandatory_components
mod = ConcreteModel()
mod.set_A = Set(initialize=[1,2])
mod.paramA_full = Param(mod.set_A, initialize={1:'a',2:'b'}, within=Any)
mod.paramA_empty = Param(mod.set_A)
mod.set_B = Set()
mod.paramB_empty = Param(mod.set_B)
mod.paramC = Param(initialize=1)
mod.paramD = Param()
check_mandatory_components(mod, 'set_A', 'paramA_full')
check_mandatory_components(mod, 'paramB_empty')
check_mandatory_components(mod, 'paramC')
with self.assertRaises(ValueError):
check_mandatory_components(mod, 'set_A', 'paramA_empty')
with self.assertRaises(ValueError):
check_mandatory_components(mod, 'set_A', 'set_B')
with self.assertRaises(ValueError):
check_mandatory_components(mod, 'paramC', 'paramD')
def test_min_data_check(self):
from switch_model.utilities import _add_min_data_check
from pyomo.environ import AbstractModel, Param, Set, Any
mod = AbstractModel()
_add_min_data_check(mod)
mod.set_A = Set(initialize=[1,2])
mod.paramA_full = Param(mod.set_A, initialize={1:'a',2:'b'}, within=Any)
mod.paramA_empty = Param(mod.set_A)
mod.min_data_check('set_A', 'paramA_full')
self.assertIsNotNone(mod.create_instance())
mod.min_data_check('set_A', 'paramA_empty')
# Fiddle with the pyomo logger to suppress its error message
logger = logging.getLogger('pyomo.core')
orig_log_level = logger.level
logger.setLevel(logging.FATAL)
with self.assertRaises(ValueError):
mod.create_instance()
logger.setLevel(orig_log_level)
if __name__ == '__main__':
unittest.main()