All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cicirello.search.operators.permutations.CycleCrossover Maven / Gradle / Ivy

Go to download

Chips-n-Salsa is a Java library of customizable, hybridizable, iterative, parallel, stochastic, and self-adaptive local search algorithms. The library includes implementations of several stochastic local search algorithms, including simulated annealing, hill climbers, as well as constructive search algorithms such as stochastic sampling. Chips-n-Salsa now also includes genetic algorithms as well as evolutionary algorithms more generally. The library very extensively supports simulated annealing. It includes several classes for representing solutions to a variety of optimization problems. For example, the library includes a BitVector class that implements vectors of bits, as well as classes for representing solutions to problems where we are searching for an optimal vector of integers or reals. For each of the built-in representations, the library provides the most common mutation operators for generating random neighbors of candidate solutions, as well as common crossover operators for use with evolutionary algorithms. Additionally, the library provides extensive support for permutation optimization problems, including implementations of many different mutation operators for permutations, and utilizing the efficiently implemented Permutation class of the JavaPermutationTools (JPT) library. Chips-n-Salsa is customizable, making extensive use of Java's generic types, enabling using the library to optimize other types of representations beyond what is provided in the library. It is hybridizable, providing support for integrating multiple forms of local search (e.g., using a hill climber on a solution generated by simulated annealing), creating hybrid mutation operators (e.g., local search using multiple mutation operators), as well as support for running more than one type of search for the same problem concurrently using multiple threads as a form of algorithm portfolio. Chips-n-Salsa is iterative, with support for multistart metaheuristics, including implementations of several restart schedules for varying the run lengths across the restarts. It also supports parallel execution of multiple instances of the same, or different, stochastic local search algorithms for an instance of a problem to accelerate the search process. The library supports self-adaptive search in a variety of ways, such as including implementations of adaptive annealing schedules for simulated annealing, such as the Modified Lam schedule, implementations of the simpler annealing schedules but which self-tune the initial temperature and other parameters, and restart schedules that adapt to run length.

There is a newer version: 7.0.1
Show newest version
/*
 * Chips-n-Salsa: A library of parallel self-adaptive local search algorithms.
 * Copyright (C) 2002-2022 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.math.rand.RandomIndexer;
import org.cicirello.permutations.Permutation;
import org.cicirello.permutations.PermutationFullBinaryOperator;
import org.cicirello.search.operators.CrossoverOperator;

/**
 * Implementation of cycle crossover (CX). CX selects a random index into the permutations, computes
 * the permutation cycle of the pair of parent permutations that includes that randomly chosen
 * element, and then exchanges the elements of the cycle between the parents in forming the
 * children.
 *
 * 

For example, consider the permutation p1 = [0, 1, 2, 3, 4, 5, 6, 7] and the permutation p2 = * [1, 2, 0, 5, 6, 7, 4, 3]. Consider that the random index is 3. At index 3 in p1 is element 3, and * at that same index in p2 is element 5. At the same position as 5 in p1 is 7 in p2. And at the * same position as element 7 in p1 is element 3 in p2, thus completing the cycle. The elements of * the cycle are exchanged between the parents to form the children. Thus, the children are c1 = [0, * 1, 2, 5, 4, 7, 6, 3] and c2 = [1, 2, 0, 3, 6, 5, 4, 7]. * *

The worst case runtime of a call to {@link #cross cross} is O(n), where n is the length of the * permutations. * *

The CX operator was introduced in the following paper:
* Oliver, I.M., Smith, D.J., and Holland, J.R.C. A study of permutation crossover operators on the * traveling salesman problem. Proceedings of the 2nd International Conference on Genetic * Algorithms, 1987, pp. 224-230. * * @author Vincent A. Cicirello, https://www.cicirello.org/ */ public final class CycleCrossover implements CrossoverOperator, PermutationFullBinaryOperator { /** Constructs a cycle crossover (CX) operator. */ public CycleCrossover() {} @Override public void cross(Permutation c1, Permutation c2) { c1.apply(this, c2); } @Override public CycleCrossover split() { // doesn't maintain any state, so safe to return this return this; } /** * See {@link PermutationFullBinaryOperator} for details of this method. This method is not * intended for direct usage. Use the {@link #cross} method instead. * * @param raw1 The raw representation of the first permutation. * @param raw2 The raw representation of the second permutation. * @param p1 The first permutation. * @param p2 The second permutation. */ @Override public void apply(int[] raw1, int[] raw2, Permutation p1, Permutation p2) { boolean[] inCycle = new boolean[raw1.length]; int[] cycle = new int[raw1.length]; int count = 0; int[] inv1 = p1.getInverse(); int i = RandomIndexer.nextInt(raw1.length); while (!inCycle[i]) { inCycle[i] = true; cycle[count] = i; count++; i = inv1[raw2[i]]; } for (i = 0; i < count; i++) { int temp = raw1[cycle[i]]; raw1[cycle[i]] = raw2[cycle[i]]; raw2[cycle[i]] = temp; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy