-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniGreenhouse.py
1393 lines (1137 loc) · 73.2 KB
/
MiniGreenhouse.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Mini-greenhouse environment with calibration model (Deep Neural Networks algorithm and physics based model (the GreenLight model)).
In this class we will combine the DNN model with the GreenLight model.
Author: Efraim Manurung
MSc Thesis in Information Technology Group, Wageningen University
efraim.manurung@gmail.com
Table - General structure of the attributes, the predicted state variables of DNN x(t) and GreenLight x'(t),
real measurement from the sensors ŷ(t), combined predicted measurement from LSTM \(\hat{y}(t)\), actuators control signal
u(t) and disturbance d(t).
-------------------------------------------------------------------------------------------------------------------
x1(t), x'1(t) Indoor CO2 [ppm] y1(t) Indoor CO2 [ppm] ŷ1(t) Indoor CO2 [ppm]
x2(t), x'2(t) Indoor temperature [°C] y2(t) Indoor temperature [°C] ŷ2(t) Indoor temperature [°C]
x3(t), x'3(t) Indoor humidity [%] y3(t) Indoor humidity [%] ŷ3(t) Indoor humidity [%]
x4(t), x'4(t) PAR Inside [W/m²] y4(t) PAR Inside [W/m²] ŷ4(t) PAR Inside [W/m²]
x7(t), x'7(t) Leaf Temperature [°C] y5(t) Leaf Temperature [°C] ŷ5(t) Leaf Temperature [°C]
-------------------------------------------------------------------------------------------------------------------
d1(t) Outside Radiation [W/m²] u1(t) Fan [0/1] (1 is on)
d2(t) Outdoor CO2 [ppm] u2(t) Toplighting status [0/1] (1 is on)
d3(t) Outdoor temperature [°C] u3(t) Heating [0/1] (1 is on)
d4(t) Outdoor humidity [%]
-------------------------------------------------------------------------------------------------------------------
based on Table above, we want to predict the state variable x(t) with control signal u(t) and disturbance d(t)
Project sources:
- https://github.com/davkat1/GreenLight
-
Other sources:
-
-
References:
- David Katzin, Simon van Mourik, Frank Kempkes, and Eldert J. Van Henten. 2020. “GreenLight - An Open Source Model for
Greenhouses with Supplemental Lighting: Evaluation of Heat Requirements under LED and HPS Lamps.”
Biosystems Engineering 194: 61–81. https://doi.org/10.1016/j.biosystemseng.2020.03.010
- Literature used:
[1] Vanthoor, B., Stanghellini, C., van Henten, E. J. & de Visser, P. H. B.
A methodology for model-based greenhouse design: Part 1, a greenhouse climate
model for a broad range of designs and climates. Biosyst. Eng. 110, 363-377 (2011).
[2] Vanthoor, B., de Visser, P. H. B., Stanghellini, C. & van Henten, E. J.
A methodology for model-based greenhouse design: Part 2, description and
validation of a tomato yield model. Biosyst. Eng. 110, 378-395 (2011).
[3] Vanthoor, B. A model based greenhouse design method. (Wageningen University, 2011).
-
'''
# IMPORT LIBRARIES for DRL model
# Import Farama foundation's gymnasium
import gymnasium as gym
from gymnasium.spaces import Box
# Import supporting libraries
import numpy as np
import scipy.io as sio
import os
import pandas as pd
# IMPORT LIBRARIES for the matlab file
import matlab.engine
# Import service functions
from utils.ServiceFunctions import ServiceFunctions
# IMPORT LIBRARIES for DNN and LSTM models
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Layer
import joblib
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
# Suppress specific TensorFlow warnings
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="tensorflow")
# Set TensorFlow logging level to ERROR
tf.get_logger().setLevel('ERROR')
# For the tf.data.Dataset only supports Python-style environment
tf.compat.v1.enable_eager_execution()
class MiniGreenhouse(gym.Env):
'''
Calibrator model that combine a DNN model and physics based model.
Link the Python code to matlab program with related methods. We can link it with the .mat file.
'''
def __init__(self, env_config):
'''
Initialize the environment.
Parameters:
env_config(dict): Configuration dictionary for the environment.
'''
print("Initialized MiniGreenhouse Environment!")
# Initialize if the main program for training or running
self.flag_run = env_config.get("flag_run", True) # The simulation is for running (other option is False for training)
self.first_day_gl = env_config.get("first_day_gl", 1) # The first day of the simulation
self.first_day_dnn = env_config.get("first_day_dnn", 0) # 1 / 72 in matlab is 1 step in this DNN model, 20 minutes
# Define the season length parameter
self.season_length_gl = env_config.get("season_length_gl", 1 / 72) # 1 / 72 * 24 [hours] * 60 [minutes / hours] = 20 minutes
self.season_length_dnn = self.first_day_dnn # so we can substract the timesteps, that is why we used the season_length_dnn from the first_day
self.online_measurements = env_config.get("online_measurements", False) # Use online measurements or not from the real-time measurements from IoT system
self.action_from_drl = env_config.get("action_from_drl", False) # Default is false, and we will use the action from offline datasets
self.flag_run_dnn = env_config.get("flag_run_dnn", False) # Default is false, flag to run the Neural Networks model
self.flag_run_gl = env_config.get("flag_run_gl", True) # Default is true, flag to run the green light model
self.flag_run_combined_models = env_config.get("flag_run_combined_models", True) # Default is true, flag to run the LSTM model
is_mature = env_config.get("is_mature", False) # The crops are mature or not
# Convert Python boolean to integer (1 for True, 0 for False)
self.is_mature_matlab = int(is_mature)
# Initiate and max steps
self.max_steps = env_config.get("max_steps", 3) # One episode = 3 steps = 1 hour, because 1 step = 20 minutes
# Start MATLAB engine
self.eng = matlab.engine.start_matlab()
# Path to MATLAB script
if self.flag_run_gl == True:
self.matlab_script_path = r'matlab\DrlGlEnvironment.m'
# No matter if the flag_run_dnn True or not we still need to load the files for the offline training
# Load the datasets from separate files for the DNN model
if is_mature == True and self.flag_run == True:
print("IS MATURE - TRUE, USING MATURE CROPS DATASETS")
# file_path = r"matlab\Mini Greenhouse\september-iot-datasets-test-mature-crops.xlsx"
file_path = r"matlab\Mini Greenhouse\october-iot-datasets-test-mature-crops.xlsx"
else:
if is_mature == False and self.flag_run == False:
print("IS MATURE - FALSE, FLAG RUN - FALSE, USING IOT DATASETS TO TRAIN DRL MODEL")
file_path = r"matlab\Mini Greenhouse\iot-datasets-train-drl.xlsx"
elif is_mature == False and self.flag_run == True:
print("IS MATURE - FALSE, FLAG RUN - TRUE, USING SMALL CROPS DATASETS")
# file_path = r"matlab\Mini Greenhouse\june-iot-datasets-test-small-crops.xlsx"
file_path = r"matlab\Mini Greenhouse\august-iot-datasets-test-small-crops.xlsx"
# Load the dataset
self.mgh_data = pd.read_excel(file_path)
# Initialize lists to store control values
self.ventilation_list = []
self.toplights_list = []
self.heater_list = []
# Initialize a list to store rewards
self.rewards_list = []
# Initialize reward
reward = 0
# Record the reward for the first time
self.rewards_list.extend([reward] * 4)
# Initialize ServiceFunctions
self.service_functions = ServiceFunctions()
# Load the updated data from the excel or from mqtt
self.load_excel_or_mqtt_data(None)
# Check if MATLAB script exists
if os.path.isfile(self.matlab_script_path):
# Initialize lists to store control values and initialize control variables to zero
self.init_controls()
# Call the MATLAB function
if self.online_measurements == True:
# Run the script with the updated outdoor measurements for the first time
self.run_matlab_script('outdoor-indoor.mat', None, None)
else:
# Run the script with empty parameter
self.run_matlab_script()
else:
print(f"MATLAB script not found: {self.matlab_script_path}")
if self.flag_run_gl == True:
#Predict from the GL model
self.predicted_inside_measurements_gl()
if self.flag_run_dnn == True:
# Predict from the DNN model
self.predicted_inside_measurements_dnn()
self.season_length_dnn += 4
# Combine the predicted results from the GL and DNN models
time_steps_formatted= list(range(0, int(self.season_length_dnn - self.first_day_dnn)))
self.format_time_steps(time_steps_formatted)
if self.flag_run_combined_models == True:
# print("time_steps_formatted 2 : ", time_steps_formatted)
self.predicted_combined_models(time_steps_formatted)
# Define the observation and action space
self.define_spaces()
# Initialize the state
self.reset()
def r2_score_metric(self, y_true, y_pred):
'''
Custom R2 score metric
Parameters:
y_true: tf.Tensor - Ground truth values.
y_pred: tf.Tensor - Predicted values.
Returns:
float: R2 score metric
'''
SS_res = tf.reduce_sum(tf.square(y_true - y_pred))
SS_tot = tf.reduce_sum(tf.square(y_true - tf.reduce_mean(y_true)))
return (1 - SS_res/(SS_tot + tf.keras.backend.epsilon()))
def define_spaces(self):
'''
Define the observation and action spaces.
Based on the observation of the mini-greenhouse system Explanation
- co2_in: CO2 inside the mini-greenhouse [ppm]
- temp_in: Temperature inside the mini-greenhouse [°C]
- rh_in: Relative humidity in percentage in the mini-greenhouse [%]
- PAR_in: Global radiation inside the mini-greenhouse [W m^{-2}]
- fruit_dw: Carbohydrates in fruit dry weight [mg{CH2O} m^{-2}] Equation 2, 3 [2], Equation A44 [5]
- fruit_tcansum: Crop development stage [°C day] Equation 8 [2]
- leaf_temp: Crop temperature [°C]
Not included:
- fruit_leaf: Carbohydrates in leaves [mg{CH2O} m^{-2}] Equation 4, 5 [2]
- fruit_stem: Carbohydrates in stem [mg{CH2O} m^{-2}] Equation 6, 7 [2]
- fruit_cbuf: Carbohydrates in buffer [mg{CH2O} m^{-2}] Equation 1, 2 [2]
'''
# Define observation and action spaces
self.observation_space = Box(
low=np.array([0.0, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00]), #in order: co2_in, temp_in, rh_in, PAR_in, fruit_dw, fruit_tcansum and leaf_temp
high=np.array([np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf]),
dtype=np.float64
)
self.action_space = Box(
low=np.array([0, 0, 0], dtype=np.float32),
high=np.array([1, 1, 1], dtype=np.float32),
dtype=np.float32
)
def init_controls(self):
'''
Initialize control variables.
'''
# Initialize for the first time
time_steps = np.linspace(300, 1200, 4) # 20 minutes (1200 seconds)
self.controls = {
'time': time_steps.reshape(-1, 1),
'ventilation': np.zeros(4).reshape(-1, 1),
'toplights': np.zeros(4).reshape(-1, 1),
'heater': np.zeros(4).reshape(-1, 1)
}
# Append only the latest 3 values from each control variable
self.ventilation_list.extend(self.controls['ventilation'].flatten()[-4:])
self.toplights_list.extend(self.controls['toplights'].flatten()[-4:])
self.heater_list.extend(self.controls['heater'].flatten()[-4:])
sio.savemat('controls.mat', self.controls)
def run_matlab_script(self, outdoor_file = None, indoor_file=None, fruit_file=None):
'''
Run the MATLAB script.
'''
# Check if the outdoor_file or indoor_file or fruit_file is None
if indoor_file is None:
indoor_file = []
if fruit_file is None:
fruit_file = []
if outdoor_file is None:
outdoor_file = []
self.eng.DrlGlEnvironment(self.season_length_gl, self.first_day_gl, 'controls.mat', outdoor_file, indoor_file, fruit_file, self.is_mature_matlab, nargout=0)
def load_excel_or_mqtt_data(self, _action_drl):
'''
Load data from .xlsx file or mqtt data and store in instance variables.
The data is appended to existing variables if they already exist.
'''
if self.online_measurements == True:
print("LOAD DATA FROM ONLINE MEASUREMENTS")
# Initialize outdoor measurements, to get the outdoor measurements
outdoor_indoor_measurements = self.service_functions.get_outdoor_indoor_measurements(broker="192.168.1.56", port=1883, topic="greenhouse-iot-system/outdoor-indoor-measurements")
# Convert outdoor_indoor_measurements to a DataFrame
self.step_data = pd.DataFrame({
'time': outdoor_indoor_measurements['time'].flatten(),
'global out': outdoor_indoor_measurements['par_out'].flatten(),
'global in': outdoor_indoor_measurements['par_in'].flatten(),
'temp out': outdoor_indoor_measurements['temp_out'].flatten(),
'temp in': outdoor_indoor_measurements['temp_in'].flatten(),
'rh out': outdoor_indoor_measurements['hum_out'].flatten(),
'rh in': outdoor_indoor_measurements['hum_in'].flatten(),
'co2 out': outdoor_indoor_measurements['co2_out'].flatten(),
'co2 in': outdoor_indoor_measurements['co2_in'].flatten(),
'leaf temp': outdoor_indoor_measurements['leaf_temp'].flatten()
})
# Add empty columns for toplights, ventilation, and heater
self.step_data['toplights'] = np.nan # or None
self.step_data['ventilation'] = np.nan # or None
self.step_data['heater'] = np.nan # or None
# Map the outdoor measurements to the corresponding variables
new_time_excel_mqtt = outdoor_indoor_measurements['time'].flatten()
new_global_out_excel_mqtt = outdoor_indoor_measurements['par_out'].flatten()
new_global_in_excel_mqtt = outdoor_indoor_measurements['par_in'].flatten()
new_temp_out_excel_mqtt = outdoor_indoor_measurements['temp_out'].flatten()
new_temp_in_excel_mqtt = outdoor_indoor_measurements['temp_in'].flatten()
new_rh_out_excel_mqtt = outdoor_indoor_measurements['hum_out'].flatten()
new_rh_in_excel_mqtt = outdoor_indoor_measurements['hum_in'].flatten()
new_co2_out_excel_mqtt = outdoor_indoor_measurements['co2_out'].flatten()
new_co2_in_excel_mqtt = outdoor_indoor_measurements['co2_in'].flatten()
new_leaf_temp_excel_mqtt = outdoor_indoor_measurements['leaf_temp'].flatten()
if self.action_from_drl == True and _action_drl is not None:
# Use the actions from the DRL model and convert actions to discrete values
ventilation = 1 if _action_drl[0] >= 0.5 else 0
toplights = 1 if _action_drl[1] >= 0.5 else 0
heater = 1 if _action_drl[2] >= 0.5 else 0
ventilation = np.full(4, ventilation)
toplights = np.full(4, toplights)
heater = np.full(4, heater)
# Update the step_data with the DRL model's actions using .loc
self.step_data.loc[:, 'toplights'] = toplights
self.step_data.loc[:, 'ventilation'] = ventilation
self.step_data.loc[:, 'heater'] = heater
# Add new data
new_toplights = self.step_data['toplights'].values
new_ventilation = self.step_data['ventilation'].values
new_heater = self.step_data['heater'].values
else:
# Handle if the _action_drl is None for the first time
ventilation = np.full(4, 0)
toplights = np.full(4, 0)
heater = np.full(4, 0)
# Update the step_data with the DRL model's actions using .loc
self.step_data.loc[:, 'toplights'] = toplights
self.step_data.loc[:, 'ventilation'] = ventilation
self.step_data.loc[:, 'heater'] = heater
# Use the actions from the offline dataset and add new data
new_toplights = self.step_data['toplights'].values
new_ventilation = self.step_data['ventilation'].values
new_heater = self.step_data['heater'].values
# Check if instance variables already exist; if not, initialize them
if not hasattr(self, 'time_excel_mqtt'):
self.time_excel_mqtt = new_time_excel_mqtt
self.global_out_excel_mqtt = new_global_out_excel_mqtt
self.global_in_excel_mqtt = new_global_in_excel_mqtt
self.temp_in_excel_mqtt = new_temp_in_excel_mqtt
self.temp_out_excel_mqtt = new_temp_out_excel_mqtt
self.rh_in_excel_mqtt = new_rh_in_excel_mqtt
self.rh_out_excel_mqtt = new_rh_out_excel_mqtt
self.co2_in_excel_mqtt = new_co2_in_excel_mqtt
self.co2_out_excel_mqtt = new_co2_out_excel_mqtt
self.leaf_temp_excel_mqtt = new_leaf_temp_excel_mqtt
self.toplights = new_toplights
self.ventilation = new_ventilation
self.heater = new_heater
else:
# Concatenate new data with existing data
self.time_excel_mqtt = np.concatenate((self.time_excel_mqtt, new_time_excel_mqtt))
self.global_out_excel_mqtt = np.concatenate((self.global_out_excel_mqtt, new_global_out_excel_mqtt))
self.global_in_excel_mqtt = np.concatenate((self.global_in_excel_mqtt, new_global_in_excel_mqtt))
self.temp_in_excel_mqtt = np.concatenate((self.temp_in_excel_mqtt, new_temp_in_excel_mqtt))
self.temp_out_excel_mqtt = np.concatenate((self.temp_out_excel_mqtt, new_temp_out_excel_mqtt))
self.rh_in_excel_mqtt = np.concatenate((self.rh_in_excel_mqtt, new_rh_in_excel_mqtt))
self.rh_out_excel_mqtt = np.concatenate((self.rh_out_excel_mqtt, new_rh_out_excel_mqtt))
self.co2_in_excel_mqtt = np.concatenate((self.co2_in_excel_mqtt, new_co2_in_excel_mqtt))
self.co2_out_excel_mqtt = np.concatenate((self.co2_out_excel_mqtt, new_co2_out_excel_mqtt))
self.leaf_temp_excel_mqtt = np.concatenate((self.leaf_temp_excel_mqtt, new_leaf_temp_excel_mqtt))
self.toplights = np.concatenate((self.toplights, new_toplights))
self.ventilation = np.concatenate((self.ventilation, new_ventilation))
self.heater = np.concatenate((self.heater, new_heater))
# Optionally: Check or print the step_data structure to ensure it's correct
print("Step Data (online):", self.step_data.head())
elif self.online_measurements == False:
# Use offline dataset
print("LOAD DATA FROM OFFLINE DATASETS")
# Slice the dataframe to get the rows for the current step
self.step_data = self.mgh_data.iloc[self.season_length_dnn:self.season_length_dnn + 4]
# Extract the required columns and flatten them
new_time_excel_mqtt = self.step_data['time'].values
new_global_out_excel_mqtt = self.step_data['global out'].values
new_global_in_excel_mqtt = self.step_data['global in'].values
new_temp_in_excel_mqtt = self.step_data['temp in'].values
new_temp_out_excel_mqtt = self.step_data['temp out'].values
new_rh_in_excel_mqtt = self.step_data['rh in'].values
new_rh_out_excel_mqtt = self.step_data['rh out'].values
new_co2_in_excel_mqtt = self.step_data['co2 in'].values
new_co2_out_excel_mqtt = self.step_data['co2 out'].values
new_leaf_temp_excel_mqtt = self.step_data['leaf temp'].values
if self.action_from_drl == True and _action_drl is not None:
# Use the actions from the DRL model
# Convert actions to discrete values
ventilation = 1 if _action_drl[0] >= 0.5 else 0
toplights = 1 if _action_drl[1] >= 0.5 else 0
heater = 1 if _action_drl[2] >= 0.5 else 0
ventilation = np.full(4, ventilation)
toplights = np.full(4, toplights)
heater = np.full(4, heater)
# Update the step_data with the DRL model's actions using .loc
self.step_data.loc[:, 'toplights'] = toplights
self.step_data.loc[:, 'ventilation'] = ventilation
self.step_data.loc[:, 'heater'] = heater
# Add new data
new_toplights = self.step_data['toplights'].values
new_ventilation = self.step_data['ventilation'].values
new_heater = self.step_data['heater'].values
else:
# Use the actions from the offline dataset and add new data
new_toplights = self.step_data['toplights'].values
new_ventilation = self.step_data['ventilation'].values
new_heater = self.step_data['heater'].values
# Check if instance variables already exist; if not, initialize them
if not hasattr(self, 'time_excel_mqtt'):
self.time_excel_mqtt = new_time_excel_mqtt
self.global_out_excel_mqtt = new_global_out_excel_mqtt
self.global_in_excel_mqtt = new_global_in_excel_mqtt
self.temp_in_excel_mqtt = new_temp_in_excel_mqtt
self.temp_out_excel_mqtt = new_temp_out_excel_mqtt
self.rh_in_excel_mqtt = new_rh_in_excel_mqtt
self.rh_out_excel_mqtt = new_rh_out_excel_mqtt
self.co2_in_excel_mqtt = new_co2_in_excel_mqtt
self.co2_out_excel_mqtt = new_co2_out_excel_mqtt
self.leaf_temp_excel_mqtt = new_leaf_temp_excel_mqtt
self.toplights = new_toplights
self.ventilation = new_ventilation
self.heater = new_heater
else:
# Concatenate new data with existing data
self.time_excel_mqtt = np.concatenate((self.time_excel_mqtt, new_time_excel_mqtt))
self.global_out_excel_mqtt = np.concatenate((self.global_out_excel_mqtt, new_global_out_excel_mqtt))
self.global_in_excel_mqtt = np.concatenate((self.global_in_excel_mqtt, new_global_in_excel_mqtt))
self.temp_in_excel_mqtt = np.concatenate((self.temp_in_excel_mqtt, new_temp_in_excel_mqtt))
self.temp_out_excel_mqtt = np.concatenate((self.temp_out_excel_mqtt, new_temp_out_excel_mqtt))
self.rh_in_excel_mqtt = np.concatenate((self.rh_in_excel_mqtt, new_rh_in_excel_mqtt))
self.rh_out_excel_mqtt = np.concatenate((self.rh_out_excel_mqtt, new_rh_out_excel_mqtt))
self.co2_in_excel_mqtt = np.concatenate((self.co2_in_excel_mqtt, new_co2_in_excel_mqtt))
self.co2_out_excel_mqtt = np.concatenate((self.co2_out_excel_mqtt, new_co2_out_excel_mqtt))
self.leaf_temp_excel_mqtt = np.concatenate((self.leaf_temp_excel_mqtt, new_leaf_temp_excel_mqtt))
self.toplights = np.concatenate((self.toplights, new_toplights))
self.ventilation = np.concatenate((self.ventilation, new_ventilation))
self.heater = np.concatenate((self.heater, new_heater))
# Debugging
print("Step Data (offline):", self.step_data.head())
# @tf.function
def predict_inside_measurements_dnn(self, target_variable, data_input):
'''
Predict the measurements or state variables inside mini-greenhouse
Parameters:
target_variable: str - The target variable to predict.
data_input: dict or pd.DataFrame - The input features for the prediction.
Features (inputs):
Outside measurements information
- time
- global out
- temp out
- rh out
- co2 out
Control(s) input
- ventilation
- toplights
- heater
Return:
np.array: predicted measurements inside mini-greenhouse
'''
if isinstance(data_input, dict):
data_input = pd.DataFrame(data_input)
# Need to be fixed
features = ['time', 'global out', 'temp out', 'rh out', 'co2 out', 'ventilation', 'toplights', 'heater']
# Ensure the data_input has the required features
for feature in features:
if feature not in data_input.columns:
raise ValueError(f"Missing feature '{feature}' in the input data.")
X_features = data_input[features]
# Load the model and scaler with error handling
try:
loaded_model = load_model(f'trained-dnn-models/{target_variable}_model.keras', custom_objects={'r2_score_metric': self.r2_score_metric})
except Exception as e:
raise ValueError(f"Failed to load the model: {e}")
try:
scaler = joblib.load(f'trained-dnn-models/{target_variable}_scaler.pkl')
except Exception as e:
raise ValueError(f"Failed to load the scaler: {e}")
# Scale the input features
X_features_scaled = scaler.transform(X_features)
# Predict the measurements
y_hat_measurements = loaded_model.predict(X_features_scaled)
# Return the predicted measurements inside the mini-greenhouse
return y_hat_measurements
def predicted_inside_measurements_dnn(self):
'''
Predicted inside measurements
'''
# Predict the inside measurements (the state variable inside the mini-greenhouse)
new_par_in_predicted_dnn = self.predict_inside_measurements_dnn('global in', self.step_data)
new_temp_in_predicted_dnn = self.predict_inside_measurements_dnn('temp in', self.step_data)
new_rh_in_predicted_dnn = self.predict_inside_measurements_dnn('rh in', self.step_data)
new_co2_in_predicted_dnn = self.predict_inside_measurements_dnn('co2 in', self.step_data)
new_leaf_temp_predicted_dnn = self.predict_inside_measurements_dnn('leaf temp', self.step_data)
# Check if instance variables already exist; if not, initialize them
if not hasattr(self, 'temp_in_predicted_dnn'):
self.par_in_predicted_dnn = new_par_in_predicted_dnn
self.temp_in_predicted_dnn = new_temp_in_predicted_dnn
self.rh_in_predicted_dnn = new_rh_in_predicted_dnn
self.co2_in_predicted_dnn = new_co2_in_predicted_dnn
self.leaf_temp_predicted_dnn = new_leaf_temp_predicted_dnn
else:
# Concatenate new data with existing data
self.par_in_predicted_dnn = np.concatenate((self.par_in_predicted_dnn, new_par_in_predicted_dnn))
self.temp_in_predicted_dnn = np.concatenate((self.temp_in_predicted_dnn, new_temp_in_predicted_dnn))
self.rh_in_predicted_dnn = np.concatenate((self.rh_in_predicted_dnn, new_rh_in_predicted_dnn))
self.co2_in_predicted_dnn = np.concatenate((self.co2_in_predicted_dnn, new_co2_in_predicted_dnn))
self.leaf_temp_predicted_dnn = np.concatenate((self.leaf_temp_predicted_dnn, new_leaf_temp_predicted_dnn))
def predicted_inside_measurements_gl(self):
'''
Load data from the .mat file.
From matlab, the structure is:
% Save the extracted data to a .mat file
save('drl-env.mat', 'time', 'temp_in', 'rh_in', 'co2_in', 'PAR_in', 'fruit_leaf', 'fruit_stem', 'fruit_dw', 'fruit_tcansum', 'leaf_temp');
'''
# Read the drl-env mat from the initialization
# Read the 3 values and append it
# get the prediction from the matlab results
data = sio.loadmat("drl-env.mat")
new_time_gl = data['time'].flatten()[-4:]
new_co2_in_predicted_gl = data['co2_in'].flatten()[-4:]
new_temp_in_predicted_gl = data['temp_in'].flatten()[-4:]
new_rh_in_predicted_gl = data['rh_in'].flatten()[-4:]
new_par_in_predicted_gl = data['PAR_in'].flatten()[-4:]
new_fruit_leaf_predicted_gl = data['fruit_leaf'].flatten()[-4:]
new_fruit_stem_predicted_gl = data['fruit_stem'].flatten()[-4:]
new_fruit_dw_predicted_gl = data['fruit_dw'].flatten()[-4:]
new_fruit_cbuf_predicted_gl = data['fruit_cbuf'].flatten()[-4:]
new_fruit_tcansum_predicted_gl = data['fruit_tcansum'].flatten()[-4:]
new_leaf_temp_predicted_gl = data['leaf_temp'].flatten()[-4:]
if not hasattr(self, 'time_gl'):
self.time_gl = new_time_gl
self.co2_in_predicted_gl = new_co2_in_predicted_gl
self.temp_in_predicted_gl = new_temp_in_predicted_gl
self.rh_in_predicted_gl = new_rh_in_predicted_gl
self.par_in_predicted_gl = new_par_in_predicted_gl
self.fruit_leaf_predicted_gl = new_fruit_leaf_predicted_gl
self.fruit_stem_predicted_gl = new_fruit_stem_predicted_gl
self.fruit_dw_predicted_gl = new_fruit_dw_predicted_gl
self.fruit_cbuf_predicted_gl = new_fruit_cbuf_predicted_gl
self.fruit_tcansum_predicted_gl = new_fruit_tcansum_predicted_gl
self.leaf_temp_predicted_gl = new_leaf_temp_predicted_gl
else:
self.time_gl = np.concatenate((self.time_gl, new_time_gl))
self.co2_in_predicted_gl = np.concatenate((self.co2_in_predicted_gl, new_co2_in_predicted_gl))
self.temp_in_predicted_gl = np.concatenate((self.temp_in_predicted_gl, new_temp_in_predicted_gl))
self.rh_in_predicted_gl= np.concatenate((self.rh_in_predicted_gl, new_rh_in_predicted_gl))
self.par_in_predicted_gl = np.concatenate((self.par_in_predicted_gl, new_par_in_predicted_gl))
self.fruit_leaf_predicted_gl = np.concatenate((self.fruit_leaf_predicted_gl, new_fruit_leaf_predicted_gl))
self.fruit_stem_predicted_gl = np.concatenate((self.fruit_stem_predicted_gl, new_fruit_stem_predicted_gl))
self.fruit_dw_predicted_gl = np.concatenate((self.fruit_dw_predicted_gl, new_fruit_dw_predicted_gl))
self.fruit_cbuf_predicted_gl = np.concatenate((self.fruit_cbuf_predicted_gl, new_fruit_cbuf_predicted_gl))
self.fruit_tcansum_predicted_gl = np.concatenate((self.fruit_tcansum_predicted_gl, new_fruit_tcansum_predicted_gl))
self.leaf_temp_predicted_gl = np.concatenate((self.leaf_temp_predicted_gl, new_leaf_temp_predicted_gl))
def reset(self, *, seed=None, options=None):
'''
Reset the environment to the initial state.
Returns:
int: The initial observation of the environment.
'''
self.current_step = 0
# self.season_length_dnn += 4 # moved it to the intialized class
return self.observation(), {}
def observation(self):
'''
Get the observation of the environment for every state.
Returns:
array: The observation space of the environment.
'''
if self.flag_run_combined_models == True:
# print the predict measurements using the LSTM model
print("PRINT THE OBSERVATION BASED ON THE COMBINED MODELS")
print("self.co2_in_predicted_combined_models :", self.co2_in_predicted_combined_models[-1])
print("self.temp_in_predicted_combined_models : ", self.temp_in_predicted_combined_models[-1])
print("self.rh_in_predicted_combined_models : ", self.rh_in_predicted_combined_models[-1])
print("self.par_in_predicted_combined_models : ", self.par_in_predicted_combined_models[-1])
print("self.leaf_temp_predicted_combined_models : ", self.leaf_temp_predicted_combined_models[-1] )
#in order: co2_in, temp_in, rh_in, PAR_in, fruit_dw, fruit_tcansum and leaf_temp
return np.array([
self.co2_in_predicted_combined_models[-1], # use combined models for the observation
self.temp_in_predicted_combined_models[-1], # use combined models for the observation
self.rh_in_predicted_combined_models[-1], # use combined models for the observation
self.par_in_predicted_combined_models[-1], # use combined models for the observation
self.fruit_dw_predicted_gl[-1], # use the predicted from the GL
self.fruit_tcansum_predicted_gl[-1], # use the predicted from the GL
self.leaf_temp_predicted_combined_models[-1] # use combined models for the observation
], np.float32)
else:
#in order: co2_in, temp_in, rh_in, PAR_in, fruit_dw, fruit_tcansum and leaf_temp
return np.array([
self.co2_in_predicted_gl[-1],
self.temp_in_predicted_gl[-1],
self.rh_in_predicted_gl[-1],
self.par_in_predicted_gl[-1],
self.fruit_dw_predicted_gl[-1],
self.fruit_tcansum_predicted_gl[-1],
self.leaf_temp_predicted_gl[-1]
], np.float32)
def get_reward(self, _ventilation, _toplights, _heater):
'''
Get the reward for the current state.
The reward function defines the immediate reward obtained by the agent for its actions in a given state.
Changed based on the MiniGreenhouse environment from the equation (4) in the source.
Source: Bridging the reality gap: An Adaptive Framework for Autonomous Greenhouse
Control with Deep Reinforcement Learning. George Qi. Wageningen University. 2024.
r(k) = w_r,y1 * Δy1(k) - Σ (from i=1 to 3) w_r,ai * ai(k)
Details
r(k): the imediate reward the agent receives at time step k.
w_r,y1 * Δy1(k): represents the positive reward for the agent due to increased in the fruit dry weight Δy1(k).
Σ (from i=1 to 3) w_r,ai * ai(k): represents the negative reward received by the agent due to cost energy with arbitrary
Obtained coefficient setting for the reward function.
Coefficients Values Details
w_r_y1 1 Fruit dry weight
w_r_a1 0.005 Ventilation
w_r_a2 0.010 Toplights
w_r_a3 0.001 Heater
Returns:
int: the immediate reward the agent receives at time step k in integer.
'''
# Initialize variables, based on the equation above
# Need to be determined to make the r_k unitless
w_r_y1 = 1 # Fruit dry weight
w_r_a1 = 0.005 # Ventilation
w_r_a2 = 0.010 # Toplights
w_r_a3 = 0.001 # Heater
# Give initial reward
if self.current_step == 0:
r_k = 0.0
return r_k # No reward for the initial state
# In the createCropModel.m in the GreenLight model (mini-greenhouse-model)
# cFruit or dry weight of fruit is the carbohydrates in fruit, so it is the best variable to count for the reward
# Calculate the change in fruit dry weight
delta_fruit_dw = (self.fruit_dw_predicted_gl[-1] - self.fruit_dw_predicted_gl[-2])
print("delta_fruit_dw: ", delta_fruit_dw)
r_k = w_r_y1 * delta_fruit_dw - ((w_r_a1 * _ventilation) + (w_r_a2 * _toplights) + (w_r_a3 * _heater))
print("r_k immediate reward: ", r_k)
return r_k
def delete_files(self):
'''
delete matlab files after simulation to make it clear.
'''
os.remove('controls.mat') # controls file
os.remove('drl-env.mat') # simulation file
os.remove('indoor.mat') # indoor measurements
os.remove('fruit.mat') # fruit growth
if self.online_measurements == True:
os.remove('outdoor-indoor.mat') # outdoor measurements
def done(self):
'''
Check if the episode is done.
Returns:
bool: True if the episode is done, otherwise False.
'''
# Episode is done if we have reached the target
# We print all the physical parameters and controls
if self.flag_run == True:
# Terminated when current step is same value with max_steps
# In one episode, for example if the max_step = 4, that mean 1 episode is for 1 hour in real-time (real-measurements)
if self.current_step >= self.max_steps:
# Print and save all data
if self.online_measurements == True:
self.print_and_save_all_data('output/output_simulated_data_online.xlsx')
else:
self.print_and_save_all_data('output/output_simulated_data_offline.xlsx')
# Delete all files
self.delete_files()
return True
else:
if self.current_step >= self.max_steps:
return True
return False
def step(self, _action_drl):
'''
Take an action in the environment.
Parameters:
Based on the u(t) controls
action (discrete integer):
- u1(t) Ventilation (-) 0-1 (1 is fully open)
- u2(t) Toplights (-) 0/1 (1 is on)
- u3(t) Heater (-) 0/1 (1 is on)
Returns:
New observation, reward, terminated-flag (frome done method), truncated-flag, info-dict (empty).
'''
# Increment the current step
self.current_step += 1
print("\n\n----------------------------------------------------------------------------------------")
print("CURRENT STEPS: ", self.current_step)
print("----------------------------------------------------------------------------------------")
if self.online_measurements == False:
# Get the oudoor measurements
self.load_excel_or_mqtt_data(_action_drl)
# Get the actions from the excel or drl from the load_excel_or_mqtt_data, for online or offline datasets
time_steps = np.linspace(300, 1200, 4) # Time steps in seconds
ventilation = self.ventilation[-4:]
toplights = self.toplights[-4:]
heater = self.heater[-4:]
# Keep only the latest 3 data points before appending
# Append controls to the lists
self.ventilation_list.extend(ventilation[-4:])
self.toplights_list.extend(toplights[-4:])
self.heater_list.extend(heater[-4:])
# Get the action from the offline datasets
print("ACTION SIGNAL u(t)")
print("ventilation: ", ventilation)
print("toplights: ", toplights)
print("heater: ", heater)
# Only publish MQTT data for the Raspberry Pi when running not training
if self.online_measurements == True:
# Get the action from the DRL model
# print("RAW ACTION: ", _action_drl)
# Convert actions to discrete values
ventilation = 1 if _action_drl[0] >= 0.5 else 0
toplights = 1 if _action_drl[1] >= 0.5 else 0
heater = 1 if _action_drl[2] >= 0.5 else 0
print("ACTION SIGNAL u(t)")
print("ventilation: ", ventilation)
print("toplights: ", toplights)
print("heater: ", heater)
time_steps = np.linspace(300, 1200, 4) # Time steps in seconds
ventilation = np.full(4, ventilation)
toplights = np.full(4, toplights)
heater = np.full(4, heater)
# Format data controls in JSON format
json_data = self.service_functions.format_data_in_JSON(time_steps, \
ventilation, toplights, \
heater)
# Publish controls to the raspberry pi (IoT system client)
self.service_functions.publish_mqtt_data(json_data, broker="192.168.1.56", port=1883, topic="greenhouse-iot-system/drl-controls")
# Create control dictionary
controls = {
'time': time_steps.reshape(-1, 1),
'ventilation': ventilation.reshape(-1, 1),
'toplights': toplights.reshape(-1, 1),
'heater': heater.reshape(-1, 1)
}
# Save control variables to .mat file
sio.savemat('controls.mat', controls)
# Update the season_length and first_day for the GL model
# 1 / 72 is 20 minutes in 24 hours, the calculation look like this
# 1 / 72 * 24 [hours] * 60 [minutes . hours ^ -1] = 20 minutes
self.season_length_gl = 1 / 72
self.first_day_gl += 1 / 72
# Update the season_length for the DNN model
self.season_length_dnn += 4
if self.flag_run_gl == True:
# print("USE INDOOR GREENLIGHT")
# Use the data from the GreenLight model
# Convert co2_in ppm using service functions
co2_density_gl = self.service_functions.co2ppm_to_dens(self.temp_in_predicted_gl[-4:], self.co2_in_predicted_gl[-4:])
# Convert Relative Humidity (RH) to Pressure in Pa
vapor_density_gl = self.service_functions.rh_to_vapor_density(self.temp_in_predicted_gl[-4:], self.rh_in_predicted_gl[-4:])
vapor_pressure_gl = self.service_functions.vapor_density_to_pressure(self.temp_in_predicted_gl[-4:], vapor_density_gl)
# Update the MATLAB environment with the 3 latest current state
# It will be used to be simulated in the GreenLight model with mini-greenhouse parameters
drl_indoor = {
'time': self.time_gl[-3:].astype(float).reshape(-1, 1),
'temp_in': self.temp_in_predicted_gl[-3:].astype(float).reshape(-1, 1),
'rh_in': vapor_pressure_gl[-3:].astype(float).reshape(-1, 1),
'co2_in': co2_density_gl[-3:].astype(float).reshape(-1, 1)
}
# Save control variables to .mat file
sio.savemat('indoor.mat', drl_indoor)
# Update the fruit growth with the 1 latest current state from the GreenLight model - mini-greenhouse parameters
fruit_growth = {
'time': self.time_gl[-1:].astype(float).reshape(-1, 1),
'fruit_leaf': self.fruit_leaf_predicted_gl[-1:].astype(float).reshape(-1, 1),
'fruit_stem': self.fruit_stem_predicted_gl[-1:].astype(float).reshape(-1, 1),
'fruit_dw': self.fruit_dw_predicted_gl[-1:].astype(float).reshape(-1, 1),
'fruit_cbuf': self.fruit_cbuf_predicted_gl[-1:].astype(float).reshape(-1, 1),
'fruit_tcansum': self.fruit_tcansum_predicted_gl[-1:].astype(float).reshape(-1, 1),
'leaf_temp': self.leaf_temp_predicted_gl[-1:].astype(float).reshape(-1,1)
}
# Save the fruit growth to .mat file
sio.savemat('fruit.mat', fruit_growth)
if self.online_measurements == True:
# Load the updated data from the excel or from mqtt, for online or offline datasets,
# we still need to call the data
# Get the oudoor measurements
self.load_excel_or_mqtt_data(_action_drl)
# Get the actions from the excel or drl from the load_excel_or_mqtt_data, for online or offline datasets
time_steps = np.linspace(300, 1200, 4) # Time steps in seconds
ventilation = self.ventilation[-4:]
toplights = self.toplights[-4:]
heater = self.heater[-4:]
# Keep only the latest 3 data points before appending
# Append controls to the lists
self.ventilation_list.extend(ventilation[-4:])
self.toplights_list.extend(toplights[-4:])
self.heater_list.extend(heater[-4:])
# Run the script with the updated state variables
if self.online_measurements == True:
self.run_matlab_script('outdoor-indoor.mat', 'indoor.mat', 'fruit.mat')
else:
self.run_matlab_script(None, 'indoor.mat', 'fruit.mat')
if self.flag_run_gl == True:
# Load the updated data from predcited from the greenlight model
self.predicted_inside_measurements_gl()
if self.flag_run_dnn == True:
# Call the predicted inside measurements with the DNN model
self.predicted_inside_measurements_dnn()
time_steps_formatted = list(range(0, int(self.season_length_dnn - self.first_day_dnn)))
self.format_time_steps(time_steps_formatted)
if self.flag_run_combined_models == True:
# Combine the predicted results from the GL and DNN models
self.predicted_combined_models(time_steps_formatted)
# Calculate reward
# Remember that the actions become a list, but we only need the first actions from 15 minutes (all of the is the same)
_reward = self.get_reward(ventilation[0], toplights[0], heater[0])
# Record the reward
self.rewards_list.extend([_reward] * 4)
# Truncated flag
truncated = False
return self.observation(), _reward, self.done(), truncated, {}
def print_and_save_all_data(self, file_name):
'''
Print all the appended data and save to an Excel file and plot it.
Parameters:
- file_name: Name of the output Excel file
- ventilation_list: List of action for fan/ventilation from DRL model or offline datasets
- toplights_list: List of action for toplights from DRL model or offline datasets
- heater_list: List of action for heater from DRL model or offline datasets
- reward_list: List of reward for iterated step
- co2_in_excel_mqtt: List of actual CO2 values
- temp_in_excel_mqtt: List of actual temperature values
- rh_in_excel_mqtt: List of actual relative humidity values
- par_in_excel_mqtt: List of actual PAR values
- co2_in_predicted_dnn: List of predicted CO2 values from Neural Network
- temp_in_predicted_dnn: List of predicted temperature values from Neural Network
- rh_in_predicted_dnn: List of predicted relative humidity values from Neural Network
- par_in_predicted_dnn: List of predicted PAR values from Neural Network
- co2_in_predicted_gl: List of predicted CO2 values from Generalized Linear Model
- temp_in_predicted_gl: List of predicted temperature values from Generalized Linear Model
- rh_in_predicted_gl: List of predicted relative humidity values from Generalized Linear Model
- par_in_predicted_gl: List of predicted PAR values from Generalized Linear Model
'''
print("\n\n-------------------------------------------------------------------------------------")
print("PRINT ALL THE APPENDED DATA")
print("-------------------------------------------------------------------------------------")
print(f"Length of Time: {len(self.time_excel_mqtt)}")
print(f"Length of Action Ventilation: {len(self.ventilation_list)}")
print(f"Length of Action Toplights: {len(self.toplights_list)}")
print(f"Length of Action Heater: {len(self.heater_list)}")
print(f"Length of reward: {len(self.rewards_list)}")
print(f"Length of CO2 In (Actual): {len(self.co2_in_excel_mqtt)}")
print(f"Length of Temperature In (Actual): {len(self.temp_in_excel_mqtt)}")
print(f"Length of RH In (Actual): {len(self.rh_in_excel_mqtt)}")
print(f"Length of PAR In (Actual): {len(self.global_in_excel_mqtt)}")
print(f"Length of Predicted CO2 In (DNN): {len(self.co2_in_predicted_dnn)}")
print(f"Length of Predicted Temperature In (DNN): {len(self.temp_in_predicted_dnn)}")