713 lines
96 KiB
HTML
713 lines
96 KiB
HTML
|
|
|
|
<!doctype html>
|
|
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>pyFTS.hyperparam.Evolutionary — pyFTS 1.7 documentation</title>
|
|
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
|
|
<link rel="stylesheet" type="text/css" href="../../../_static/bizstyle.css" />
|
|
|
|
<script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
|
|
<script src="../../../_static/jquery.js"></script>
|
|
<script src="../../../_static/underscore.js"></script>
|
|
<script src="../../../_static/doctools.js"></script>
|
|
<script src="../../../_static/bizstyle.js"></script>
|
|
<link rel="index" title="Index" href="../../../genindex.html" />
|
|
<link rel="search" title="Search" href="../../../search.html" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
|
<!--[if lt IE 9]>
|
|
<script src="_static/css3-mediaqueries.js"></script>
|
|
<![endif]-->
|
|
</head><body>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li class="right" >
|
|
<a href="../../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">pyFTS.hyperparam.Evolutionary</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<h1>Source code for pyFTS.hyperparam.Evolutionary</h1><div class="highlight"><pre>
|
|
<span></span><span class="sd">"""</span>
|
|
<span class="sd">Distributed Evolutionary Hyperparameter Optimization (DEHO) for MVFTS</span>
|
|
<span class="sd">"""</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
|
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
|
|
<span class="kn">import</span> <span class="nn">math</span>
|
|
<span class="kn">import</span> <span class="nn">time</span>
|
|
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">reduce</span>
|
|
<span class="kn">from</span> <span class="nn">operator</span> <span class="k">import</span> <span class="n">itemgetter</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">random</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.partitioners</span> <span class="k">import</span> <span class="n">Grid</span><span class="p">,</span> <span class="n">Entropy</span> <span class="c1"># , Huarng</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Membership</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">ifts</span><span class="p">,</span> <span class="n">pwfts</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.hyperparam</span> <span class="k">import</span> <span class="n">Util</span> <span class="k">as</span> <span class="n">hUtil</span>
|
|
|
|
|
|
<span class="n">__measures</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'f1'</span><span class="p">,</span> <span class="s1">'f2'</span><span class="p">,</span> <span class="s1">'rmse'</span><span class="p">,</span> <span class="s1">'size'</span><span class="p">]</span>
|
|
|
|
|
|
<div class="viewcode-block" id="genotype"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.genotype">[docs]</a><span class="k">def</span> <span class="nf">genotype</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">npart</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">lags</span><span class="p">,</span> <span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Create the individual genotype</span>
|
|
|
|
<span class="sd"> :param mf: membership function</span>
|
|
<span class="sd"> :param npart: number of partitions</span>
|
|
<span class="sd"> :param partitioner: partitioner method</span>
|
|
<span class="sd"> :param order: model order</span>
|
|
<span class="sd"> :param alpha: alpha-cut</span>
|
|
<span class="sd"> :param lags: array with lag indexes</span>
|
|
<span class="sd"> :param f1: accuracy fitness value</span>
|
|
<span class="sd"> :param f2: parsimony fitness value</span>
|
|
<span class="sd"> :return: the genotype, a dictionary with all hyperparameters</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">ind</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">mf</span><span class="o">=</span><span class="n">mf</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">npart</span><span class="p">,</span> <span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="n">order</span><span class="p">,</span>
|
|
<span class="n">alpha</span><span class="o">=</span><span class="n">alpha</span><span class="p">,</span> <span class="n">lags</span><span class="o">=</span><span class="n">lags</span><span class="p">,</span> <span class="n">f1</span><span class="o">=</span><span class="n">f1</span><span class="p">,</span> <span class="n">f2</span><span class="o">=</span><span class="n">f2</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">ind</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="random_genotype"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.random_genotype">[docs]</a><span class="k">def</span> <span class="nf">random_genotype</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Create random genotype</span>
|
|
|
|
<span class="sd"> :return: the genotype, a dictionary with all hyperparameters</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">order</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
|
|
<span class="n">lags</span> <span class="o">=</span> <span class="p">[</span><span class="n">k</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="o">+</span><span class="mi">1</span><span class="p">)]</span>
|
|
<span class="k">return</span> <span class="n">genotype</span><span class="p">(</span>
|
|
<span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
|
|
<span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">100</span><span class="p">),</span>
|
|
<span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
|
|
<span class="n">order</span><span class="p">,</span>
|
|
<span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">),</span>
|
|
<span class="n">lags</span><span class="p">,</span>
|
|
<span class="kc">None</span><span class="p">,</span>
|
|
<span class="kc">None</span>
|
|
<span class="p">)</span></div>
|
|
|
|
|
|
<span class="c1">#</span>
|
|
<div class="viewcode-block" id="initial_population"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.initial_population">[docs]</a><span class="k">def</span> <span class="nf">initial_population</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Create a random population of size n</span>
|
|
|
|
<span class="sd"> :param n: the size of the population</span>
|
|
<span class="sd"> :return: a list with n random individuals</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">create_random_individual</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'random_individual'</span><span class="p">,</span> <span class="n">random_genotype</span><span class="p">)</span>
|
|
|
|
<span class="n">pop</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
|
|
<span class="n">pop</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">create_random_individual</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
|
|
<span class="k">return</span> <span class="n">pop</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="phenotype"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.phenotype">[docs]</a><span class="k">def</span> <span class="nf">phenotype</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="p">{},</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Instantiate the genotype, creating a fitted model with the genotype hyperparameters</span>
|
|
|
|
<span class="sd"> :param individual: a genotype</span>
|
|
<span class="sd"> :param train: the training dataset</span>
|
|
<span class="sd"> :param fts_method: the FTS method </span>
|
|
<span class="sd"> :param parameters: dict with model specific arguments for fit method.</span>
|
|
<span class="sd"> :return: a fitted FTS model</span>
|
|
<span class="sd"> """</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">ifts</span><span class="p">,</span> <span class="n">pwfts</span>
|
|
|
|
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span>
|
|
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
|
|
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trapmf</span>
|
|
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
|
|
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">gaussmf</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">mf</span> <span class="o">=</span> <span class="n">Membership</span><span class="o">.</span><span class="n">trimf</span>
|
|
|
|
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="n">partitioner</span> <span class="o">=</span> <span class="n">Grid</span><span class="o">.</span><span class="n">GridPartitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">train</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">],</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">)</span>
|
|
<span class="k">elif</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
|
|
<span class="n">partitioner</span> <span class="o">=</span> <span class="n">Entropy</span><span class="o">.</span><span class="n">EntropyPartitioner</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">train</span><span class="p">,</span> <span class="n">npart</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">],</span> <span class="n">func</span><span class="o">=</span><span class="n">mf</span><span class="p">)</span>
|
|
|
|
<span class="n">model</span> <span class="o">=</span> <span class="n">fts_method</span><span class="p">(</span><span class="n">partitioner</span><span class="o">=</span><span class="n">partitioner</span><span class="p">,</span>
|
|
<span class="n">lags</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">],</span>
|
|
<span class="n">alpha_cut</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">'alpha'</span><span class="p">],</span>
|
|
<span class="n">order</span><span class="o">=</span><span class="n">individual</span><span class="p">[</span><span class="s1">'order'</span><span class="p">])</span>
|
|
|
|
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="o">**</span><span class="n">parameters</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">model</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="evaluate"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.evaluate">[docs]</a><span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Evaluate an individual using a sliding window cross validation over the dataset.</span>
|
|
|
|
<span class="sd"> :param dataset: Evaluation dataset</span>
|
|
<span class="sd"> :param individual: genotype to be tested</span>
|
|
<span class="sd"> :param window_size: The length of scrolling window for train/test on dataset</span>
|
|
<span class="sd"> :param train_rate: The train/test split ([0,1])</span>
|
|
<span class="sd"> :param increment_rate: The increment of the scrolling window, relative to the window_size ([0,1])</span>
|
|
<span class="sd"> :param parameters: dict with model specific arguments for fit method.</span>
|
|
<span class="sd"> :return: a tuple (len_lags, rmse) with the parsimony fitness value and the accuracy fitness value</span>
|
|
<span class="sd"> """</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.models</span> <span class="k">import</span> <span class="n">hofts</span><span class="p">,</span> <span class="n">ifts</span><span class="p">,</span> <span class="n">pwfts</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.common</span> <span class="k">import</span> <span class="n">Util</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.benchmarks</span> <span class="k">import</span> <span class="n">Measures</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.hyperparam.Evolutionary</span> <span class="k">import</span> <span class="n">phenotype</span><span class="p">,</span> <span class="n">__measures</span>
|
|
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
|
|
|
|
<span class="n">window_size</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'window_size'</span><span class="p">,</span> <span class="mi">800</span><span class="p">)</span>
|
|
<span class="n">train_rate</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'train_rate'</span><span class="p">,</span> <span class="o">.</span><span class="mi">8</span><span class="p">)</span>
|
|
<span class="n">increment_rate</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'increment_rate'</span><span class="p">,</span> <span class="o">.</span><span class="mi">2</span><span class="p">)</span>
|
|
<span class="n">fts_method</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'fts_method'</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
|
|
<span class="n">parameters</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'parameters'</span><span class="p">,{})</span>
|
|
|
|
<span class="k">if</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'f1'</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'f2'</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="p">{</span> <span class="n">key</span><span class="p">:</span> <span class="n">individual</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span> <span class="p">}</span>
|
|
|
|
<span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="n">lengths</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">for</span> <span class="n">count</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">Util</span><span class="o">.</span><span class="n">sliding_window</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">window_size</span><span class="p">,</span> <span class="n">train</span><span class="o">=</span><span class="n">train_rate</span><span class="p">,</span> <span class="n">inc</span><span class="o">=</span><span class="n">increment_rate</span><span class="p">):</span>
|
|
|
|
<span class="k">try</span><span class="p">:</span>
|
|
|
|
<span class="n">model</span> <span class="o">=</span> <span class="n">phenotype</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">fts_method</span><span class="o">=</span><span class="n">fts_method</span><span class="p">,</span> <span class="n">parameters</span><span class="o">=</span><span class="n">parameters</span><span class="p">)</span>
|
|
|
|
<span class="n">forecasts</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">test</span><span class="p">)</span>
|
|
|
|
<span class="n">rmse</span> <span class="o">=</span> <span class="n">Measures</span><span class="o">.</span><span class="n">rmse</span><span class="p">(</span><span class="n">test</span><span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">max_lag</span><span class="p">:],</span> <span class="n">forecasts</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
|
|
<span class="n">lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">model</span><span class="p">))</span>
|
|
|
|
<span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rmse</span><span class="p">)</span>
|
|
|
|
<span class="k">except</span><span class="p">:</span>
|
|
<span class="n">lengths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">)</span>
|
|
<span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">)</span>
|
|
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">_lags</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">lags</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span>
|
|
|
|
<span class="n">_rmse</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span>
|
|
<span class="n">_len</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">lengths</span><span class="p">)</span>
|
|
|
|
<span class="n">f1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="o">.</span><span class="mi">6</span> <span class="o">*</span> <span class="n">_rmse</span><span class="p">,</span> <span class="o">.</span><span class="mi">4</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">nanstd</span><span class="p">(</span><span class="n">errors</span><span class="p">)])</span>
|
|
<span class="n">f2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nansum</span><span class="p">([</span><span class="o">.</span><span class="mi">4</span> <span class="o">*</span> <span class="n">_len</span><span class="p">,</span> <span class="o">.</span><span class="mi">6</span> <span class="o">*</span> <span class="n">_lags</span><span class="p">])</span>
|
|
|
|
<span class="k">return</span> <span class="p">{</span><span class="s1">'f1'</span><span class="p">:</span> <span class="n">f1</span><span class="p">,</span> <span class="s1">'f2'</span><span class="p">:</span> <span class="n">f2</span><span class="p">,</span> <span class="s1">'rmse'</span><span class="p">:</span> <span class="n">_rmse</span><span class="p">,</span> <span class="s1">'size'</span><span class="p">:</span> <span class="n">_len</span> <span class="p">}</span>
|
|
<span class="k">except</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="p">{</span><span class="s1">'f1'</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="s1">'f2'</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="s1">'rmse'</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="s1">'size'</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">}</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="tournament"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.tournament">[docs]</a><span class="k">def</span> <span class="nf">tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">objective</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Simple tournament selection strategy.</span>
|
|
|
|
<span class="sd"> :param population: the population</span>
|
|
<span class="sd"> :param objective: the objective to be considered on tournament</span>
|
|
<span class="sd"> :return:</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">population</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
|
|
|
|
<span class="n">r1</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span> <span class="o">></span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">0</span>
|
|
<span class="n">r2</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">if</span> <span class="n">n</span> <span class="o">></span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">1</span>
|
|
<span class="n">ix</span> <span class="o">=</span> <span class="n">r1</span> <span class="k">if</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">][</span><span class="n">objective</span><span class="p">]</span> <span class="o"><</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">][</span><span class="n">objective</span><span class="p">]</span> <span class="k">else</span> <span class="n">r2</span>
|
|
<span class="k">return</span> <span class="n">population</span><span class="p">[</span><span class="n">ix</span><span class="p">]</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="double_tournament"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.double_tournament">[docs]</a><span class="k">def</span> <span class="nf">double_tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Double tournament selection strategy.</span>
|
|
|
|
<span class="sd"> :param population:</span>
|
|
<span class="sd"> :return:</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">ancestor1</span> <span class="o">=</span> <span class="n">tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="s1">'f1'</span><span class="p">)</span>
|
|
<span class="n">ancestor2</span> <span class="o">=</span> <span class="n">tournament</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="s1">'f1'</span><span class="p">)</span>
|
|
|
|
<span class="n">selected</span> <span class="o">=</span> <span class="n">tournament</span><span class="p">([</span><span class="n">ancestor1</span><span class="p">,</span> <span class="n">ancestor2</span><span class="p">],</span> <span class="s1">'f2'</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">selected</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="lag_crossover2"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.lag_crossover2">[docs]</a><span class="k">def</span> <span class="nf">lag_crossover2</span><span class="p">(</span><span class="n">best</span><span class="p">,</span> <span class="n">worst</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Cross over two lag genes</span>
|
|
|
|
<span class="sd"> :param best: best genotype</span>
|
|
<span class="sd"> :param worst: worst genotype</span>
|
|
<span class="sd"> :return: a tuple (order, lags)</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">order</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">'order'</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'order'</span><span class="p">]))</span>
|
|
<span class="n">lags</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">min_order</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">best</span><span class="p">[</span><span class="s1">'order'</span><span class="p">],</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'order'</span><span class="p">])</span>
|
|
|
|
<span class="n">max_order</span> <span class="o">=</span> <span class="n">best</span> <span class="k">if</span> <span class="n">best</span><span class="p">[</span><span class="s1">'order'</span><span class="p">]</span> <span class="o">></span> <span class="n">min_order</span> <span class="k">else</span> <span class="n">worst</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="n">k</span> <span class="o"><</span> <span class="n">min_order</span><span class="p">:</span>
|
|
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">][</span><span class="n">k</span><span class="p">])))</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">lags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">max_order</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">][</span><span class="n">k</span><span class="p">])</span>
|
|
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
|
|
<span class="k">while</span> <span class="n">lags</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">>=</span> <span class="n">lags</span><span class="p">[</span><span class="n">k</span><span class="p">]:</span>
|
|
<span class="n">lags</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">order</span><span class="p">,</span> <span class="n">lags</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="crossover"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.crossover">[docs]</a><span class="k">def</span> <span class="nf">crossover</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Crossover operation between two parents</span>
|
|
|
|
<span class="sd"> :param population: the original population</span>
|
|
<span class="sd"> :return: a genotype</span>
|
|
<span class="sd"> """</span>
|
|
<span class="kn">import</span> <span class="nn">random</span>
|
|
|
|
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">population</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
|
|
|
|
<span class="n">r1</span><span class="p">,</span> <span class="n">r2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>
|
|
<span class="k">while</span> <span class="n">r1</span> <span class="o">==</span> <span class="n">r2</span><span class="p">:</span>
|
|
<span class="n">r1</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
|
|
<span class="n">r2</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">][</span><span class="s1">'f1'</span><span class="p">]</span> <span class="o"><</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">][</span><span class="s1">'f1'</span><span class="p">]:</span>
|
|
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">]</span>
|
|
<span class="n">worst</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">]</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r2</span><span class="p">]</span>
|
|
<span class="n">worst</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="n">r1</span><span class="p">]</span>
|
|
|
|
<span class="n">npart</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">]))</span>
|
|
<span class="n">alpha</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="o">.</span><span class="mi">7</span> <span class="o">*</span> <span class="n">best</span><span class="p">[</span><span class="s1">'alpha'</span><span class="p">]</span> <span class="o">+</span> <span class="o">.</span><span class="mi">3</span> <span class="o">*</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'alpha'</span><span class="p">])</span>
|
|
|
|
<span class="n">rnd</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
|
<span class="n">mf</span> <span class="o">=</span> <span class="n">best</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">]</span> <span class="k">if</span> <span class="n">rnd</span> <span class="o"><</span> <span class="o">.</span><span class="mi">7</span> <span class="k">else</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">]</span>
|
|
|
|
<span class="n">rnd</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
|
<span class="n">partitioner</span> <span class="o">=</span> <span class="n">best</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">]</span> <span class="k">if</span> <span class="n">rnd</span> <span class="o"><</span> <span class="o">.</span><span class="mi">7</span> <span class="k">else</span> <span class="n">worst</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">]</span>
|
|
|
|
<span class="n">order</span><span class="p">,</span> <span class="n">lags</span> <span class="o">=</span> <span class="n">lag_crossover2</span><span class="p">(</span><span class="n">best</span><span class="p">,</span> <span class="n">worst</span><span class="p">)</span>
|
|
|
|
<span class="n">descendent</span> <span class="o">=</span> <span class="n">genotype</span><span class="p">(</span><span class="n">mf</span><span class="p">,</span> <span class="n">npart</span><span class="p">,</span> <span class="n">partitioner</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">lags</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">descendent</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="mutation_lags"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.mutation_lags">[docs]</a><span class="k">def</span> <span class="nf">mutation_lags</span><span class="p">(</span><span class="n">lags</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Mutation operation for lags gene</span>
|
|
|
|
<span class="sd"> :param lags:</span>
|
|
<span class="sd"> :param order:</span>
|
|
<span class="sd"> :return:</span>
|
|
<span class="sd"> """</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lags</span><span class="p">)</span>
|
|
<span class="n">new</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">lag</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">order</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="n">lag</span> <span class="o"><</span> <span class="n">l</span><span class="p">:</span>
|
|
<span class="n">new</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="nb">min</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">lags</span><span class="p">[</span><span class="n">lag</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">))))</span> <span class="p">)</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">new</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="n">new</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">order</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="p">):</span>
|
|
<span class="k">while</span> <span class="n">new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o"><=</span> <span class="n">new</span><span class="p">[</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]:</span>
|
|
<span class="n">new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">new</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
|
|
|
|
<span class="k">return</span> <span class="n">new</span>
|
|
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">lags</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">new</span><span class="p">,</span> <span class="n">lag</span><span class="p">)</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="mutation"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.mutation">[docs]</a><span class="k">def</span> <span class="nf">mutation</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Mutation operator</span>
|
|
|
|
<span class="sd"> :param individual: an individual genotype</span>
|
|
<span class="sd"> :param pmut: individual probability o</span>
|
|
<span class="sd"> :return:</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">individual</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">))))</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'alpha'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'alpha'</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">)))</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'order'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">individual</span><span class="p">[</span><span class="s1">'order'</span><span class="p">]</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))))</span>
|
|
<span class="c1"># Chama a função mutation_lags</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">]</span> <span class="o">=</span> <span class="n">mutation_lags</span><span class="p">(</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">],</span> <span class="n">individual</span><span class="p">[</span><span class="s1">'order'</span><span class="p">])</span>
|
|
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'f1'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="s1">'f2'</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
|
|
|
|
<span class="k">return</span> <span class="n">individual</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="elitism"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.elitism">[docs]</a><span class="k">def</span> <span class="nf">elitism</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">new_population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Elitism operation, always select the best individual of the population and discard the worst</span>
|
|
|
|
<span class="sd"> :param population:</span>
|
|
<span class="sd"> :param new_population:</span>
|
|
<span class="sd"> :return:</span>
|
|
<span class="sd"> """</span>
|
|
<span class="n">population</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="s1">'f1'</span><span class="p">))</span>
|
|
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|
|
|
<span class="n">new_population</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">new_population</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="s1">'f1'</span><span class="p">))</span>
|
|
<span class="k">if</span> <span class="n">new_population</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">"f1"</span><span class="p">]</span> <span class="o">></span> <span class="n">best</span><span class="p">[</span><span class="s2">"f1"</span><span class="p">]:</span>
|
|
<span class="n">new_population</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">best</span><span class="p">)</span>
|
|
<span class="k">elif</span> <span class="n">new_population</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">"f1"</span><span class="p">]</span> <span class="o">==</span> <span class="n">best</span><span class="p">[</span><span class="s2">"f1"</span><span class="p">]</span> <span class="ow">and</span> <span class="n">new_population</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s2">"f2"</span><span class="p">]</span> <span class="o">></span> <span class="n">best</span><span class="p">[</span><span class="s2">"f2"</span><span class="p">]:</span>
|
|
<span class="n">new_population</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">best</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">new_population</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="GeneticAlgorithm"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.GeneticAlgorithm">[docs]</a><span class="k">def</span> <span class="nf">GeneticAlgorithm</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Genetic algoritm for Distributed Evolutionary Hyperparameter Optimization (DEHO)</span>
|
|
|
|
<span class="sd"> :param dataset: The time series to optimize the FTS</span>
|
|
<span class="sd"> :keyword ngen: An integer value with the maximum number of generations, default value: 30</span>
|
|
<span class="sd"> :keyword mgen: An integer value with the maximum number of generations without improvement to stop, default value 7</span>
|
|
<span class="sd"> :keyword npop: An integer value with the population size, default value: 20</span>
|
|
<span class="sd"> :keyword pcross: A float value between 0 and 1 with the probability of crossover, default: .5</span>
|
|
<span class="sd"> :keyword psel: A float value between 0 and 1 with the probability of selection, default: .5</span>
|
|
<span class="sd"> :keyword pmut: A float value between 0 and 1 with the probability of mutation, default: .3</span>
|
|
<span class="sd"> :keyword fts_method: The FTS method to optimize</span>
|
|
<span class="sd"> :keyword parameters: dict with model specific arguments for fts_method</span>
|
|
<span class="sd"> :keyword elitism: A boolean value indicating if the best individual must always survive to next population</span>
|
|
<span class="sd"> :keyword initial_operator: a function that receives npop and return a random population with size npop</span>
|
|
<span class="sd"> :keyword evalutation_operator: a function that receives a dataset and an individual and return its fitness</span>
|
|
<span class="sd"> :keyword selection_operator: a function that receives the whole population and return a selected individual</span>
|
|
<span class="sd"> :keyword crossover_operator: a function that receives the whole population and return a descendent individual</span>
|
|
<span class="sd"> :keyword mutation_operator: a function that receives one individual and return a changed individual</span>
|
|
<span class="sd"> :keyword window_size: An integer value with the the length of scrolling window for train/test on dataset</span>
|
|
<span class="sd"> :keyword train_rate: A float value between 0 and 1 with the train/test split ([0,1])</span>
|
|
<span class="sd"> :keyword increment_rate: A float value between 0 and 1 with the the increment of the scrolling window,</span>
|
|
<span class="sd"> relative to the window_size ([0,1])</span>
|
|
<span class="sd"> :keyword collect_statistics: A boolean value indicating to collect statistics for each generation</span>
|
|
<span class="sd"> :keyword distributed: A value indicating it the execution will be local and sequential (distributed=False),</span>
|
|
<span class="sd"> or parallel and distributed (distributed='dispy' or distributed='spark')</span>
|
|
<span class="sd"> :keyword cluster: If distributed='dispy' the list of cluster nodes, else if distributed='spark' it is the master node</span>
|
|
<span class="sd"> :return: the best genotype</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">statistics</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">ngen</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'ngen'</span><span class="p">,</span><span class="mi">30</span><span class="p">)</span>
|
|
<span class="n">mgen</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'mgen'</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
|
|
<span class="n">npop</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'npop'</span><span class="p">,</span><span class="mi">20</span><span class="p">)</span>
|
|
<span class="n">psel</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'psel'</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">)</span>
|
|
<span class="n">pcross</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'pcross'</span><span class="p">,</span><span class="o">.</span><span class="mi">5</span><span class="p">)</span>
|
|
<span class="n">pmut</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'pmut'</span><span class="p">,</span><span class="o">.</span><span class="mi">3</span><span class="p">)</span>
|
|
<span class="n">distributed</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'distributed'</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
|
|
|
|
<span class="n">initial_operator</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'initial_operator'</span><span class="p">,</span> <span class="n">initial_population</span><span class="p">)</span>
|
|
<span class="n">evaluation_operator</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'evaluation_operator'</span><span class="p">,</span> <span class="n">evaluate</span><span class="p">)</span>
|
|
<span class="n">selection_operator</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'selection_operator'</span><span class="p">,</span> <span class="n">double_tournament</span><span class="p">)</span>
|
|
<span class="n">crossover_operator</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'crossover_operator'</span><span class="p">,</span> <span class="n">crossover</span><span class="p">)</span>
|
|
<span class="n">mutation_operator</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'mutation_operator'</span><span class="p">,</span> <span class="n">mutation</span><span class="p">)</span>
|
|
|
|
<span class="n">_elitism</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'elitism'</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
|
|
|
|
<span class="n">elitism_operator</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'elitism_operator'</span><span class="p">,</span> <span class="n">elitism</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">'dispy'</span><span class="p">:</span>
|
|
<span class="n">cluster</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">'cluster'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
|
|
|
|
<span class="n">collect_statistics</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'collect_statistics'</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
|
|
|
|
<span class="n">no_improvement_count</span> <span class="o">=</span> <span class="mi">0</span>
|
|
|
|
<span class="n">new_population</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">population</span> <span class="o">=</span> <span class="n">initial_operator</span><span class="p">(</span><span class="n">npop</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="n">last_best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"Evaluating initial population </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</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="k">if</span> <span class="ow">not</span> <span class="n">distributed</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">individual</span> <span class="ow">in</span> <span class="n">population</span><span class="p">:</span>
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="n">evaluation_operator</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
|
<span class="k">elif</span> <span class="n">distributed</span><span class="o">==</span><span class="s1">'dispy'</span><span class="p">:</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span> <span class="k">as</span> <span class="n">dUtil</span>
|
|
<span class="kn">import</span> <span class="nn">dispy</span>
|
|
<span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">individual</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">population</span><span class="p">):</span>
|
|
<span class="n">job</span> <span class="o">=</span> <span class="n">cluster</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="n">job</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">ct</span>
|
|
<span class="n">jobs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">:</span>
|
|
<span class="n">result</span> <span class="o">=</span> <span class="n">job</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="n">job</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="n">dispy</span><span class="o">.</span><span class="n">DispyJob</span><span class="o">.</span><span class="n">Finished</span> <span class="ow">and</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
|
|
<span class="n">population</span><span class="p">[</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">exception</span><span class="p">)</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
|
|
|
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ngen</span><span class="p">):</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"GENERATION </span><span class="si">{}</span><span class="s2"> </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</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="n">generation_statistics</span> <span class="o">=</span> <span class="p">{}</span>
|
|
|
|
<span class="c1"># Selection</span>
|
|
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">npop</span> <span class="o">*</span> <span class="n">psel</span><span class="p">)):</span>
|
|
<span class="n">new_population</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">selection_operator</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
|
|
|
|
<span class="c1"># Crossover</span>
|
|
<span class="n">new</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">npop</span> <span class="o">*</span> <span class="n">pcross</span><span class="p">)):</span>
|
|
<span class="n">new</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">crossover_operator</span><span class="p">(</span><span class="n">new_population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
|
|
|
|
<span class="n">new_population</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
|
|
|
|
<span class="c1"># Mutation</span>
|
|
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">individual</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">new_population</span><span class="p">):</span>
|
|
<span class="n">rnd</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">rnd</span> <span class="o"><</span> <span class="n">pmut</span><span class="p">:</span>
|
|
<span class="n">new_population</span><span class="p">[</span><span class="n">ct</span><span class="p">]</span> <span class="o">=</span> <span class="n">mutation_operator</span><span class="p">(</span><span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="c1"># Evaluation</span>
|
|
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span>
|
|
<span class="n">stats</span> <span class="o">=</span> <span class="p">{}</span>
|
|
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
|
|
<span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">distributed</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">individual</span> <span class="ow">in</span> <span class="n">new_population</span><span class="p">:</span>
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="n">evaluation_operator</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
|
|
<span class="n">individual</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">ret</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span> <span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ret</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
|
|
|
|
<span class="k">elif</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">'dispy'</span><span class="p">:</span>
|
|
<span class="n">jobs</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">for</span> <span class="n">ct</span><span class="p">,</span> <span class="n">individual</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">new_population</span><span class="p">):</span>
|
|
<span class="n">job</span> <span class="o">=</span> <span class="n">cluster</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="n">individual</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="n">job</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">ct</span>
|
|
<span class="n">jobs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">job</span> <span class="ow">in</span> <span class="n">jobs</span><span class="p">:</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'job id </span><span class="si">{}</span><span class="s1">'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">))</span>
|
|
<span class="n">result</span> <span class="o">=</span> <span class="n">job</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="n">job</span><span class="o">.</span><span class="n">status</span> <span class="o">==</span> <span class="n">dispy</span><span class="o">.</span><span class="n">DispyJob</span><span class="o">.</span><span class="n">Finished</span> <span class="ow">and</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span><span class="p">:</span>
|
|
<span class="n">new_population</span><span class="p">[</span><span class="n">job</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span> <span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">exception</span><span class="p">)</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
|
|
|
|
|
|
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span>
|
|
<span class="n">mean_stats</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmedian</span><span class="p">(</span><span class="n">stats</span><span class="p">[</span><span class="n">key</span><span class="p">])</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span> <span class="p">}</span>
|
|
|
|
<span class="n">generation_statistics</span><span class="p">[</span><span class="s1">'population'</span><span class="p">]</span> <span class="o">=</span> <span class="n">mean_stats</span>
|
|
|
|
<span class="c1"># Elitism</span>
|
|
<span class="k">if</span> <span class="n">_elitism</span><span class="p">:</span>
|
|
<span class="n">population</span> <span class="o">=</span> <span class="n">elitism_operator</span><span class="p">(</span><span class="n">population</span><span class="p">,</span> <span class="n">new_population</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="n">population</span> <span class="o">=</span> <span class="n">population</span><span class="p">[:</span><span class="n">npop</span><span class="p">]</span>
|
|
|
|
<span class="n">new_population</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">last_best</span> <span class="o">=</span> <span class="n">best</span>
|
|
|
|
<span class="n">best</span> <span class="o">=</span> <span class="n">population</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|
|
|
<span class="k">if</span> <span class="n">collect_statistics</span><span class="p">:</span>
|
|
<span class="n">generation_statistics</span><span class="p">[</span><span class="s1">'best'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="n">key</span><span class="p">:</span> <span class="n">best</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">__measures</span> <span class="p">}</span>
|
|
|
|
<span class="n">statistics</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">generation_statistics</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">last_best</span><span class="p">[</span><span class="s1">'f1'</span><span class="p">]</span> <span class="o"><=</span> <span class="n">best</span><span class="p">[</span><span class="s1">'f1'</span><span class="p">]</span> <span class="ow">and</span> <span class="n">last_best</span><span class="p">[</span><span class="s1">'f2'</span><span class="p">]</span> <span class="o"><=</span> <span class="n">best</span><span class="p">[</span><span class="s1">'f2'</span><span class="p">]:</span>
|
|
<span class="n">no_improvement_count</span> <span class="o">+=</span> <span class="mi">1</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"WITHOUT IMPROVEMENT </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">no_improvement_count</span><span class="p">))</span>
|
|
<span class="n">pmut</span> <span class="o">+=</span> <span class="o">.</span><span class="mi">05</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">no_improvement_count</span> <span class="o">=</span> <span class="mi">0</span>
|
|
<span class="n">pcross</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'pcross'</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">)</span>
|
|
<span class="n">pmut</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'pmut'</span><span class="p">,</span> <span class="o">.</span><span class="mi">3</span><span class="p">)</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">best</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">no_improvement_count</span> <span class="o">==</span> <span class="n">mgen</span><span class="p">:</span>
|
|
<span class="k">break</span>
|
|
|
|
<span class="k">return</span> <span class="n">best</span><span class="p">,</span> <span class="n">statistics</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="process_experiment"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.process_experiment">[docs]</a><span class="k">def</span> <span class="nf">process_experiment</span><span class="p">(</span><span class="n">fts_method</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Persist the results of an DEHO execution in sqlite database (best hyperparameters) and json file (generation statistics)</span>
|
|
|
|
<span class="sd"> :param fts_method:</span>
|
|
<span class="sd"> :param result:</span>
|
|
<span class="sd"> :param datasetname:</span>
|
|
<span class="sd"> :param conn:</span>
|
|
<span class="sd"> :return:</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">log_result</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s1">'individual'</span><span class="p">])</span>
|
|
<span class="n">persist_statistics</span><span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s1">'statistics'</span><span class="p">])</span>
|
|
<span class="k">return</span> <span class="n">result</span><span class="p">[</span><span class="s1">'individual'</span><span class="p">]</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="persist_statistics"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.persist_statistics">[docs]</a><span class="k">def</span> <span class="nf">persist_statistics</span><span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="n">statistics</span><span class="p">):</span>
|
|
<span class="kn">import</span> <span class="nn">json</span>
|
|
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'statistics_</span><span class="si">{}</span><span class="s1">.json'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">datasetname</span><span class="p">),</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
|
|
<span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">statistics</span><span class="p">))</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="log_result"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.log_result">[docs]</a><span class="k">def</span> <span class="nf">log_result</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
|
|
<span class="n">metrics</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'rmse'</span><span class="p">,</span> <span class="s1">'size'</span><span class="p">,</span> <span class="s1">'time'</span><span class="p">]</span>
|
|
<span class="k">for</span> <span class="n">metric</span> <span class="ow">in</span> <span class="n">metrics</span><span class="p">:</span>
|
|
<span class="n">record</span> <span class="o">=</span> <span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="s1">'Evolutive'</span><span class="p">,</span> <span class="n">fts_method</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s1">'mf'</span><span class="p">],</span>
|
|
<span class="n">result</span><span class="p">[</span><span class="s1">'order'</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="s1">'partitioner'</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="s1">'npart'</span><span class="p">],</span>
|
|
<span class="n">result</span><span class="p">[</span><span class="s1">'alpha'</span><span class="p">],</span> <span class="nb">str</span><span class="p">(</span><span class="n">result</span><span class="p">[</span><span class="s1">'lags'</span><span class="p">]),</span> <span class="n">metric</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="n">metric</span><span class="p">])</span>
|
|
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">record</span><span class="p">)</span>
|
|
|
|
<span class="n">hUtil</span><span class="o">.</span><span class="n">insert_hyperparam</span><span class="p">(</span><span class="n">record</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span></div>
|
|
|
|
|
|
<div class="viewcode-block" id="execute"><a class="viewcode-back" href="../../../pyFTS.hyperparam.html#pyFTS.hyperparam.Evolutionary.execute">[docs]</a><span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">datasetname</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="sd">"""</span>
|
|
<span class="sd"> Batch execution of Distributed Evolutionary Hyperparameter Optimization (DEHO) for monovariate methods</span>
|
|
|
|
<span class="sd"> :param datasetname:</span>
|
|
<span class="sd"> :param dataset: The time series to optimize the FTS</span>
|
|
<span class="sd"> :keyword file:</span>
|
|
<span class="sd"> :keyword experiments:</span>
|
|
<span class="sd"> :keyword distributed:</span>
|
|
<span class="sd"> :keyword ngen: An integer value with the maximum number of generations, default value: 30</span>
|
|
<span class="sd"> :keyword mgen: An integer value with the maximum number of generations without improvement to stop, default value 7</span>
|
|
<span class="sd"> :keyword npop: An integer value with the population size, default value: 20</span>
|
|
<span class="sd"> :keyword pcross: A float value between 0 and 1 with the probability of crossover, default: .5</span>
|
|
<span class="sd"> :keyword psel: A float value between 0 and 1 with the probability of selection, default: .5</span>
|
|
<span class="sd"> :keyword pmut: A float value between 0 and 1 with the probability of mutation, default: .3</span>
|
|
<span class="sd"> :keyword fts_method: The FTS method to optimize</span>
|
|
<span class="sd"> :keyword parameters: dict with model specific arguments for fts_method</span>
|
|
<span class="sd"> :keyword elitism: A boolean value indicating if the best individual must always survive to next population</span>
|
|
<span class="sd"> :keyword initial_operator: a function that receives npop and return a random population with size npop</span>
|
|
<span class="sd"> :keyword random_individual: create an random genotype</span>
|
|
<span class="sd"> :keyword evalutation_operator: a function that receives a dataset and an individual and return its fitness</span>
|
|
<span class="sd"> :keyword selection_operator: a function that receives the whole population and return a selected individual</span>
|
|
<span class="sd"> :keyword crossover_operator: a function that receives the whole population and return a descendent individual</span>
|
|
<span class="sd"> :keyword mutation_operator: a function that receives one individual and return a changed individual</span>
|
|
<span class="sd"> :keyword window_size: An integer value with the the length of scrolling window for train/test on dataset</span>
|
|
<span class="sd"> :keyword train_rate: A float value between 0 and 1 with the train/test split ([0,1])</span>
|
|
<span class="sd"> :keyword increment_rate: A float value between 0 and 1 with the the increment of the scrolling window,</span>
|
|
<span class="sd"> relative to the window_size ([0,1])</span>
|
|
<span class="sd"> :keyword collect_statistics: A boolean value indicating to collect statistics for each generation</span>
|
|
<span class="sd"> :keyword distributed: A value indicating it the execution will be local and sequential (distributed=False),</span>
|
|
<span class="sd"> or parallel and distributed (distributed='dispy' or distributed='spark')</span>
|
|
<span class="sd"> :keyword cluster: If distributed='dispy' the list of cluster nodes, else if distributed='spark' it is the master node</span>
|
|
<span class="sd"> :return: the best genotype</span>
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">file</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'file'</span><span class="p">,</span> <span class="s1">'hyperparam.db'</span><span class="p">)</span>
|
|
|
|
<span class="n">conn</span> <span class="o">=</span> <span class="n">hUtil</span><span class="o">.</span><span class="n">open_hyperparam_db</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
|
|
|
|
<span class="n">experiments</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'experiments'</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
|
|
|
|
<span class="n">distributed</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'distributed'</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
|
|
|
|
<span class="n">fts_method</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'fts_method'</span><span class="p">,</span> <span class="n">hofts</span><span class="o">.</span><span class="n">WeightedHighOrderFTS</span><span class="p">)</span>
|
|
<span class="n">shortname</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">fts_method</span><span class="o">.</span><span class="vm">__module__</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">'dispy'</span><span class="p">:</span>
|
|
<span class="kn">from</span> <span class="nn">pyFTS.distributed</span> <span class="k">import</span> <span class="n">dispy</span> <span class="k">as</span> <span class="n">dUtil</span>
|
|
<span class="n">nodes</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'nodes'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'127.0.0.1'</span><span class="p">])</span>
|
|
<span class="n">cluster</span><span class="p">,</span> <span class="n">http_server</span> <span class="o">=</span> <span class="n">dUtil</span><span class="o">.</span><span class="n">start_dispy_cluster</span><span class="p">(</span><span class="n">evaluate</span><span class="p">,</span> <span class="n">nodes</span><span class="o">=</span><span class="n">nodes</span><span class="p">)</span>
|
|
<span class="n">kwargs</span><span class="p">[</span><span class="s1">'cluster'</span><span class="p">]</span> <span class="o">=</span> <span class="n">cluster</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">experiments</span><span class="p">):</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"Experiment </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
|
|
|
|
<span class="n">start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
|
|
<span class="n">ret</span><span class="p">,</span> <span class="n">statistics</span> <span class="o">=</span> <span class="n">GeneticAlgorithm</span><span class="p">(</span><span class="n">dataset</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="n">end</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
|
|
<span class="n">ret</span><span class="p">[</span><span class="s1">'time'</span><span class="p">]</span> <span class="o">=</span> <span class="n">end</span> <span class="o">-</span> <span class="n">start</span>
|
|
<span class="n">experiment</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'individual'</span><span class="p">:</span> <span class="n">ret</span><span class="p">,</span> <span class="s1">'statistics'</span><span class="p">:</span> <span class="n">statistics</span><span class="p">}</span>
|
|
|
|
<span class="n">ret</span> <span class="o">=</span> <span class="n">process_experiment</span><span class="p">(</span><span class="n">shortname</span><span class="p">,</span> <span class="n">experiment</span><span class="p">,</span> <span class="n">datasetname</span><span class="p">,</span> <span class="n">conn</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">distributed</span> <span class="o">==</span> <span class="s1">'dispy'</span><span class="p">:</span>
|
|
<span class="n">dUtil</span><span class="o">.</span><span class="n">stop_dispy_cluster</span><span class="p">(</span><span class="n">cluster</span><span class="p">,</span> <span class="n">http_server</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret</span></div>
|
|
</pre></div>
|
|
|
|
<div class="clearer"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<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>$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../../../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li class="right" >
|
|
<a href="../../../py-modindex.html" title="Python Module Index"
|
|
>modules</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="../../../index.html">pyFTS 1.7 documentation</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">pyFTS.hyperparam.Evolutionary</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2022, Machine Intelligence and Data Science Laboratory - UFMG - Brazil.
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
|
|
</div>
|
|
</body>
|
|
</html> |