Skip to content

Commit

Permalink
deploy: 8ae1f55
Browse files Browse the repository at this point in the history
  • Loading branch information
SevgiAkten committed Nov 12, 2024
1 parent 8161e61 commit 08990e0
Show file tree
Hide file tree
Showing 43 changed files with 4,158 additions and 1,249 deletions.
87 changes: 69 additions & 18 deletions _modules/pycellga/problems/abstract_problem.html
Original file line number Diff line number Diff line change
Expand Up @@ -326,40 +326,91 @@
<div itemprop="articleBody">

<h1>Source code for pycellga.problems.abstract_problem</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABC</span><span class="p">,</span> <span class="n">abstractmethod</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">Any</span>
<span class="kn">from</span> <span class="nn">pymoo.core.problem</span> <span class="kn">import</span> <span class="n">Problem</span>

<div class="viewcode-block" id="AbstractProblem">
<a class="viewcode-back" href="../../../pycellga.problems.html#pycellga.problems.abstract_problem.AbstractProblem">[docs]</a>
<span></span><span class="k">class</span> <span class="nc">AbstractProblem</span><span class="p">:</span>
<span class="k">class</span> <span class="nc">AbstractProblem</span><span class="p">(</span><span class="n">Problem</span><span class="p">,</span> <span class="n">ABC</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> An abstract base class for optimization problems.</span>

<span class="sd"> Methods</span>
<span class="sd"> -------</span>
<span class="sd"> f(x)</span>
<span class="sd"> Evaluates the fitness of a given solution x.</span>
<span class="sd"> Abstract base class for optimization problems.</span>
<span class="sd"> &quot;&quot;&quot;</span>

<div class="viewcode-block" id="AbstractProblem.__init__">
<a class="viewcode-back" href="../../../pycellga.problems.html#pycellga.problems.abstract_problem.AbstractProblem.__init__">[docs]</a>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">design_variables</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]],</span>
<span class="n">bounds</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">float</span><span class="p">]],</span>
<span class="n">objectives</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]],</span>
<span class="n">constraints</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="p">[]):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Initialize the problem with variables, bounds, objectives, and constraints.</span>
<span class="sd"> </span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> design_variables : int or List[str]</span>
<span class="sd"> If an integer, it specifies the number of design variables. </span>
<span class="sd"> If a list of strings, it specifies the names of design variables.</span>
<span class="sd"> bounds : List[Tuple[float, float]]</span>
<span class="sd"> Bounds for each design variable as (min, max).</span>
<span class="sd"> objectives : str, int, or List[str]</span>
<span class="sd"> Objectives for optimization, e.g., &quot;minimize&quot; or &quot;maximize&quot;.</span>
<span class="sd"> constraints : str, int, or List[str], optional</span>
<span class="sd"> Constraints for the problem (default is an empty list).</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Ensure objectives and constraints are always lists</span>
<span class="n">objectives</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">objectives</span><span class="p">)]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">objectives</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">))</span> <span class="k">else</span> <span class="nb">list</span><span class="p">(</span><span class="n">objectives</span><span class="p">)</span>
<span class="n">constraints</span> <span class="o">=</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">constraints</span><span class="p">)]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">constraints</span><span class="p">,</span> <span class="p">(</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">))</span> <span class="k">else</span> <span class="nb">list</span><span class="p">(</span><span class="n">constraints</span><span class="p">)</span>

<span class="c1"># Pymoo-specific attributes</span>
<span class="n">n_var</span> <span class="o">=</span> <span class="n">design_variables</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">design_variables</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="k">else</span> <span class="nb">len</span><span class="p">(</span><span class="n">design_variables</span><span class="p">)</span>
<span class="n">xl</span> <span class="o">=</span> <span class="p">[</span><span class="n">bound</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">bound</span> <span class="ow">in</span> <span class="n">bounds</span><span class="p">]</span>
<span class="n">xu</span> <span class="o">=</span> <span class="p">[</span><span class="n">bound</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">bound</span> <span class="ow">in</span> <span class="n">bounds</span><span class="p">]</span>

<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">n_var</span><span class="o">=</span><span class="n">n_var</span><span class="p">,</span> <span class="n">n_obj</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">objectives</span><span class="p">),</span> <span class="n">n_constr</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="n">constraints</span><span class="p">),</span> <span class="n">xl</span><span class="o">=</span><span class="n">xl</span><span class="p">,</span> <span class="n">xu</span><span class="o">=</span><span class="n">xu</span><span class="p">)</span>

<span class="c1"># Custom attributes</span>
<span class="bp">self</span><span class="o">.</span><span class="n">design_variables</span> <span class="o">=</span> <span class="p">[</span><span class="sa">f</span><span class="s2">&quot;x</span><span class="si">{</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="si">}</span><span class="s2">&quot;</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_var</span><span class="p">)]</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">design_variables</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span> <span class="k">else</span> <span class="n">design_variables</span>
<span class="bp">self</span><span class="o">.</span><span class="n">bounds</span> <span class="o">=</span> <span class="n">bounds</span>
<span class="bp">self</span><span class="o">.</span><span class="n">objectives</span> <span class="o">=</span> <span class="n">objectives</span>
<span class="bp">self</span><span class="o">.</span><span class="n">constraints</span> <span class="o">=</span> <span class="n">constraints</span></div>


<div class="viewcode-block" id="AbstractProblem.f">
<a class="viewcode-back" href="../../../pycellga.problems.html#pycellga.problems.abstract_problem.AbstractProblem.f">[docs]</a>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="nd">@abstractmethod</span>
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Any</span><span class="p">])</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Evaluate the fitness of a given solution x.</span>

<span class="sd"> Abstract method for evaluating the fitness of a solution.</span>
<span class="sd"> </span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> x : list</span>
<span class="sd"> A list representing a candidate solution.</span>

<span class="sd"> List of design variable values.</span>
<span class="sd"> </span>
<span class="sd"> Returns</span>
<span class="sd"> -------</span>
<span class="sd"> float</span>
<span class="sd"> The fitness value of the candidate solution.</span>
<span class="sd"> Fitness value.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Subclasses should implement this method.&quot;</span><span class="p">)</span></div>

<span class="sd"> Raises</span>
<span class="sd"> ------</span>
<span class="sd"> NotImplementedError</span>
<span class="sd"> If the method is not implemented by a subclass.</span>

<div class="viewcode-block" id="AbstractProblem.evaluate">
<a class="viewcode-back" href="../../../pycellga.problems.html#pycellga.problems.abstract_problem.AbstractProblem.evaluate">[docs]</a>
<span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Evaluate function for compatibility with pymoo&#39;s optimizer.</span>
<span class="sd"> </span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> x : numpy.ndarray</span>
<span class="sd"> Array of input variables.</span>
<span class="sd"> out : dict</span>
<span class="sd"> Dictionary to store the output fitness values.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
<span class="n">out</span><span class="p">[</span><span class="s2">&quot;F&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span></div>
</div>

</pre></div>
Expand Down
Loading

0 comments on commit 08990e0

Please sign in to comment.