
org.cicirello.search.operators.permutations.PermutationInitializer Maven / Gradle / Ivy
Show all versions of chips-n-salsa Show documentation
/*
* Chips-n-Salsa: A library of parallel self-adaptive local search algorithms.
* Copyright (C) 2002-2021 Vincent A. Cicirello
*
* This file is part of Chips-n-Salsa (https://chips-n-salsa.cicirello.org/).
*
* Chips-n-Salsa is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chips-n-Salsa is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.cicirello.search.operators.permutations;
import org.cicirello.permutations.Permutation;
import org.cicirello.search.operators.Initializer;
/**
* The PermutationInitializer provides metaheuristic implementations, such as of simulated
* annealing, etc, with a way to generate random initial solutions to a problem, as well as a way to
* make copies of current solution configurations. Since it implements the {@link Initializer}
* interface, which relies on generics, such implementations of search algorithms such as simulated
* annealing, etc, do not need to know the representation of solutions.
*
* This initializer generates permutations uniformly at random from among the space of
* permutations. This is the typical case for many metaheuristics (e.g., hill climbers and simulated
* annealers often begin at a random solution).
*
* @author Vincent A. Cicirello, https://www.cicirello.org/
*/
public final class PermutationInitializer implements Initializer {
private final int n;
/**
* Constructs a PermutationInitializer.
*
* @param n The length of the permutations generated by this permutation initializer. n must be
* non-negative.
* @throws IllegalArgumentException if n is negative.
*/
public PermutationInitializer(int n) {
if (n < 0) throw new IllegalArgumentException("n must be non-negative");
this.n = n;
}
@Override
public Permutation createCandidateSolution() {
return new Permutation(n);
}
@Override
public PermutationInitializer split() {
// class is thread-safe, and immutable so just return this.
return this;
}
}