-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
238 lines (159 loc) · 7.32 KB
/
main.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
import sys
from pyhugin92 import *
from DRL_load import standard_tank, standard_reactor, agent_DRL_S1_tank, agent_DRL_S2_pump, agent_DRL_S3_reactor, scaling_factor_tank, scaling_factor_pump, scaling_factor_reactor
import numpy as np
#
# Function Definitions
#
#sdshsfthsgfhstgjbalkjfbgælnhgl
def parse_listener(line, description):
"""A listener for parsing errors."""
print(f"Parse error line {line}: {description}")
def load_model(cc_name, class_name, steps):
"""Load a Bayesian network model."""
cc = ClassCollection()
try:
cc.parse_classes(cc_name, parse_listener)
return Domain(cc.get_class_by_name(class_name), steps)
except HuginException:
print("A Hugin Exception was raised!")
raise
def set_evidence(node, value):
"""Set evidence for a node."""
state_index = node.get_state_index_from_label(value) if isinstance(value, str) else node.get_state_index_from_value(value)
node.select_state(state_index)
def find_least_conflicting_state(domain, fault_node):
"""Determine the least conflicting state for a fault node."""
min_conflict = float('inf')
least_conflicting_state = []
for state in range(fault_node.get_number_of_states()):
set_standard_evidence_conflict(domain)
fault_node.select_state(state)
domain.propagate()
current_conflict = np.round(domain.get_conflict(),2)
domain.initialize()
if current_conflict < min_conflict:
min_conflict = current_conflict
least_conflicting_state = [fault_node.get_name(), fault_node.get_state_label(state)]
return least_conflicting_state
def set_standard_evidence_conflict(domain):
"""Set standard evidence for the domain."""
evidence_values = {
"PressureT1": 101000,
# Here we creat an issue with the nitrogen flow beeing 0.
"Nitrogen_flow":0,
# If auto is 1 it will detect first a control fault. If it 0 will detect valve fault
"Auto":1,
# We select the Primary system
"Systeme": 1
}
for node_name, value in evidence_values.items():
set_evidence(domain.get_node_by_name(node_name), value)
def set_standard_evidence_decision(domain):
"""Set standard evidence for the domain."""
evidence_values = {
"T0.PressureT1": 101000,
"T0.Level": 6.4999,
"T0.Fault": "none",
}
for node_name, value in evidence_values.items():
set_evidence(domain.get_node_by_name(node_name), value)
def find_best_decision(domain, decision_nodes):
"""Find the best decision for each node."""
L_best_decision = []
nitrogen_flow=domain.get_node_by_name("T1.Nitrogen_flow")
for decision_node in decision_nodes:
utility_max = -float('inf')
for state in range(decision_node.get_number_of_states()):
if decision_node.get_name() == "Systeme" and state == 1:
value = 0
state = domain.get_node_by_name("set_point_Nitrogen").get_state_index_from_value(value)
nitrogen_flow.select_state(state)
utility = decision_node.get_expected_utility(state)
if utility >= utility_max:
utility_max = utility
right_state = state
nitrogen_flow.retract_findings()
decision_node.select_state(right_state)
domain.propagate()
L_best_decision.append([decision_node.get_name(),decision_node.get_state_value(right_state)])
return(L_best_decision)
def adjust_decision_for_fault_control(pressure, scaling_factor, agent, transformer, decision, node):
"""
Adjust the decision based on the output of the DRL agent for 'Fault control'.
"""
normalized_pressure = transformer.transform(np.array([pressure / 101350]).reshape(-1, 1))
rl_value = scaling_factor.scale(agent.select_action(normalized_pressure))[0]
value = float(decision[1])
index = node.get_state_index_from_value(value)
uper_interval = node.get_state_value(index + 1)
if rl_value < uper_interval and value < rl_value:
decision[1] = rl_value
return decision
def adjust_decision_for_fault_valve(pressure, scaling_factor, agent, decision, node):
"""
Adjust the decision based on the output of the DRL agent for 'Fault valve'.
"""
normalized_pressure = pressure / 101350
rl_value = np.round(scaling_factor.scale(agent.select_action([normalized_pressure]))[0], 2)
value = float(decision[1])
index = node.get_state_index_from_value(value)
uper_interval = node.get_state_value(index + 1)
if rl_value < uper_interval and value < rl_value:
decision[1] = rl_value
return decision
# Main Execution - Conflict Detection
#
conflict_model_name = "model_Conflict"
conflict_cc_name = f"C:\\Users\\jomie\\Documents\\GitHub\\Code_Journal_processes\\{conflict_model_name}.net"
conflict_dom = Domain.parse_domain(conflict_cc_name )
conflict_dom.compile()
fault_node = conflict_dom.get_node_by_name("Fault")
least_conflicting_state = find_least_conflicting_state(conflict_dom, fault_node)
print(least_conflicting_state)
conflict_dom.delete()
#
# Main Execution - Decision Making
#
if least_conflicting_state[1]!="none":
decision_model_name = "DID"
decision_cc_name = f"C:\\Users\\jomie\\Documents\\GitHub\\Code_Journal_processes\\{decision_model_name}.oobn"
decision_dom = load_model(decision_cc_name, decision_model_name, 10)
decision_dom.compile()
decision_dom.compress()
decision_node_names = ["T1.Auto_Nitrogen", "T1.Set_point_Nitrogen", "T1.Systeme", "T1.Auto_Pump", "T1.Set_point_pump"]
decision_nodes = [decision_dom.get_node_by_name(name) for name in decision_node_names]
set_evidence(decision_dom.get_node_by_name(f"T1.{least_conflicting_state[0]}"), least_conflicting_state[1])
set_standard_evidence_decision(decision_dom)
decision_dom.propagate()
best_decisions = np.array(find_best_decision(decision_dom, decision_nodes))
Pressure=101000
# Main Execution
Pressure = 101000
if least_conflicting_state[1] == "Fault control":
node=decision_dom.get_node_by_name(best_decisions[1][0])
best_decisions[1] = adjust_decision_for_fault_control(
Pressure, scaling_factor_tank, agent_DRL_S1_tank,
standard_tank, best_decisions[1], node
)
elif least_conflicting_state[1] == "Fault valve":
node=decision_dom.get_node_by_name(best_decisions[4][0])
best_decisions[4] = adjust_decision_for_fault_valve(
Pressure, scaling_factor_pump, agent_DRL_S2_pump,
best_decisions[4], node
)
#print(best_decisions)
decision_dom.delete()
data = best_decisions
# Iterate over the list and process the conditions
for i in range(len(data)):
if data[i][0] == 'T1.Auto_Nitrogen' and data[i][1] == '0.0':
set_point_nitrogen = data[i + 1][1] if i + 1 < len(data) else "Unknown"
print(f"Set the nitrogen flow to manual mode with a set point of {set_point_nitrogen}")
if data[i][0] == 'T1.Systeme' and data[i][1] == '2.0':
print("Switch to backup system")
if data[i][0] == 'T1.Auto_Pump' and data[i][1] == '0.0':
set_point_pump = data[i + 1][1] if i + 1 < len(data) else "Unknown"
print(f"Set the pump power to auto with a set the point of {set_point_pump}")
else:
print("Monitoring")