Skip to content

Commit

Permalink
create geneticalgorithm2.utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
PasaOpasen committed Apr 13, 2024
1 parent f03006a commit e86e28a
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pip install geneticalgorithm2[full]
- recreate the repository without excess heavy files materials
- host the [code documentation](https://pasaopasen.github.io/geneticalgorithm2/)
- rename `geneticalgorithm2` class to `GeneticAlgorithm2`
- substantial package architecture refactor

## 6.8.7 minor update

Expand Down
4 changes: 2 additions & 2 deletions geneticalgorithm2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

from .initializer import Population_initializer

from .cache import np_lru_cache
from .callbacks import Callbacks, Actions, ActionConditions, MiddleCallbacks

from .plotting_tools import plot_pop_scores, plot_several_lines
from .utils.cache import np_lru_cache
from .utils.plotting import plot_pop_scores, plot_several_lines



Expand Down
8 changes: 4 additions & 4 deletions geneticalgorithm2/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

from OppOpPopInit import OppositionOperators, SampleInitializers

from .aliases import TypeAlias, array1D, array2D, PathLike
from .files import mkdir_of_file, mkdir
from .utils.aliases import TypeAlias, array1D, array2D, PathLike
from .utils.files import mkdir
from .utils.funcs import union_to_matrix, fast_max

from .classes import MiddleCallbackData, Generation

from .utils import union_to_matrix, fast_max

from .crossovers import CrossoverFunc
from .selections import SelectionFunc
Expand Down Expand Up @@ -270,7 +270,7 @@ def PlotPopulationScores(
plots population scores
needs 2 functions like data->str for title and file name
"""
from .plotting_tools import plot_pop_scores
from .utils.plotting import plot_pop_scores

use_save_as = (lambda data: None) if save_as_name_pattern is None else save_as_name_pattern

Expand Down
6 changes: 3 additions & 3 deletions geneticalgorithm2/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import numpy as np

from .aliases import array1D, array2D, TypeAlias, PathLike
from .files import mkdir_of_file
from .utils.aliases import array1D, array2D, TypeAlias, PathLike
from .utils.files import mkdir_of_file
from .utils.funcs import can_be_prob, union_to_matrix

from .crossovers import Crossover, CrossoverFunc
from .mutations import Mutations, MutationIntFunc, MutationFloatFunc
from .selections import Selection, SelectionFunc

from .utils import can_be_prob, union_to_matrix


class DictLikeGetSet:
Expand Down
2 changes: 1 addition & 1 deletion geneticalgorithm2/crossovers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import random
import numpy as np

from .aliases import TypeAlias, array1D
from .utils.aliases import TypeAlias, array1D

CrossoverFunc: TypeAlias = Callable[[array1D, array1D], Tuple[array1D, array1D]]

Expand Down
6 changes: 3 additions & 3 deletions geneticalgorithm2/geneticalgorithm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

#region INTERNAL IMPORTS

from .aliases import array1D, array2D
from .utils.aliases import array1D, array2D

from .classes import AlgorithmParams, Generation, MiddleCallbackData, GAResult, GenerationConvertible

from .initializer import Population_initializer
from .plotting_tools import plot_pop_scores, plot_several_lines
from .utils.plotting import plot_pop_scores, plot_several_lines

from .utils import can_be_prob, is_numpy, is_current_gen_number, fast_min, random_indexes_pair
from .utils.funcs import can_be_prob, is_numpy, is_current_gen_number, fast_min, random_indexes_pair

from .callbacks import MiddleCallbackFunc, CallbackFunc

Expand Down
2 changes: 1 addition & 1 deletion geneticalgorithm2/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from .aliases import TypeAlias, array1D, array2D
from .utils.aliases import TypeAlias, array1D, array2D


LOCAL_OPT_STEPS: TypeAlias = Literal['before_select', 'after_select', 'never']
Expand Down
4 changes: 2 additions & 2 deletions geneticalgorithm2/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import numpy as np


from .utils import fast_min, fast_max
from .utils.aliases import TypeAlias
from .utils.funcs import fast_min, fast_max

from .aliases import TypeAlias

MutationFloatFunc: TypeAlias = Callable[[float, float, float], float]
MutationIntFunc: TypeAlias = Callable[[int, int, int], int]
Expand Down
2 changes: 1 addition & 1 deletion geneticalgorithm2/selections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
import numpy as np

from .aliases import array1D, TypeAlias
from .utils.aliases import array1D, TypeAlias

SelectionFunc: TypeAlias = Callable[[array1D, int], array1D]

Expand Down
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#from fastcache import clru_cache

from .aliases import array1D
from geneticalgorithm2.utils.aliases import array1D


def np_lru_cache(*args, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pathlib import Path

from .aliases import PathLike
from geneticalgorithm2.utils.aliases import PathLike


def _mkdir(path: Path):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

from typing import Optional, Any, Tuple

from pathlib import Path

import random
import numpy as np

from .aliases import array1D, array2D
from geneticalgorithm2.utils.aliases import array1D, array2D


def fast_min(a, b):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from .aliases import PathLike
from geneticalgorithm2.utils.aliases import PathLike
from .files import mkdir_of_file


Expand Down Expand Up @@ -51,6 +51,7 @@ def plot_several_lines(

plt.show()


def plot_pop_scores(scores: Sequence[float], title: str = 'Population scores', save_as: Optional[str] = None):
"""
plots scores (numeric values) as sorted bars
Expand All @@ -76,8 +77,6 @@ def autolabel(rects):
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=14, fontweight='bold')



cols = np.zeros(len(sc))
cols[-1] = 1
Expand All @@ -96,7 +95,6 @@ def autolabel(rects):
#ax.set_ylim([0, max(subdict.values())*1.2])
#fig.suptitle(title, fontsize=15, fontweight='bold')


fig.tight_layout()

if save_as is not None:
Expand Down

0 comments on commit e86e28a

Please sign in to comment.