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

org.cicirello.search.problems.QuadraticAssignmentProblem 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.problems;

import java.util.SplittableRandom;
import java.util.random.RandomGenerator;
import org.cicirello.math.rand.RandomIndexer;
import org.cicirello.permutations.Permutation;

/**
 * This class is an implementation of the Quadratic Assignment Problem (QAP), an NP-Hard
 * optimization problem. In this implementation, both the cost and distance matrices are
 * integer-valued. This class uses factory methods, rather than constructors to better support a
 * variety of ways of generating random instances. The class supports generating uniform random
 * instances via the {@link #createUniformRandomInstance createUniformRandomInstance} method, as
 * well as creating instances by directly specifying the cost and distance matrices via the {@link
 * #createInstance createInstance}.
 *
 * @author Vincent A. Cicirello, https://www.cicirello.org/
 */
public final class QuadraticAssignmentProblem
    implements IntegerCostOptimizationProblem {

  private final int[][] cost;
  private final int[][] distance;

  /*
   * package private internal constructor. This constructor does no validation.
   * Additionally, be careful in matrices passed to this constructor as it
   * stores references directly to those matrices.
   */
  QuadraticAssignmentProblem(int[][] cost, int[][] distance) {
    this.cost = cost;
    this.distance = distance;
  }

  @Override
  public int cost(Permutation candidate) {
    int total = 0;
    for (int i = 0; i < cost.length; i++) {
      for (int j = i + 1; j < cost.length; j++) {
        total += cost[i][j] * distance[candidate.get(i)][candidate.get(j)];
        total += cost[j][i] * distance[candidate.get(j)][candidate.get(i)];
      }
    }
    return total;
  }

  @Override
  public int value(Permutation candidate) {
    return cost(candidate);
  }

  @Override
  public int minCost() {
    // simply return 0 as a trivial lower bound
    return 0;
  }

  /**
   * Gets the size of the instance.
   *
   * @return the size of the instance.
   */
  public int size() {
    return cost.length;
  }

  /**
   * Gets an element of the cost matrix.
   *
   * @param i The row index.
   * @param j The column index.
   * @return The cost of cell i, j.
   * @throws IndexOutOfBoundsException if either i or j is less than 0, or if either i or j is
   *     greater than or equal to size().
   */
  public int getCost(int i, int j) {
    return cost[i][j];
  }

  /**
   * Gets an element of the distance matrix.
   *
   * @param i The row index.
   * @param j The column index.
   * @return The distance of cell i, j.
   * @throws IndexOutOfBoundsException if either i or j is less than 0, or if either i or j is
   *     greater than or equal to size().
   */
  public int getDistance(int i, int j) {
    return distance[i][j];
  }

  /**
   * Creates an instance of the QAP problem from given cost and distance matrices. Note that the
   * {@link #cost cost} and {@link #value value} methods assume that the diagonal is always 0.
   *
   * @param cost The cost matrix which must be square.
   * @param distance The distance matrix which must be square and of the same dimensions as the cost
   *     matrix.
   * @return an instance of the QAP problem from the specified cost and distance matrices
   * @throws IllegalArgumentException if the cost and distance matrices are not square or not of the
   *     same dimensions
   */
  public static QuadraticAssignmentProblem createInstance(int[][] cost, int[][] distance) {
    if (cost.length != distance.length) {
      throw new IllegalArgumentException(
          "cost and distance matrices must have same number of rows");
    }
    int[][] costPrime = new int[cost.length][];
    int[][] distancePrime = new int[distance.length][];
    for (int i = 0; i < cost.length; i++) {
      if (cost[i].length != cost.length || distance[i].length != cost.length) {
        throw new IllegalArgumentException("cost and distance matrices must be square");
      }
      costPrime[i] = cost[i].clone();
      distancePrime[i] = distance[i].clone();
    }
    return new QuadraticAssignmentProblem(costPrime, distancePrime);
  }

  /**
   * Creates an instance of the QAP problem with cost and distance matrices formed from uniformly
   * random integers. Note that the diagonal is always 0.
   *
   * @param size Create a size by size instance.
   * @param minCost Costs are uniform at random from an interval with minCost as the minimum.
   * @param maxCost Costs are uniform at random from an interval with maxCost as the maximum.
   * @param minDistance Distances are uniform at random from an interval with minDistance as the
   *     minimum.
   * @param maxDistance Distances are uniform at random from an interval with maxDistance as the
   *     maximum.
   * @return an instance of the QAP problem with uniform random cost and distance matrices
   * @throws IllegalArgumentException if size is less than 1, or maxCost is less than minCost, or
   *     maxDistance is less than minDistance.
   */
  public static QuadraticAssignmentProblem createUniformRandomInstance(
      int size, int minCost, int maxCost, int minDistance, int maxDistance) {
    return createUniformRandomInstance(
        size, minCost, maxCost, minDistance, maxDistance, new SplittableRandom());
  }

  /**
   * Creates an instance of the QAP problem with cost and distance matrices formed from uniformly
   * random integers. Note that the diagonal is always 0.
   *
   * @param size Create a size by size instance.
   * @param minCost Costs are uniform at random from an interval with minCost as the minimum.
   * @param maxCost Costs are uniform at random from an interval with maxCost as the maximum.
   * @param minDistance Distances are uniform at random from an interval with minDistance as the
   *     minimum.
   * @param maxDistance Distances are uniform at random from an interval with maxDistance as the
   *     maximum.
   * @param seed The seed for the random number generator.
   * @return an instance of the QAP problem with uniform random cost and distance matrices
   * @throws IllegalArgumentException if size is less than 1, or maxCost is less than minCost, or
   *     maxDistance is less than minDistance.
   */
  public static QuadraticAssignmentProblem createUniformRandomInstance(
      int size, int minCost, int maxCost, int minDistance, int maxDistance, long seed) {
    return createUniformRandomInstance(
        size, minCost, maxCost, minDistance, maxDistance, new SplittableRandom(seed));
  }

  private static QuadraticAssignmentProblem createUniformRandomInstance(
      int size, int minCost, int maxCost, int minDistance, int maxDistance, RandomGenerator gen) {
    if (size < 1) throw new IllegalArgumentException("size must be at least 1");
    if (maxCost < minCost) throw new IllegalArgumentException("maxCost must be at least minCost");
    if (maxDistance < minDistance)
      throw new IllegalArgumentException("maxDistance must be at least minDistance");
    return new QuadraticAssignmentProblem(
        createRandomMatrix(size, minCost, maxCost, gen),
        createRandomMatrix(size, minDistance, maxDistance, gen));
  }

  private static int[][] createRandomMatrix(int size, int min, int max, RandomGenerator gen) {
    int[][] matrix = new int[size][size];
    int bound = max - min + 1;
    for (int i = 0; i < size; i++) {
      for (int j = i + 1; j < size; j++) {
        matrix[i][j] = min + RandomIndexer.nextInt(bound, gen);
        matrix[j][i] = min + RandomIndexer.nextInt(bound, gen);
      }
    }
    return matrix;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy