Skip to content

Neighborhoods

neighborhoods

Neighborhoods: the local moves of each variable type, for the local search methods.

A neighborhood lists the moves of one solution (moves), draws random ones (sample), builds the neighbors (apply) and gives each move its tabu attributes (attributes). Each variable type has one class with these move kinds:

Type Kinds (the first is the basic move) A made move makes tabu
Permutation swap, insertion, inversion an item going back to where it was (inversion: a removed edge returning)
Integer step (plus or minus 1), reset (another value) the component going back to its old value
Binary flip flipping that bit again
Real coordinate (one component plus or minus step), gaussian (noise on all) the component moving back
Selection replace (an item with an unused one), swap (two positions) the removed item coming back
BinarySelection swap (a one and a zero) the removed item coming back
Partition, BinaryPartition move (an item to another group), swap (items of two groups) the item going back
FixedSum transfer (part of one element to another with the same sum) the reverse transfer

ProblemNeighborhood joins the neighborhoods of all variables of a problem and works on code vectors: a random move picks a variable, then a kind, uniformly at random. Its moves are tuples (variable index, kind, move), where a move is a float array: the row of a variable with count (0 otherwise), then the move's own columns. All random numbers come from the methods that ypea.StudioRng offers.

ProblemNeighborhood

ProblemNeighborhood(problem, kinds=None, *, real_moves='coordinate', step=0.1, inversion_attribute='edges')

The moves of all variables of a problem, on code vectors.

kinds maps a variable type or name to its move kinds (default: all kinds, and real_moves for Real variables). step is the Real step as a fraction of the range.

values

values(code)

The decoded value of each variable.

pass_size

pass_size(values, level=None)

The number of moves in one pass: the count, with unlimited kinds counted by sample_count.

candidates

candidates(code, values, size, rng, level=None, shuffle=False)

Moves of the solution code: all of them when there are at most size, otherwise size random ones. Returns (moves, codes of the neighbors).

neighbors

neighbors(code, values, groups)

Codes of the neighbors for groups of (variable, kind, moves).

attributes

attributes(values, moves)

(made, checked) attribute keys of each move, as (M, 2) arrays with -1 for none.

random_moves

random_moves(code, k, rng, distinct=True, level=None)

Apply k random moves one after another (VNS shaking) and return the new code. With distinct, each move changes elements that earlier moves left alone, when one of 10 random moves does.

Neighborhood

Neighborhood(var, kinds=None)

Moves of one variable. Subclasses work on one row (a flat array or a partition) and set kinds, row_attributes_size (the size of a row's attribute space) and row_size (its number of elements).

row_attributes

row_attributes(row, kind, moves)

(made, checked): the attributes a move makes tabu, and those that make it tabu.

row_touched

row_touched(row, kind, moves)

The elements a move changes (positions, components or items), or None for all.

sample_count

sample_count(x, kind)

How many random moves a pass draws when the neighborhood is unlimited.

apply

apply(x, kind, moves)

The neighbors, one per move, as a list of values.

neighborhood

neighborhood(var, kinds=None, *, step=0.1, inversion_attribute='edges')

The neighborhood of a variable, with the given move kinds (default: all of them).