Neighborhoods implementation

Neighborhoods implementation#

Here are presented implementations of neighborhoods that can be used for the base and composed variables.

Base variables#

class IntInterval(neighborhood, variable=None)[source]

Bases: VarNeighborhood

Addon, used to determine the neighbor of an IntVar. Draw a random point in x \pm neighborhood.

Parameters:
  • variable (IntVar, default=None) – Targeted Variable.

  • neighborhood (int, default=None) – x \pm neighborhood

Examples

>>> from dragon.search_space.base_variables import IntVar
>>> from dragon.search_operators.base_neighborhoods import IntInterval
>>> a = IntVar("test", 0, 5, neighbor=IntInterval(neighborhood=1))
>>> print(a)
IntVar(test, [0;6])
>>> a_test = a.random()
>>> print(a_test)
4
>>> a.neighbor(a_test)
5
property neighborhood
property target
class FloatInterval(neighborhood, variable=None)[source]

Bases: VarNeighborhood

Addon, used to determine the neighbor of a FloatVar. Draw a random point in x \pm neighborhood.

Parameters:
  • variable (FloatVar, default=None) – Targeted Variable.

  • neighborhood (float, default=None) – x \pm neighborhood

Examples

>>> from dragon.search_space.base_variables import FloatVar
>>> from dragon.search_operators.base_neighborhoods import FloatInterval
>>> a = FloatVar("test", 0, 5, neighbor=FloatInterval(neighborhood=1))
>>> print(a)
FloatVar(test, [0;5])
>>> a_test = a.random()
>>> print(a_test)
4.0063806879878925
>>> a.neighbor(a_test)
4.477278307217116
property neighborhood
property target
class CatInterval(variable=None, neighborhood=None)[source]

Bases: VarNeighborhood

Addon, used to determine the neighbor of a CatVar. Draw a random feature in CatVar.

Parameters:
  • variable (CatVar, default=None) – Targeted Variable.

  • neighborhood (int, default=None) – Undefined, for CatVar it draws a random feature.

Examples

>>> from dragon.search_space.base_variables import CatVar, IntVar
>>> from dragon.search_operators.base_neighborhoods import CatInterval, IntInterval
>>> a = CatVar("test", ['a', 1, 2.56, IntVar("int", 100 , 200, neighbor=IntInterval(10))], neighbor=CatInterval())
>>> print(a)
CatVar(test, ['a', 1, 2.56, IntVar(int, [100;201])])
>>> a.neighbor(120, 10) # 10 neighbors for the value '120' within this search space
[188, 2.56, 'a', 1, 'a', 1, 2.56, 151, 151, 1]
property neighborhood
property target
class ConstantInterval(variable=None, neighborhood=None)[source]

Bases: VarNeighborhood

Addon, used to determine the neighbor of a Constant. Do nothing. Return the constant.

Parameters:

variable (Constant, default=None) – Targeted Variable.

Examples

>>> from dragon.search_space.base_variables import Constant
>>> from dragon.search_operators.base_neighborhoods import ConstantInterval
>>> a = Constant("test", 5, neighbor=ConstantInterval())
>>> print(a)
Constant(test, 5)
>>> a_test = a.random()
>>> print(a_test)
5
>>> a.neighbor(a_test)
5
property neighborhood
property target

Composed variables#

class ArrayInterval(neighborhood=None, variable=None)[source]

Bases: VarNeighborhood

Addon, used to determine the neighbor of an ArrayVar. neighbor kwarg must be implemented for all Variable of the ArrayVar. One Variable is modified for each neighbor drawn.

Parameters:

variable (ArrayVar, default=None) – Targeted Variable.

Examples

>>> from dragon.search_space.base_variables import ArrayVar, IntVar, FloatVar, CatVar
>>> from dragon.search_operators.base_neighborhoods import IntInterval, FloatInterval, CatInterval, ArrayInterval
>>> a = ArrayVar(IntVar("int_1", 0,8, neighbor=IntInterval(2)), IntVar("int_2", 4,45, neighbor=IntInterval(10)),
...              FloatVar("float_1", 2,12, neighbor=FloatInterval(0.5)), CatVar("cat_1", ["Hello", 87, 2.56], neighbor=CatInterval()), neighbor=ArrayInterval())
>>> print(a)
ArrayVar(, [IntVar(int_1, [0;9]),IntVar(int_2, [4;46]),FloatVar(float_1, [2;12]),CatVar(cat_1, ['Hello', 87, 2.56])])
>>> a_test = a.random()
>>> print(a_test)
[7, 25, 7.631003022147808, 87]
>>> a.neighbor(a_test, 10)
[[7, 25, 8.003980345265523, 87], [8, 25, 7.631003022147808, 87], [7, 25, 7.631003022147808, 2.56],
[8, 25, 7.631003022147808, 87], [7, 25, 7.631003022147808, 'Hello'], [7, 17, 7.631003022147808, 87],
[7, 25, 7.631003022147808, 2.56], [7, 25, 7.254907155441848, 87], [7, 25, 7.602659938485088, 87],
[7, 25, 7.631003022147808, 'Hello']]
property neighborhood
property target
class BlockInterval(neighborhood=None, variable=None)[source]

Bases: VarNeighborhood

Addon, used to determine the neighbor of an BlockInterval. neighbor kwarg must be implemented for all Variable of the BlockInterval.

Parameters:

variable (Block, default=None) – Targeted Variable.

Examples

>>> from dragon.search_space.base_variables import Block, ArrayVar, FloatVar, IntVar
>>> from dragon.search_operators.base_neighborhoods import BlockInterval, ArrayInterval, FloatInterval, IntInterval
>>> content = ArrayVar(IntVar("int_1", 0,8, neighbor=IntInterval(2)), IntVar("int_2", 4,45, neighbor=IntInterval(10)),  FloatVar("float_1", 2,12, neighbor=FloatInterval(10)), neighbor=ArrayInterval())
>>> a = Block("max size 10 Block", content, 3, neighbor=BlockInterval())
>>> print(a)
Block(max size 10 Block, [IntVar(int_1, [0;9]),IntVar(int_2, [4;46]),FloatVar(float_1, [2;12]),])
>>> test_a = a.random()
>>> print(test_a)
[[5, 4, 10.780991223247005], [1, 11, 11.446866387945619], [8, 44, 2.9377647083768217]]
>>> a.neighbor(test_a)
[[5, 7, 10.780991223247005], [0, 11, 11.446866387945619], [8, 44, 2.9377647083768217]]
property neighborhood
property target
class DynamicBlockInterval(neighborhood, variable=None)[source]

Bases: VarNeighborhood

BlockInterval

Addon, used to determine the neighbor of a DynamicBlock. neighbor kwarg must be implemented for all Variable of the BlockInterval.

Parameters:
  • variable (IntVar, default=None) – Targeted Variable.

  • neighborhood (int) – Neighborhood of the DynamicBlock size

Example

>>> from dragon.search_space.base_variables import DynamicBlock, ArrayVar, FloatVar, IntVar
>>> from dragon.search_operators.base_neighborhoods import DynamicBlockInterval, ArrayInterval, FloatInterval, IntInterval
>>> content = ArrayVar(IntVar("int_1", 0,8, neighbor=IntInterval(2)), IntVar("int_2", 4,45, neighbor=IntInterval(10)),  FloatVar("float_1", 2,12, neighbor=FloatInterval(10)), neighbor=ArrayInterval())
>>> a = DynamicBlock("max size 10 Block", content, 5, neighbor=DynamicBlockInterval(1))
>>> print(a)
DynamicBlock(max size 10 Block, [IntVar(int_1, [0;9]),IntVar(int_2, [4;46]),FloatVar(float_1, [2;12]),])
>>> test_a = a.random()
>>> print(test_a)
[[4, 10, 7.476654992446498]]
>>> a.neighbor(test_a)
[[4, 17, 7.476654992446498], [2, 5, 8.057170687346623], [2, 19, 7.316509989314727], [8, 9, 8.294482483654278], [2, 31, 5.36321423474537]]
property neighborhood
property target