OppOpPopInit


NameOppOpPopInit JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/PasaOpasen/opp-op-pop-init
SummaryPyPI package containing opposition learning operators and population initializers for evolutionary algorithms
upload_time2023-01-06 15:34:49
maintainerDemetry Pascal
docs_urlNone
authorDemetry Pascal
requires_python
license
keywords opposition-based-learning optimization evolutionary algorithms fast easy evolution generator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Opposition learning operators and population initializers

[![PyPI
version](https://badge.fury.io/py/OppOpPopInit.svg)](https://pypi.org/project/OppOpPopInit/)
[![Downloads](https://pepy.tech/badge/oppoppopinit)](https://pepy.tech/project/oppoppopinit)
[![Downloads](https://pepy.tech/badge/oppoppopinit/month)](https://pepy.tech/project/oppoppopinit)
[![Downloads](https://pepy.tech/badge/oppoppopinit/week)](https://pepy.tech/project/oppoppopinit) 

[![Join the chat at https://gitter.im/opp-op-pop-init/community](https://badges.gitter.im/opp-op-pop-init/community.svg)](https://gitter.im/opp-op-pop-init/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Documentation: https://pasaopasen.github.io/opp-op-pop-init/

**OPP**osition learning **OP**erators and **POP**ulation **INIT**ializers (from [DPEA](https://github.com/PasaOpasen/PasaOpasen.github.io/blob/master/EA_packages.md)) is the python package containing opposition learning operators and population initializers for evolutionary algorithms.


- [Opposition learning operators and population initializers](#opposition-learning-operators-and-population-initializers)
  - [Installation](#installation)
  - [About opposition operators](#about-opposition-operators)
  - [Imports](#imports)
  - [Interface agreements](#interface-agreements)
    - [Borders](#borders)
    - [Format of **creators** and **oppositors**](#format-of-creators-and-oppositors)
  - [Available opposition operators](#available-opposition-operators)
    - [Checklist](#checklist)
    - [Examples](#examples)
      - [`abs` oppositor](#abs-oppositor)
      - [`modular` oppositor](#modular-oppositor)
      - [`quasi` oppositor](#quasi-oppositor)
      - [`quasi_reflect` oppositor](#quasi_reflect-oppositor)
      - [`over` oppositor](#over-oppositor)
      - [`integers_by_order` oppositor](#integers_by_order-oppositor)
      - [More examples](#more-examples)
    - [Partial/Combined oppositor](#partialcombined-oppositor)
    - [RandomPartialOppositor](#randompartialoppositor)
    - [Reflect method](#reflect-method)
    - [Reflection with selection best](#reflection-with-selection-best)
  - [Population initializers](#population-initializers)
    - [Simple random populations](#simple-random-populations)
      - [`RandomInteger`](#randominteger)
      - [`Uniform`](#uniform)
      - [`Normal`](#normal)
      - [`Mixed`](#mixed)
    - [Populations with oppositions](#populations-with-oppositions)

## Installation

```
pip install OppOpPopInit
```

or 

```
pip3 install OppOpPopInit
```

## About opposition operators

In several evolutionary algorithms it can be useful to create the opposite of some part of current population to explore searching space better. Usually it uses at the begging of searching process (with first population initialization) and every few generations with decreasing probability `F`. Also it's better to create oppositions of worse objects from populations. See [this article](https://www.google.ru/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwi5kN-7gdDtAhUklIsKHRFnC7wQFjAAegQIAxAC&url=https%3A%2F%2Fwww.researchgate.net%2Fprofile%2FMohamed_Mourad_Lafifi%2Fpost%2FCan_anybody_please_send_me_MatLab_code_for_oppotion_based_PSO_BBO%2Fattachment%2F5f5be101e66b860001a0f71c%2FAS%253A934681725919233%25401599856897644%2Fdownload%2FOpposition%2Bbased%2Blearning%2BA%2Bliterature%2Breview.pdf&usg=AOvVaw02oywUU7ZaSWH24jkmNxPu) for more information. 

This package provides several operators for creating oppositions (**opposition operators**) and methods for creating start population using different distribution functions and opposition operators *for each dimension*!

## Imports

What can u import from this package:

```python
from OppOpPopInit import OppositionOperators # available opposition operators as static class
from OppOpPopInit import SampleInitializers  # available population initializers as static class

# main function which creates random population
# using initializers and oppositors
from OppOpPopInit import init_population 
```

Also there is a little module for plotting pictures like u can see below

```python
import OppOpPopInit.plotting
```

## Interface agreements

### Borders

In the entire part of math optimization tasks the best skin of possible solutions is using 1D-vectors `(x1, x2, ..., xn)` of variables where each variables `x` can be from some area `[xmin, xmax]`. [geneticalgorithm2 package](https://github.com/PasaOpasen/geneticalgorithm2) and this package are both geared towards this case.

So, many functions here takes arguments `minimums` and `maximums` which mean the lower and the upper bounds (borders) of available areas by each using dimension. `minimums` and `maximums` should be sequences of integer or real numbers or only one number, but they cannot be numbers both and they must have equal length if they are sequence both.

### Format of **creators** and **oppositors**

**Creators** are functions which create random samples (depended on bounds). In code a creator can be any object who can be called like function with signature `() -> np.ndarray` (`Callable[[], np.ndarray]`).

**Oppositors** are functions which take `np.ndarray` samples and return it's opposed form as `np.ndarray`. So in code an oppositor if the object can be called like `(np.ndarray) -> np.ndarray` (`Callable[[np.ndarray], np.ndarray]`).

## Available opposition operators

### Checklist

*`OppositionOperators.Continual.abs`

*`OppositionOperators.Continual.modular`

*`OppositionOperators.Continual.quasi`

*`OppositionOperators.Continual.quasi_reflect`

*`OppositionOperators.Continual.over`

*`OppositionOperators.Continual.Partial` -- for using several opposition operators for each subset of searching area.

*`OppositionOperators.Discrete.integers_by_order` -- it's like `abs` operator but for integer values

*`OppositionOperators.CombinedOppositor` -- for using existing opposition operators for each dimension with continual or mixed task. See example[below](#partial-oppositor)

U can create your own oppositor using pattern:

```python
def oppositor(sample: np.ndarray) -> np.ndarray:
  # some code
  return new_sample
```

There are also `OppositionOperators.Discrete._index_by_order` and `OppositionOperators.Discrete.value_by_order` constructors for very special discrete tasks with available sets of valid values (like `[-1, 0, 1, 5, 15]`), but it's highly recommended to convert this task to indexes array task (and use `OppositionOperators.Discrete.integers_by_order`) like below:

```python
# available values
vals = np.array([1, 90. -45, 3, 0.7, 3.4, 12])

valid_array_example = np.array([1, 1, 1, 3, -45])

# function
def optimized_func(arr: np.ndarray) -> float:
    #some code
    return result

# recommented way for optimization algorithm
indexes = np.arange(vals.size)

def new_optimized_functio(new_arr):
    arr = np.array([vals[i] for i in new_arr])
    return optimized_func(arr)

# and forth u are using indexes format for your population

print(
  new_optimized_functio(
    np.array([0, 0, 1, 4])
  )
)
```

### Examples

#### `abs` oppositor

[Code](tests/op_abs.py)
![](tests/output/abs.png)
        


#### `modular` oppositor

[Code](tests/op_modular.py)
![](tests/output/modular.png)
        


#### `quasi` oppositor

[Code](tests/op_quasi.py)
![](tests/output/quasi.png)
        


#### `quasi_reflect` oppositor

[Code](tests/op_quasi_reflect.py)
![](tests/output/quasi_reflect.png)
        


#### `over` oppositor

[Code](tests/op_over.py)
![](tests/output/over.png)
        

#### `integers_by_order` oppositor

[Code](tests/op_integers_by_order.py)
![](tests/output/integers_by_order.png)


#### More examples

[Code](tests/more_examples.py)

![](tests/output/more_1.png)
![](tests/output/more_2.png)
![](tests/output/more_3.png)
![](tests/output/more_4.png)
![](tests/output/more_5.png)

### Partial/Combined oppositor

If u want to use some oppositor to one dimenstion subset (e. g. indexes 0, 1, 3) and other oppositors for other subsets (e. g. indexes 2, 4, 5) -- u need to create `Partial` or `Combined` oppositors. The difference between them is that u need existing oppositors to make combined oppositor with them and u need oppositor makers to make partial oppositor. So, `Partial` oppositor is often easier to use but `Combined` is more flexible.

To create `Combined` oppositor use code like:

```python
oppositor = OppositionOperators.CombinedOppositor(
  [
    (sequece of indexes to apply, oppositor_for_this_dimentions),
    (sequece of indexes to apply, oppositor_for_this_dimentions),
    ...
    (sequece of indexes to apply, oppositor_for_this_dimentions)
  ]
)
```

To create `Partial` oppositor use code:

```python
oppositor = OppositionOperators.PartialOppositor(
    minimums=minimumns,
    maximums=maximums,
    indexes_to_opp_creator=[
        (sequece of indexes to apply, oppositor_creator),
        (sequece of indexes to apply, oppositor_creator)
    ]
)
```


Example:

```python
import numpy as np
from OppOpPopInit import OppositionOperators

# 5 dim population

min_bound = np.array([-8, -3, -5.7, 0, 0])
max_bound = np.array([5, 4, 4, 9, 9])

# population points
points = np.array([
  [1, 2, 3, 4, 7.5],
  [1.6, -2, 3.9, 0.4, 5],
  [1.1, 3.2, -3, 4, 5],
  [4.1, 2, 3, -4, 0.5]
])

# saved indexes for oppositors
first_op_indexes = np.array([0, 2])
second_op_indexes = np.array([1, 3])

oppositor = OppositionOperators.CombinedOppositor(
  [
    (first_op_indexes, OppositionOperators.Continual.abs(
      minimums=min_bound[first_op_indexes],
      maximums=max_bound[first_op_indexes],
    )),
    (second_op_indexes, OppositionOperators.Continual.over(
      minimums=min_bound[second_op_indexes],
      maximums=max_bound[second_op_indexes],
    ))
  ]
)

# or 

oppositor = OppositionOperators.PartialOppositor(
    minimums=min_bound,
    maximums=max_bound,
    indexes_to_opp_creator=[
        (first_op_indexes, OppositionOperators.Continual.abs),
        (second_op_indexes, OppositionOperators.Continual.over)
    ]
)

# as u see, it's not necessary to oppose population by all dimensions, here we won't oppose by last dimension

oppositions = OppositionOperators.Reflect(points, oppositor)

print(oppositions)

# array([[-4.        ,  1.84589799, -4.7       ,  5.04795851,  7.5       ],
#       [-4.6       , -0.74399971, -5.6       ,  7.49178902,  5.        ],
#       [-4.1       ,  0.54619162,  1.3       ,  6.14214043,  5.        ],
#       [-7.1       , -2.59648698, -4.7       ,  0.95770904,  0.5       ]])

```

[Another example code](tests/op_mixed.py)
![](tests/output/mixed.png)


### RandomPartialOppositor

One of the most amazing feature of this package is `RandomPartialOppositor`. It lets u apply several oppositors to random subsets of dimension area and change these subsets after some counts after applying. It means that u can apply only one this oppositor to several samples and get result like u applyed several partial oppositors to parts of this samples.

Create `RandomPartialOppositor` oppositor using this structure:

```python
oppositor = OppositionOperators.RandomPartialOppositor(
    [
        (count_of_random_dimensions, repeate_config_during_steps, avalable_indexes, oppositor_creator),
        (count_of_random_dimensions, repeate_config_during_steps, avalable_indexes, oppositor_creator),
        ...
        (count_of_random_dimensions, repeate_config_during_steps, avalable_indexes, oppositor_creator)
    ],
    minimums,
    maximums
)
```

See [simplest example of using](tests/random%20partial%20oppositor.py)


### Reflect method

Use `OppositionOperators.Reflect(samples, oppositor)` for oppose samples array using some oppositor. `samples` argument here is 2D-array with size samples*dimension.

### Reflection with selection best

There is `OppositionOperators.ReflectWithSelectionBest(population_samples, oppositor, eval_func, samples_scores = None, more_is_better = False)` method for reflect population (with size `N`) and select best `N` objects from existing `2N` objects. It has parameters:

* `population_samples` : 2D numpy array;
            reflected population.
* `oppositor` : function;
            applying oppositor.
* `eval_func` : function;
            optimized function of population/task.
* `samples_scores` : `None`/1D numpy array, optional;
            scores for reflected population (if calculated -- it's not necessary to calculate it again). The default is `None`.
* `more_is_better` : logical, optional;
            The goal -- is maximize the function. The default is `False`.

See [example](tests/reflection_with_selection.py)

![](tests/output/reflection_with_selection_before.png)
![](tests/output/reflection_with_selection_after.png)

## Population initializers

### Simple random populations

Like `oppositors operators` there are some constructors for creating creators of start population:

* `SampleInitializers.RandomInteger(minimums, maximums)` -- returns function which will return random integer vectors between `minimums` and `maximums`
* `SampleInitializers.Uniform(minimums, maximums)` -- returns function which will return random vectors between `minimums` and `maximums` from uniform distribution
* `SampleInitializers.Normal(minimums, maximums, sd = None)` -- returns function which will return random vectors between `minimums` and `maximums` from normal distribution

U can create your initializer function:
```python
def func() -> np.ndarray:
    # code
    return valid_sample_array 
```

There is also `SampleInitializers.Combined(minimums, maximums, indexes, creator_initializers)` for generate population with [different constructors for each dimension subset](#mixed)!

Use `creator` for initialize population with `k` objects using `SampleInitializers.CreateSamples(creator, k)`.

#### `RandomInteger`

[Code](tests/random_int_pop.py)
![](tests/output/random_int_pop.png)

#### `Uniform`

[Code](tests/random_uniform_pop.py)
![](tests/output/random_uniform_pop.png)

#### `Normal`

[Code](tests/random_normal_pop.py)
![](tests/output/random_normal_pop.png)

#### `Mixed`

[Code](tests/random_mixed_pop.py)
![](tests/output/random_mixed_pop.png)


### Populations with oppositions

Use `init_population(total_count, creator, oppositors = None)` to create population of size `total_count` where some objects are constructed by `creator` and other objects are constructed by applying each oppositor from `oppositors` to start objects.

[Code](tests/pop_with_oppositors.py)
![](tests/output/pop_with_op.png)
![](tests/output/pop_with_op2.png)
![](tests/output/pop_with_op3.png)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PasaOpasen/opp-op-pop-init",
    "name": "OppOpPopInit",
    "maintainer": "Demetry Pascal",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "opposition-based-learning,optimization,evolutionary algorithms,fast,easy,evolution,generator",
    "author": "Demetry Pascal",
    "author_email": "qtckpuhdsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/59/3e/59fa1a994f1ec6fe85539293e8dcd363cefb4ee04d7ae2d35591cb437e5a/OppOpPopInit-2.0.1.tar.gz",
    "platform": null,
    "description": "\n# Opposition learning operators and population initializers\n\n[![PyPI\nversion](https://badge.fury.io/py/OppOpPopInit.svg)](https://pypi.org/project/OppOpPopInit/)\n[![Downloads](https://pepy.tech/badge/oppoppopinit)](https://pepy.tech/project/oppoppopinit)\n[![Downloads](https://pepy.tech/badge/oppoppopinit/month)](https://pepy.tech/project/oppoppopinit)\n[![Downloads](https://pepy.tech/badge/oppoppopinit/week)](https://pepy.tech/project/oppoppopinit) \n\n[![Join the chat at https://gitter.im/opp-op-pop-init/community](https://badges.gitter.im/opp-op-pop-init/community.svg)](https://gitter.im/opp-op-pop-init/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)\n\nDocumentation: https://pasaopasen.github.io/opp-op-pop-init/\n\n**OPP**osition learning **OP**erators and **POP**ulation **INIT**ializers (from [DPEA](https://github.com/PasaOpasen/PasaOpasen.github.io/blob/master/EA_packages.md)) is the python package containing opposition learning operators and population initializers for evolutionary algorithms.\n\n\n- [Opposition learning operators and population initializers](#opposition-learning-operators-and-population-initializers)\n  - [Installation](#installation)\n  - [About opposition operators](#about-opposition-operators)\n  - [Imports](#imports)\n  - [Interface agreements](#interface-agreements)\n    - [Borders](#borders)\n    - [Format of **creators** and **oppositors**](#format-of-creators-and-oppositors)\n  - [Available opposition operators](#available-opposition-operators)\n    - [Checklist](#checklist)\n    - [Examples](#examples)\n      - [`abs` oppositor](#abs-oppositor)\n      - [`modular` oppositor](#modular-oppositor)\n      - [`quasi` oppositor](#quasi-oppositor)\n      - [`quasi_reflect` oppositor](#quasi_reflect-oppositor)\n      - [`over` oppositor](#over-oppositor)\n      - [`integers_by_order` oppositor](#integers_by_order-oppositor)\n      - [More examples](#more-examples)\n    - [Partial/Combined oppositor](#partialcombined-oppositor)\n    - [RandomPartialOppositor](#randompartialoppositor)\n    - [Reflect method](#reflect-method)\n    - [Reflection with selection best](#reflection-with-selection-best)\n  - [Population initializers](#population-initializers)\n    - [Simple random populations](#simple-random-populations)\n      - [`RandomInteger`](#randominteger)\n      - [`Uniform`](#uniform)\n      - [`Normal`](#normal)\n      - [`Mixed`](#mixed)\n    - [Populations with oppositions](#populations-with-oppositions)\n\n## Installation\n\n```\npip install OppOpPopInit\n```\n\nor \n\n```\npip3 install OppOpPopInit\n```\n\n## About opposition operators\n\nIn several evolutionary algorithms it can be useful to create the opposite of some part of current population to explore searching space better. Usually it uses at the begging of searching process (with first population initialization) and every few generations with decreasing probability `F`. Also it's better to create oppositions of worse objects from populations. See [this article](https://www.google.ru/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwi5kN-7gdDtAhUklIsKHRFnC7wQFjAAegQIAxAC&url=https%3A%2F%2Fwww.researchgate.net%2Fprofile%2FMohamed_Mourad_Lafifi%2Fpost%2FCan_anybody_please_send_me_MatLab_code_for_oppotion_based_PSO_BBO%2Fattachment%2F5f5be101e66b860001a0f71c%2FAS%253A934681725919233%25401599856897644%2Fdownload%2FOpposition%2Bbased%2Blearning%2BA%2Bliterature%2Breview.pdf&usg=AOvVaw02oywUU7ZaSWH24jkmNxPu) for more information. \n\nThis package provides several operators for creating oppositions (**opposition operators**) and methods for creating start population using different distribution functions and opposition operators *for each dimension*!\n\n## Imports\n\nWhat can u import from this package:\n\n```python\nfrom OppOpPopInit import OppositionOperators # available opposition operators as static class\nfrom OppOpPopInit import SampleInitializers  # available population initializers as static class\n\n# main function which creates random population\n# using initializers and oppositors\nfrom OppOpPopInit import init_population \n```\n\nAlso there is a little module for plotting pictures like u can see below\n\n```python\nimport OppOpPopInit.plotting\n```\n\n## Interface agreements\n\n### Borders\n\nIn the entire part of math optimization tasks the best skin of possible solutions is using 1D-vectors `(x1, x2, ..., xn)` of variables where each variables `x` can be from some area `[xmin, xmax]`. [geneticalgorithm2 package](https://github.com/PasaOpasen/geneticalgorithm2) and this package are both geared towards this case.\n\nSo, many functions here takes arguments `minimums` and `maximums` which mean the lower and the upper bounds (borders) of available areas by each using dimension. `minimums` and `maximums` should be sequences of integer or real numbers or only one number, but they cannot be numbers both and they must have equal length if they are sequence both.\n\n### Format of **creators** and **oppositors**\n\n**Creators** are functions which create random samples (depended on bounds). In code a creator can be any object who can be called like function with signature `() -> np.ndarray` (`Callable[[], np.ndarray]`).\n\n**Oppositors** are functions which take `np.ndarray` samples and return it's opposed form as `np.ndarray`. So in code an oppositor if the object can be called like `(np.ndarray) -> np.ndarray` (`Callable[[np.ndarray], np.ndarray]`).\n\n## Available opposition operators\n\n### Checklist\n\n*`OppositionOperators.Continual.abs`\n\n*`OppositionOperators.Continual.modular`\n\n*`OppositionOperators.Continual.quasi`\n\n*`OppositionOperators.Continual.quasi_reflect`\n\n*`OppositionOperators.Continual.over`\n\n*`OppositionOperators.Continual.Partial` -- for using several opposition operators for each subset of searching area.\n\n*`OppositionOperators.Discrete.integers_by_order` -- it's like `abs` operator but for integer values\n\n*`OppositionOperators.CombinedOppositor` -- for using existing opposition operators for each dimension with continual or mixed task. See example[below](#partial-oppositor)\n\nU can create your own oppositor using pattern:\n\n```python\ndef oppositor(sample: np.ndarray) -> np.ndarray:\n  # some code\n  return new_sample\n```\n\nThere are also `OppositionOperators.Discrete._index_by_order` and `OppositionOperators.Discrete.value_by_order` constructors for very special discrete tasks with available sets of valid values (like `[-1, 0, 1, 5, 15]`), but it's highly recommended to convert this task to indexes array task (and use `OppositionOperators.Discrete.integers_by_order`) like below:\n\n```python\n# available values\nvals = np.array([1, 90. -45, 3, 0.7, 3.4, 12])\n\nvalid_array_example = np.array([1, 1, 1, 3, -45])\n\n# function\ndef optimized_func(arr: np.ndarray) -> float:\n    #some code\n    return result\n\n# recommented way for optimization algorithm\nindexes = np.arange(vals.size)\n\ndef new_optimized_functio(new_arr):\n    arr = np.array([vals[i] for i in new_arr])\n    return optimized_func(arr)\n\n# and forth u are using indexes format for your population\n\nprint(\n  new_optimized_functio(\n    np.array([0, 0, 1, 4])\n  )\n)\n```\n\n### Examples\n\n#### `abs` oppositor\n\n[Code](tests/op_abs.py)\n![](tests/output/abs.png)\n        \n\n\n#### `modular` oppositor\n\n[Code](tests/op_modular.py)\n![](tests/output/modular.png)\n        \n\n\n#### `quasi` oppositor\n\n[Code](tests/op_quasi.py)\n![](tests/output/quasi.png)\n        \n\n\n#### `quasi_reflect` oppositor\n\n[Code](tests/op_quasi_reflect.py)\n![](tests/output/quasi_reflect.png)\n        \n\n\n#### `over` oppositor\n\n[Code](tests/op_over.py)\n![](tests/output/over.png)\n        \n\n#### `integers_by_order` oppositor\n\n[Code](tests/op_integers_by_order.py)\n![](tests/output/integers_by_order.png)\n\n\n#### More examples\n\n[Code](tests/more_examples.py)\n\n![](tests/output/more_1.png)\n![](tests/output/more_2.png)\n![](tests/output/more_3.png)\n![](tests/output/more_4.png)\n![](tests/output/more_5.png)\n\n### Partial/Combined oppositor\n\nIf u want to use some oppositor to one dimenstion subset (e. g. indexes 0, 1, 3) and other oppositors for other subsets (e. g. indexes 2, 4, 5) -- u need to create `Partial` or `Combined` oppositors. The difference between them is that u need existing oppositors to make combined oppositor with them and u need oppositor makers to make partial oppositor. So, `Partial` oppositor is often easier to use but `Combined` is more flexible.\n\nTo create `Combined` oppositor use code like:\n\n```python\noppositor = OppositionOperators.CombinedOppositor(\n  [\n    (sequece of indexes to apply, oppositor_for_this_dimentions),\n    (sequece of indexes to apply, oppositor_for_this_dimentions),\n    ...\n    (sequece of indexes to apply, oppositor_for_this_dimentions)\n  ]\n)\n```\n\nTo create `Partial` oppositor use code:\n\n```python\noppositor = OppositionOperators.PartialOppositor(\n    minimums=minimumns,\n    maximums=maximums,\n    indexes_to_opp_creator=[\n        (sequece of indexes to apply, oppositor_creator),\n        (sequece of indexes to apply, oppositor_creator)\n    ]\n)\n```\n\n\nExample:\n\n```python\nimport numpy as np\nfrom OppOpPopInit import OppositionOperators\n\n# 5 dim population\n\nmin_bound = np.array([-8, -3, -5.7, 0, 0])\nmax_bound = np.array([5, 4, 4, 9, 9])\n\n# population points\npoints = np.array([\n  [1, 2, 3, 4, 7.5],\n  [1.6, -2, 3.9, 0.4, 5],\n  [1.1, 3.2, -3, 4, 5],\n  [4.1, 2, 3, -4, 0.5]\n])\n\n# saved indexes for oppositors\nfirst_op_indexes = np.array([0, 2])\nsecond_op_indexes = np.array([1, 3])\n\noppositor = OppositionOperators.CombinedOppositor(\n  [\n    (first_op_indexes, OppositionOperators.Continual.abs(\n      minimums=min_bound[first_op_indexes],\n      maximums=max_bound[first_op_indexes],\n    )),\n    (second_op_indexes, OppositionOperators.Continual.over(\n      minimums=min_bound[second_op_indexes],\n      maximums=max_bound[second_op_indexes],\n    ))\n  ]\n)\n\n# or \n\noppositor = OppositionOperators.PartialOppositor(\n    minimums=min_bound,\n    maximums=max_bound,\n    indexes_to_opp_creator=[\n        (first_op_indexes, OppositionOperators.Continual.abs),\n        (second_op_indexes, OppositionOperators.Continual.over)\n    ]\n)\n\n# as u see, it's not necessary to oppose population by all dimensions, here we won't oppose by last dimension\n\noppositions = OppositionOperators.Reflect(points, oppositor)\n\nprint(oppositions)\n\n# array([[-4.        ,  1.84589799, -4.7       ,  5.04795851,  7.5       ],\n#       [-4.6       , -0.74399971, -5.6       ,  7.49178902,  5.        ],\n#       [-4.1       ,  0.54619162,  1.3       ,  6.14214043,  5.        ],\n#       [-7.1       , -2.59648698, -4.7       ,  0.95770904,  0.5       ]])\n\n```\n\n[Another example code](tests/op_mixed.py)\n![](tests/output/mixed.png)\n\n\n### RandomPartialOppositor\n\nOne of the most amazing feature of this package is `RandomPartialOppositor`. It lets u apply several oppositors to random subsets of dimension area and change these subsets after some counts after applying. It means that u can apply only one this oppositor to several samples and get result like u applyed several partial oppositors to parts of this samples.\n\nCreate `RandomPartialOppositor` oppositor using this structure:\n\n```python\noppositor = OppositionOperators.RandomPartialOppositor(\n    [\n        (count_of_random_dimensions, repeate_config_during_steps, avalable_indexes, oppositor_creator),\n        (count_of_random_dimensions, repeate_config_during_steps, avalable_indexes, oppositor_creator),\n        ...\n        (count_of_random_dimensions, repeate_config_during_steps, avalable_indexes, oppositor_creator)\n    ],\n    minimums,\n    maximums\n)\n```\n\nSee [simplest example of using](tests/random%20partial%20oppositor.py)\n\n\n### Reflect method\n\nUse `OppositionOperators.Reflect(samples, oppositor)` for oppose samples array using some oppositor. `samples` argument here is 2D-array with size samples*dimension.\n\n### Reflection with selection best\n\nThere is `OppositionOperators.ReflectWithSelectionBest(population_samples, oppositor, eval_func, samples_scores = None, more_is_better = False)` method for reflect population (with size `N`) and select best `N` objects from existing `2N` objects. It has parameters:\n\n* `population_samples` : 2D numpy array;\n            reflected population.\n* `oppositor` : function;\n            applying oppositor.\n* `eval_func` : function;\n            optimized function of population/task.\n* `samples_scores` : `None`/1D numpy array, optional;\n            scores for reflected population (if calculated -- it's not necessary to calculate it again). The default is `None`.\n* `more_is_better` : logical, optional;\n            The goal -- is maximize the function. The default is `False`.\n\nSee [example](tests/reflection_with_selection.py)\n\n![](tests/output/reflection_with_selection_before.png)\n![](tests/output/reflection_with_selection_after.png)\n\n## Population initializers\n\n### Simple random populations\n\nLike `oppositors operators` there are some constructors for creating creators of start population:\n\n* `SampleInitializers.RandomInteger(minimums, maximums)` -- returns function which will return random integer vectors between `minimums` and `maximums`\n* `SampleInitializers.Uniform(minimums, maximums)` -- returns function which will return random vectors between `minimums` and `maximums` from uniform distribution\n* `SampleInitializers.Normal(minimums, maximums, sd = None)` -- returns function which will return random vectors between `minimums` and `maximums` from normal distribution\n\nU can create your initializer function:\n```python\ndef func() -> np.ndarray:\n    # code\n    return valid_sample_array \n```\n\nThere is also `SampleInitializers.Combined(minimums, maximums, indexes, creator_initializers)` for generate population with [different constructors for each dimension subset](#mixed)!\n\nUse `creator` for initialize population with `k` objects using `SampleInitializers.CreateSamples(creator, k)`.\n\n#### `RandomInteger`\n\n[Code](tests/random_int_pop.py)\n![](tests/output/random_int_pop.png)\n\n#### `Uniform`\n\n[Code](tests/random_uniform_pop.py)\n![](tests/output/random_uniform_pop.png)\n\n#### `Normal`\n\n[Code](tests/random_normal_pop.py)\n![](tests/output/random_normal_pop.png)\n\n#### `Mixed`\n\n[Code](tests/random_mixed_pop.py)\n![](tests/output/random_mixed_pop.png)\n\n\n### Populations with oppositions\n\nUse `init_population(total_count, creator, oppositors = None)` to create population of size `total_count` where some objects are constructed by `creator` and other objects are constructed by applying each oppositor from `oppositors` to start objects.\n\n[Code](tests/pop_with_oppositors.py)\n![](tests/output/pop_with_op.png)\n![](tests/output/pop_with_op2.png)\n![](tests/output/pop_with_op3.png)\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "PyPI package containing opposition learning operators and population initializers for evolutionary algorithms",
    "version": "2.0.1",
    "split_keywords": [
        "opposition-based-learning",
        "optimization",
        "evolutionary algorithms",
        "fast",
        "easy",
        "evolution",
        "generator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "96d7f63b820b788761adb8aab4d369d39dcfc2bce768b7f5a074ec49f0fa9543",
                "md5": "1a72a7a8e10bcf72c2241dd477476bbd",
                "sha256": "874f29683c7090e52218b9fee6951835f90434884b596e7c6f9eebea5756f238"
            },
            "downloads": -1,
            "filename": "OppOpPopInit-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1a72a7a8e10bcf72c2241dd477476bbd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15752,
            "upload_time": "2023-01-06T15:34:47",
            "upload_time_iso_8601": "2023-01-06T15:34:47.580851Z",
            "url": "https://files.pythonhosted.org/packages/96/d7/f63b820b788761adb8aab4d369d39dcfc2bce768b7f5a074ec49f0fa9543/OppOpPopInit-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "593e59fa1a994f1ec6fe85539293e8dcd363cefb4ee04d7ae2d35591cb437e5a",
                "md5": "f63ad5fe55b83546b1c4f2bf28c04763",
                "sha256": "6068451b606fc918011a5882bbbee17d2979c9f8951e61f1dddfada78b9e8d53"
            },
            "downloads": -1,
            "filename": "OppOpPopInit-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "f63ad5fe55b83546b1c4f2bf28c04763",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 13997,
            "upload_time": "2023-01-06T15:34:49",
            "upload_time_iso_8601": "2023-01-06T15:34:49.450556Z",
            "url": "https://files.pythonhosted.org/packages/59/3e/59fa1a994f1ec6fe85539293e8dcd363cefb4ee04d7ae2d35591cb437e5a/OppOpPopInit-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-06 15:34:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "PasaOpasen",
    "github_project": "opp-op-pop-init",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "oppoppopinit"
}
        
Elapsed time: 0.04395s