Skip to content
Snippets Groups Projects
Commit 683b7a21 authored by Nour's avatar Nour
Browse files

merged for testing

parents b5738616 fcc8943e
No related branches found
No related tags found
No related merge requests found
Showing
with 14340 additions and 21 deletions
......@@ -13,8 +13,13 @@ The following are the import for the backend. Just write the name of the script
Then you can immediately use the function from the script directly in the app.
"""
sys.path.append(os.path.join(os.path.dirname(sys.path[0]),'backend'))
<<<<<<< HEAD
import input, dqn, petrinet
import eventlog as elog
=======
import input
>>>>>>> nour
#define the app
app = Flask(__name__)
......
......@@ -21,9 +21,9 @@ def train(space, activities):
os.makedirs(logdir)
# model = PPO('MultiInputPolicy', env, verbose=1, tensorboard_log=logdir)
model = DQN('MultiInputPolicy', env, verbose=1, exploration_fraction = 0.8, gamma=0.1, learning_starts = 10000, tensorboard_log=logdir)
model = DQN('MultiInputPolicy', env, verbose=1, exploration_fraction = 0.33, learning_starts = 10000, tensorboard_log=logdir)
TIMESTEPS = 1250000
TIMESTEPS = 1000000
iters = 0
while True:
iters += 1
......
import numpy as np
import gymnasium as gym
import environment
from stable_baselines3 import PPO, DQN
import os
import time
def main():
process = []
num_s = 1
process.append(num_s+1)
num_ot = 5
process.append(num_ot+1)
num_sh_a = 3
process.append(num_sh_a+1)
num_sh_b = 3
process.append(num_sh_b+1)
num_sh_c = 3
process.append(num_sh_c+1)
num_m_a = 3
process.append(num_m_a+1)
num_m_b = 2
process.append(num_m_b+1)
num_p_a = 4
process.append(num_p_a+1)
num_p_b = 5
process.append(num_p_b+1)
num_p_c = 4
process.append(num_p_c+1)
num_ds_a = 7
process.append(num_ds_a+1)
num_ds_b = 7
process.append(num_ds_b+1)
num_ds_c = 7
process.append(num_ds_c+1)
case = []
for i in range(15):
case.append(2)
space = [process, case]
activities = 16
env = environment.BusinessProcessEnv(space, activities)
env.reset()
models_dir = f"models/{int(time.time())}/"
logdir = f"logs/{int(time.time())}/"
if not os.path.exists(models_dir):
os.makedirs(models_dir)
if not os.path.exists(logdir):
os.makedirs(logdir)
# model = PPO('MultiInputPolicy', env, verbose=1, tensorboard_log=logdir)
model = DQN('MultiInputPolicy', env, verbose=1, tensorboard_log=logdir)
TIMESTEPS = 500000
iters = 0
while True:
iters += 1
model.learn(total_timesteps=TIMESTEPS, reset_num_timesteps=False, tb_log_name=f"DQN")
model.save(f"{models_dir}/{TIMESTEPS*iters}")
if __name__ == "__main__":
main()
......@@ -40,6 +40,8 @@ class BusinessProcessEnv(gym.Env):
self.reward = 0
def get_current_state(self, caseid):
process, case, event = simmodel.get_current_state(self.process, caseid)
state = OrderedDict()
......@@ -48,6 +50,18 @@ class BusinessProcessEnv(gym.Env):
state['process'] = np.asarray(process)
return state
def get_ressources(self, caseid, action):
state = self.get_current_state(caseid)
if action == 1 or action == 15:
return state['process'][0]
elif action == 2 or action == 3:
return state['process'][1]
else:
ressours_position = action - 2
return state['process'][ressours_position]
def step(self, action):
self.process.next = action
......@@ -68,7 +82,7 @@ class BusinessProcessEnv(gym.Env):
self.model_env.process(simmodel.execute_case(self.model_env, self.process.case_id, self.process))
'''
if self.process.case_id == None or self.process.case_id in self.process.done_cases:
if self.process.case_id in self.process.done_cases:
early_case = self.process.active_cases[0]
early = 15
for next_case in self.process.active_cases:
......@@ -83,29 +97,50 @@ class BusinessProcessEnv(gym.Env):
case_obj = self.process.case_objects[self.process.case_id]
self.current_state = self.get_current_state(case_obj)
# print(f"{self.current_state['event']}")
# print(self.process.case_id)
# print(self.get_current_state(case_obj))
start = self.process.env.now
self.process.flag = True
if self.process.is_valid(self.current_state['event'], action, case_obj):
print(f"{self.current_state}")
print(action)
print(self.current_state['process'])
print(self.get_ressources(case_obj, action))
while(self.process.flag):
self.model_env.step()
if self.get_ressources(case_obj, action ) == 0 :
reward = -10
self.reward += reward
next_state = self.current_state
done = False
truncated = False
info = {}
return next_state, reward, done, truncated, info
stop = self.process.env.now
# case_obj = self.process.case_objects[self.process.case_id]
print(f"Agent did case {self.process.case_id} activity {action}.")
# print(f"Agent did case {self.process.case_id} activity {action}.")
next_state = self.get_current_state(case_obj)
self.current_state = next_state
# next_state = self.flatten_observation_to_int(next_state)
time = stop - start
if time == 0:
reward = 4
reward = 0
else:
reward = 4.5 - math.log(time, 10)
self.reward += reward
......@@ -115,7 +150,7 @@ class BusinessProcessEnv(gym.Env):
return next_state, reward, done, truncated, info # either self.reward or just reward ???
else:
reward = -0.1
reward = -20
self.reward += reward
# next_state = self.flatten_observation_to_int(self.current_state)
next_state = self.current_state
......
......@@ -38,7 +38,6 @@ def export_to_csv(process, file_path):
event_log_df = pd.DataFrame.from_dict(process.event_log)
event_log_df.to_csv(file_path, index=False)
def export_to_xes(process, file_path):
event_log = process.event_log
......@@ -156,9 +155,9 @@ def get_state(case_id,name):
process[8] = num_p_b
num_p_c = 3
process[9] = num_p_c
num_ds_a = 25
num_ds_a = 30
process[10] = num_ds_a
num_ds_b = 40
num_ds_b = 45
process[11] = num_ds_b
num_ds_c = 45
process[12] = num_ds_c
......@@ -228,6 +227,11 @@ def get_state(case_id,name):
state['event'] = event
state['process'] = process
state = OrderedDict()
state['case'] = case
state['event'] = event
state['process'] = process
# print(state)
return state
......
This diff is collapsed.
File added
File added
File added
File added
File added
File added
......@@ -38,9 +38,9 @@ def main():
ressources.append(num_p_b+1)
num_p_c = 3
ressources.append(num_p_c+1)
num_ds_a = 25
num_ds_a = 30
ressources.append(num_ds_a+1)
num_ds_b = 40
num_ds_b = 45
ressources.append(num_ds_b+1)
num_ds_c = 45
ressources.append(num_ds_c+1)
......@@ -56,13 +56,16 @@ def main():
# dqn.train(space, activities)
# dqn.train(space, activities)
''''''
# generate event log
env = simpy.Environment()
business_process = model.BusinessProcess(env, ressources)
business_process.event_log_flag = True
env.process(model.run_process(env, business_process))
env.run(until = 100)
log.export_to_csv(business_process, r'D:\test\optis.csv')
env.run(until = 30000)
log.export_to_csv(business_process, r'C:\Users\nourm\OneDrive\Desktop\Nour\optis_app\backend\eventlog_test\optis.csv')
# extract active cases from event log
active_cases = log.get_active_cases()
......@@ -78,11 +81,7 @@ def main():
print(dqn.deploy(state))
test.test_agent()
if __name__ == "__main__":
main()
\ No newline at end of file
File added
......@@ -34,18 +34,30 @@ def main():
ressources.append(num_p_b+1)
num_p_c = 3
ressources.append(num_p_c+1)
<<<<<<< HEAD
num_ds_a = 25
ressources.append(num_ds_a+1)
num_ds_b = 40
ressources.append(num_ds_b+1)
num_ds_c = 40
=======
num_ds_a = 30
ressources.append(num_ds_a+1)
num_ds_b = 45
ressources.append(num_ds_b+1)
num_ds_c = 45
>>>>>>> nour
ressources.append(num_ds_c+1)
model_env = simpy.Environment()
process = simmodel.BusinessProcess(model_env, ressources)
model_env.process(simmodel.run_process(model_env, process))
<<<<<<< HEAD
for i in range(2000):
=======
for i in range(30000):
>>>>>>> nour
model_env.step()
print(simmodel.get_ressources(process))
......
......@@ -3,9 +3,15 @@ import numpy as np
import dqn
def test_agent():
<<<<<<< HEAD
case = [1,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
event = 3
process = [1,4,2,2,2,4,0,2,3,3,20,20,20]
=======
case = [1,1,0,1,0,0,0,0,0,0,0,0,0,0,0]
event = 4
process = [0,2,1,2,0,0,3,2,3,3,7,6,0]
>>>>>>> nour
state = OrderedDict()
state['case'] = np.asarray(case)
......@@ -13,6 +19,10 @@ def test_agent():
state['process'] = np.asarray(process)
print(state)
<<<<<<< HEAD
print(dqn.deploy(state))
test_agent()
=======
print(dqn.deploy(state))
>>>>>>> nour
This diff is collapsed.
File deleted
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment