Skip to content
Snippets Groups Projects
Commit 4fdde49a authored by Aleksandra Dimitrova's avatar Aleksandra Dimitrova
Browse files

added documentation

parent e820b6df
No related branches found
No related tags found
No related merge requests found
Showing
with 704 additions and 2 deletions
"""Script to test the agent on an event log.
"""Script to train and test the agent on an event log.
To train uncomment line 52.
To see the logs run the following command on the terminal: tensorboard --logdir logs
"""
import simpy
......
No preview for this file type
No preview for this file type
......@@ -263,3 +263,4 @@ class BusinessProcess(object):
return True
else:
return False
\ No newline at end of file
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 622c302ccb4811f080b9ebb454d75806
tags: 645f666f9bcd5a90fca523b33c5a78b7
This diff is collapsed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>case &#8212; OPTIS 1.0.0 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for case</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<div class="viewcode-block" id="Case"><a class="viewcode-back" href="../case.html#case.Case">[docs]</a><span class="k">class</span> <span class="nc">Case</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A class representing a single Case from the process </span>
<span class="sd"> ....</span>
<span class="sd"> Attributes</span>
<span class="sd"> ----------</span>
<span class="sd"> case_id : int</span>
<span class="sd"> unique case-id</span>
<span class="sd"> state : numpy.ndarray</span>
<span class="sd"> shows the trace of the case - which activities have been already done</span>
<span class="sd"> current : int </span>
<span class="sd"> shows the current activity being executed in the case</span>
<span class="sd"> agent : bool</span>
<span class="sd"> flag showing if a RL-agent is currently controlling the case</span>
<span class="sd"> standard_order: bool </span>
<span class="sd"> flag showing whether the order is standard or custom</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">case</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> case : int</span>
<span class="sd"> case-id of the case</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">case_id</span> <span class="o">=</span> <span class="n">case</span>
<span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span> <span class="n">dtype</span> <span class="o">=</span> <span class="nb">int</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">current</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">agent</span> <span class="o">=</span> <span class="kc">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">standard_order</span> <span class="o">=</span> <span class="kc">True</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">OPTIS</a></h1>
<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../modules.html">Backend</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Module code</a><ul>
</ul></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2023, OPTIS Team.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>dqn &#8212; OPTIS 1.0.0 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css" />
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for dqn</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot; DQN-Agent</span>
<span class="sd">This module includes the training and deployment of a DQN, which uses a custom environment and simulation model of a process</span>
<span class="sd">to learn to optimize that process by recommending the optimal next activity (in terms of saving time). </span>
<span class="sd">It includes the following functions:</span>
<span class="sd"> * train(space, activities)</span>
<span class="sd"> * deploy(state)</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">stable_baselines3</span> <span class="kn">import</span> <span class="n">DQN</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">environment</span> <span class="kn">import</span> <span class="n">BusinessProcessEnv</span>
<div class="viewcode-block" id="train"><a class="viewcode-back" href="../dqn.html#dqn.train">[docs]</a><span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="n">space</span><span class="p">,</span> <span class="n">activities</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Train a DQN on a simulation model of a process and save the model and its logs.</span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> space : list of list of int</span>
<span class="sd"> contains the numbers of resources and indicates how the trace of a case looks like</span>
<span class="sd"> activities : int</span>
<span class="sd"> the number of allowed activities in the process</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># initialize environment</span>
<span class="n">env</span> <span class="o">=</span> <span class="n">BusinessProcessEnv</span><span class="p">(</span><span class="n">space</span><span class="p">,</span> <span class="n">activities</span><span class="p">)</span>
<span class="n">env</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="c1"># create directories fro saving the models and logs</span>
<span class="n">models_dir</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;models/</span><span class="si">{</span><span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span><span class="si">}</span><span class="s2">/&quot;</span>
<span class="n">logdir</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;logs/</span><span class="si">{</span><span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span><span class="si">}</span><span class="s2">/&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">models_dir</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">models_dir</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">logdir</span><span class="p">):</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">logdir</span><span class="p">)</span>
<span class="c1"># create and train the DQN model</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">DQN</span><span class="p">(</span><span class="s1">&#39;MultiInputPolicy&#39;</span><span class="p">,</span> <span class="n">env</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">exploration_fraction</span> <span class="o">=</span> <span class="mf">0.33</span><span class="p">,</span> <span class="n">learning_starts</span> <span class="o">=</span> <span class="mi">10000</span><span class="p">,</span> <span class="n">tensorboard_log</span><span class="o">=</span><span class="n">logdir</span><span class="p">)</span>
<span class="c1"># save a model every TIMESTEPS </span>
<span class="n">TIMESTEPS</span> <span class="o">=</span> <span class="mi">1000000</span>
<span class="n">iters</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="n">iters</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">model</span><span class="o">.</span><span class="n">learn</span><span class="p">(</span><span class="n">total_timesteps</span><span class="o">=</span><span class="n">TIMESTEPS</span><span class="p">,</span> <span class="n">reset_num_timesteps</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">tb_log_name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;DQN&quot;</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">models_dir</span><span class="si">}</span><span class="s2">/</span><span class="si">{</span><span class="n">TIMESTEPS</span><span class="o">*</span><span class="n">iters</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span></div>
<span class="c1"># choose a working model and use it to recommend the next action for the given state</span>
<div class="viewcode-block" id="deploy"><a class="viewcode-back" href="../dqn.html#dqn.deploy">[docs]</a><span class="k">def</span> <span class="nf">deploy</span><span class="p">(</span><span class="n">state</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Deploy a DQN-model by using it to recommend the best next activity for a certain state.</span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> state : collections.OrderedDict of {str : list of int, str : int, str : list of int}</span>
<span class="sd"> the state of a case in a process (the trace of the case, the current event in the case, the available resources)</span>
<span class="sd"> Returns</span>
<span class="sd"> -------</span>
<span class="sd"> int</span>
<span class="sd"> number of the encoded optimal action to take</span>
<span class="sd"> </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">DQN</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;../models/1687176631/2000000&#39;</span><span class="p">)</span>
<span class="n">action</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="n">deterministic</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">action</span></div>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">OPTIS</a></h1>
<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../modules.html">Backend</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="index.html">Module code</a><ul>
</ul></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2023, OPTIS Team.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
</div>
</body>
</html>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment