initial commit v0.2.1
This commit is contained in:
commit
2b9a10979b
19 files changed
+1665
No files matched your search
@@ -0,0 +1,65 @@
|
||||
"""Offline tests: run python -m unittest discover -s tests -v."""
|
||||
import importlib.util
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
path = Path(__file__).resolve().parents[1] / 'custom_components' / 'heating_mpc' / 'physics.py'
|
||||
spec = importlib.util.spec_from_file_location('heating_mpc_physics', path)
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
import sys
|
||||
sys.modules[spec.name] = m
|
||||
spec.loader.exec_module(m)
|
||||
|
||||
|
||||
class ModelTests(unittest.TestCase):
|
||||
def test_fitting_and_restore(self):
|
||||
learn = m.Learner()
|
||||
t = 20.0
|
||||
lag = 0.0
|
||||
for step in range(1000):
|
||||
heater = int(step % 96 < 35)
|
||||
lag = lag + m.HEAT_LAG * (heater - lag)
|
||||
value = t + 0.003 * (8 - t) + 0.055 * lag + 0.001
|
||||
learn.learn(t, value, 8, lag)
|
||||
t = value
|
||||
self.assertGreaterEqual(learn.count, 950)
|
||||
self.assertAlmostEqual(learn.theta[0], 0.003, delta=0.003)
|
||||
self.assertAlmostEqual(learn.theta[1], 0.055, delta=0.03)
|
||||
restored = m.Learner.from_dict(learn.as_dict())
|
||||
self.assertEqual(restored.count, learn.count)
|
||||
self.assertEqual(restored.theta, learn.theta)
|
||||
|
||||
def test_optimize_warm_room_prefers_off(self):
|
||||
zones = {'living': 23.5, 'bedroom': 23.4}
|
||||
learners = {'living': m.Learner(), 'bedroom': m.Learner()}
|
||||
out = m.optimize(zones, 12, 0, 22, {'living': 2, 'bedroom': 1},
|
||||
learners, False, 3600, horizon_minutes=60)
|
||||
self.assertFalse(out['proposal'])
|
||||
self.assertEqual(len(out['plan_15min']), 4)
|
||||
|
||||
def test_optimize_cold_room_prefers_heat(self):
|
||||
zones = {'living': 19.5, 'bedroom': 19.9}
|
||||
learners = {'living': m.Learner(), 'bedroom': m.Learner()}
|
||||
out = m.optimize(zones, 4, 0, 22, {'living': 2, 'bedroom': 1},
|
||||
learners, False, 3600, horizon_minutes=60)
|
||||
self.assertTrue(out['proposal'])
|
||||
|
||||
def test_minimum_duration_on_and_off(self):
|
||||
learners = {'r': m.Learner()}
|
||||
# Could not switch OFF during first 15 minutes if just turned ON.
|
||||
out = m.optimize({'r': 24}, 12, 1, 22, {'r': 1}, learners, True, 120,
|
||||
minimum_on_minutes=15, horizon_minutes=60)
|
||||
self.assertTrue(out['proposal'])
|
||||
# Could not switch ON during first 10 minutes if just turned OFF.
|
||||
out = m.optimize({'r': 18}, 0, 0, 22, {'r': 1}, learners, False, 120,
|
||||
minimum_off_minutes=10, horizon_minutes=60)
|
||||
self.assertFalse(out['proposal'])
|
||||
|
||||
def test_invalid_room_weights_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
m.optimize({'r': 20}, 10, 0, 22, {'r': 0}, {'r': m.Learner()},
|
||||
False, 1000, horizon_minutes=60)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user