Saturday, December 28, 2024
Google search engine
HomeLanguagesWhat is TABU Search?

What is TABU Search?

Tabu search (TS) is an iterative neighborhood search algorithm, where the neighborhood changes dynamically. Tabu search enhances local search by avoiding points in the search space which are already visited. By avoiding already visited points, loops in search space are avoided and local optima can be escaped. 

The main feature of Tabu Search is the use of explicit memory, with two goals: 

  • To prevent the search from revisiting previously visited solutions.
  • To explore the unvisited areas of the solution space. 

How to Optimize an Algorithm Using Tabu Search?

Optimize a solution using the Tabu Search algorithm to find the best solution that minimizes the fitness value of the objective function for a given initial solution, within a specified number of iterations and using a tabu list of limited size.

The code uses Tabu Search to iteratively investigate solutions by assessing their fitness with an objective function and producing nearby solutions with a neighborhood function. Utilizing a tabu list, it attempts to discover the optimal solution with the lowest fitness within a predetermined amount of iterations, avoiding returning to previously explored solutions.

C++




#include <algorithm>
#include <iostream>
#include <numeric> // Added to include accumulate
#include <vector>
 
// Define the objective function
int objective_function(const std::vector<int>& solution)
{ // Added const qualifier
    // TODO: Implement your objective function here
    // The objective function should evaluate
    // the quality of a given solution and
    // return a numerical value representing
    // the solution's fitness
    // Example: return std::accumulate(solution.begin(),
    // solution.end(), 0);
    return std::accumulate(solution.begin(), solution.end(),
                           0);
}
 
// Define the neighborhood function
std::vector<std::vector<int> >
get_neighbors(const std::vector<int>& solution)
{ // Added const qualifier
    std::vector<std::vector<int> > neighbors;
    for (size_t i = 0; i < solution.size(); i++) {
        for (size_t j = i + 1; j < solution.size(); j++) {
            std::vector<int> neighbor = solution;
            std::swap(neighbor[i], neighbor[j]);
            neighbors.push_back(neighbor);
        }
    }
    return neighbors;
}
 
// Define the Tabu Search algorithm
std::vector<int>
tabu_search(const std::vector<int>& initial_solution,
            int max_iterations, int tabu_list_size)
{ // Added const qualifier
    std::vector<int> best_solution = initial_solution;
    std::vector<int> current_solution = initial_solution;
    std::vector<std::vector<int> > tabu_list;
 
    for (int iter = 0; iter < max_iterations; iter++) {
        std::vector<std::vector<int> > neighbors
            = get_neighbors(current_solution);
        std::vector<int> best_neighbor;
        int best_neighbor_fitness
            = std::numeric_limits<int>::max();
 
        for (const std::vector<int>& neighbor : neighbors) {
            if (std::find(tabu_list.begin(),
                          tabu_list.end(), neighbor)
                == tabu_list.end()) {
                int neighbor_fitness
                    = objective_function(neighbor);
                if (neighbor_fitness
                    < best_neighbor_fitness) {
                    best_neighbor = neighbor;
                    best_neighbor_fitness
                        = neighbor_fitness;
                }
            }
        }
 
        if (best_neighbor.empty()) {
            // No non-tabu neighbors found,
            // terminate the search
            break;
        }
 
        current_solution = best_neighbor;
        tabu_list.push_back(best_neighbor);
        if (tabu_list.size() > tabu_list_size) {
            // Remove the oldest entry from the
            // tabu list if it exceeds the size
            tabu_list.erase(tabu_list.begin());
        }
 
        if (objective_function(best_neighbor)
            < objective_function(best_solution)) {
            // Update the best solution if the
            // current neighbor is better
            best_solution = best_neighbor;
        }
    }
 
    return best_solution;
}
 
int main()
{
    // Example usage
    // Provide an initial solution
    std::vector<int> initial_solution = { 1, 2, 3, 4, 5 };
    int max_iterations = 100;
    int tabu_list_size = 10;
 
    std::vector<int> best_solution = tabu_search(
        initial_solution, max_iterations, tabu_list_size);
    std::cout << "Best solution:";
    for (int val : best_solution) {
        std::cout << " " << val;
    }
    std::cout << std::endl;
    std::cout << "Best solution fitness: "
              << objective_function(best_solution)
              << std::endl;
 
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
 
public class TabuSearch {
    // Define the objective function
    public static int objectiveFunction(List<Integer> solution) {
        // TODO: Implement your objective function here
        // The objective function should evaluate
        // the quality of a given solution and
        // return a numerical value representing
        // the solution's fitness
        // Example: return solution.stream().mapToInt(Integer::intValue).sum();
        return solution.stream().mapToInt(Integer::intValue).sum();
    }
 
    // Define the neighborhood function
    public static List<List<Integer>> getNeighbors(List<Integer> solution) {
        List<List<Integer>> neighbors = new ArrayList<>();
        for (int i = 0; i < solution.size(); i++) {
            for (int j = i + 1; j < solution.size(); j++) {
                List<Integer> neighbor = new ArrayList<>(solution);
                // Swap elements at indices i and j
                int temp = neighbor.get(i);
                neighbor.set(i, neighbor.get(j));
                neighbor.set(j, temp);
                neighbors.add(neighbor);
            }
        }
        return neighbors;
    }
 
    // Define the Tabu Search algorithm
    public static List<Integer> tabuSearch(List<Integer> initialSolution,
                                           int maxIterations, int tabuListSize) {
        List<Integer> bestSolution = new ArrayList<>(initialSolution);
        List<Integer> currentSolution = new ArrayList<>(initialSolution);
        List<List<Integer>> tabuList = new ArrayList<>();
 
        for (int iter = 0; iter < maxIterations; iter++) {
            List<List<Integer>> neighbors = getNeighbors(currentSolution);
            List<Integer> bestNeighbor = new ArrayList<>();
            int bestNeighborFitness = Integer.MAX_VALUE;
 
            for (List<Integer> neighbor : neighbors) {
                if (!tabuList.contains(neighbor)) {
                    int neighborFitness = objectiveFunction(neighbor);
                    if (neighborFitness < bestNeighborFitness) {
                        bestNeighbor = new ArrayList<>(neighbor);
                        bestNeighborFitness = neighborFitness;
                    }
                }
            }
 
            if (bestNeighbor.isEmpty()) {
                // No non-tabu neighbors found, terminate the search
                break;
            }
 
            currentSolution = new ArrayList<>(bestNeighbor);
            tabuList.add(bestNeighbor);
 
            if (tabuList.size() > tabuListSize) {
                // Remove the oldest entry from the tabu list if it exceeds the size
                tabuList.remove(0);
            }
 
            if (objectiveFunction(bestNeighbor) < objectiveFunction(bestSolution)) {
                // Update the best solution if the current neighbor is better
                bestSolution = new ArrayList<>(bestNeighbor);
            }
        }
 
        return bestSolution;
    }
 
    public static void main(String[] args) {
        // Example usage
        // Provide an initial solution
        List<Integer> initialSolution = List.of(1, 2, 3, 4, 5);
        int maxIterations = 100;
        int tabuListSize = 10;
 
        List<Integer> bestSolution =
          tabuSearch(initialSolution, maxIterations, tabuListSize);
        System.out.print("Best solution:");
        for (int val : bestSolution) {
            System.out.print(" " + val);
        }
        System.out.println();
        System.out.println("Best solution fitness: " + objectiveFunction(bestSolution));
    }
}


Python




import random
 
# Define the objective function
 
 
def objective_function(solution):
    # TODO: Implement your objective function here
    # The objective function should evaluate
    # the quality of a given solution and
    # return a numerical value representing
    # the solution's fitness
    # Example: return sum(solution)
    return sum(solution)
 
# Define the neighborhood function
 
 
def get_neighbors(solution):
 
    # TODO: Implement your neighborhood function here
    # The neighborhood function should generate
    # a set of neighboring solutions based on a given solution
    # Example: Generate neighboring solutions by
    # swapping two elements in the solution
 
    neighbors = []
    for i in range(len(solution)):
        for j in range(i + 1, len(solution)):
            neighbor = solution[:]
            neighbor[i], neighbor[j] = neighbor[j], neighbor[i]
            neighbors.append(neighbor)
    return neighbors
 
# Define the Tabu Search algorithm
 
 
def tabu_search(initial_solution, max_iterations, tabu_list_size):
    best_solution = initial_solution
    current_solution = initial_solution
    tabu_list = []
 
    for _ in range(max_iterations):
        neighbors = get_neighbors(current_solution)
        best_neighbor = None
        best_neighbor_fitness = float('inf')
 
        for neighbor in neighbors:
            if neighbor not in tabu_list:
                neighbor_fitness = objective_function(neighbor)
                if neighbor_fitness < best_neighbor_fitness:
                    best_neighbor = neighbor
                    best_neighbor_fitness = neighbor_fitness
 
        if best_neighbor is None:
 
            # No non-tabu neighbors found,
            # terminate the search
            break
 
        current_solution = best_neighbor
        tabu_list.append(best_neighbor)
        if len(tabu_list) > tabu_list_size:
 
            # Remove the oldest entry from the
            # tabu list if it exceeds the size
            tabu_list.pop(0)
 
        if objective_function(best_neighbor) < objective_function(best_solution):
            # Update the best solution if the
            # current neighbor is better
            best_solution = best_neighbor
 
    return best_solution
 
 
# Example usage
# Provide an initial solution
initial_solution = [1, 2, 3, 4, 5]
max_iterations = 100
tabu_list_size = 10
 
best_solution = tabu_search(initial_solution, max_iterations, tabu_list_size)
print("Best solution: {}".format(best_solution))
print("Best solution fitness: {}".format(objective_function(best_solution)))


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
      // Define the objective function
    static int ObjectiveFunction(List<int> solution)
    {
     // Added const qualifier
    // TODO: Implement your objective function here
    // The objective function should evaluate
    // the quality of a given solution and
    // return a numerical value representing
    // the solution's fitness
    // Example: return std::accumulate(solution.begin(),
    // solution.end(), 0);
        return solution.Sum();
    }
 
      // Define the neighborhood function
    static List<List<int>> GetNeighbors(List<int> solution)
    {
          // Added const qualifier
        List<List<int>> neighbors = new List<List<int>>();
        for (int i = 0; i < solution.Count; i++)
        {
            for (int j = i + 1; j < solution.Count; j++)
            {
                List<int> neighbor = new List<int>(solution);
                neighbor[i] = solution[j];
                neighbor[j] = solution[i];
                neighbors.Add(neighbor);
            }
        }
        return neighbors;
    }
 
      // Define the Tabu Search algorithm
    static List<int> TabuSearch(List<int> initial_solution,
                                int max_iterations,
                                int tabu_list_size)
            {
       
    // Added const qualifier
    List<int> best_solution = initial_solution;
    List<int> current_solution = initial_solution;
    List<List<int>> tabu_list = new List<List<int>>();
    for (int iter = 0; iter < max_iterations; iter++)
    {
        List<List<int>> neighbors = GetNeighbors(current_solution);
        List<int> best_neighbor = new List<int>();
        int best_neighbor_fitness = int.MaxValue;
        foreach (List<int> neighbor in neighbors)
        {
            if (!tabu_list.Contains(neighbor))
            {
                int neighbor_fitness = ObjectiveFunction(neighbor);
                if (neighbor_fitness < best_neighbor_fitness)
                {
                    best_neighbor = neighbor;
                    best_neighbor_fitness = neighbor_fitness;
                }
            }
        }
        if (best_neighbor.Count == 0)
        {
              // No non-tabu neighbors found,
            // terminate the search
            break;
        }
        current_solution = best_neighbor;
        tabu_list.Add(best_neighbor);
        if (tabu_list.Count > tabu_list_size)
        {
              // Remove the oldest entry from the
            // tabu list if it exceeds the size
            tabu_list.RemoveAt(0);
        }
        if (ObjectiveFunction(best_neighbor) < ObjectiveFunction(best_solution))
        {
              // Update the best solution if the
            // current neighbor is better
            best_solution = best_neighbor;
        }
    }
    return best_solution;
}
      static void Main()
    {
          // Example usage
        // Provide an initial solution
        List<int> initial_solution = new List<int> { 1, 2, 3, 4, 5 };
        int max_iterations = 100;
        int tabu_list_size = 10;
        List<int> best_solution = TabuSearch(
            initial_solution, max_iterations, tabu_list_size);
        Console.Write("Best solution:");
        foreach (int val in best_solution)
        {
            Console.Write(" " + val);
        }
        Console.WriteLine();
        Console.WriteLine("Best solution fitness: " +
            ObjectiveFunction(best_solution));
    }
 
}


Javascript




function objectiveFunction(solution) {
    // TODO: Implement your objective function here
    // The objective function should evaluate
    // the quality of a given solution and
    // return a numerical value representing
    // the solution's fitness
    // Example: return solution.reduce((sum, val) => sum + val, 0);
    return solution.reduce((sum, val) => sum + val, 0);
}
 
function getNeighbors(solution) {
    const neighbors = [];
    for (let i = 0; i < solution.length; i++) {
        for (let j = i + 1; j < solution.length; j++) {
            const neighbor = [...solution];
            // Swap elements at indices i and j
            [neighbor[i], neighbor[j]] = [neighbor[j], neighbor[i]];
            neighbors.push(neighbor);
        }
    }
    return neighbors;
}
 
function tabuSearch(initialSolution, maxIterations, tabuListSize) {
    let bestSolution = [...initialSolution];
    let currentSolution = [...initialSolution];
    const tabuList = [];
 
    for (let iter = 0; iter < maxIterations; iter++) {
        const neighbors = getNeighbors(currentSolution);
        let bestNeighbor = [];
        let bestNeighborFitness = Number.MAX_VALUE;
 
        for (const neighbor of neighbors) {
            if (!tabuList.some(entry => JSON.stringify(entry) === JSON.stringify(neighbor))) {
                const neighborFitness = objectiveFunction(neighbor);
                if (neighborFitness < bestNeighborFitness) {
                    bestNeighbor = [...neighbor];
                    bestNeighborFitness = neighborFitness;
                }
            }
        }
 
        if (bestNeighbor.length === 0) {
            // No non-tabu neighbors found, terminate the search
            break;
        }
 
        currentSolution = [...bestNeighbor];
        tabuList.push([...bestNeighbor]);
 
        if (tabuList.length > tabuListSize) {
            // Remove the oldest entry from the tabu list if it exceeds the size
            tabuList.shift();
        }
 
        if (objectiveFunction(bestNeighbor) < objectiveFunction(bestSolution)) {
            // Update the best solution if the current neighbor is better
            bestSolution = [...bestNeighbor];
        }
    }
 
    return bestSolution;
}
 
// Example usage
const initialSolution = [1, 2, 3, 4, 5];
const maxIterations = 100;
const tabuListSize = 10;
 
const bestSolution = tabuSearch(initialSolution, maxIterations, tabuListSize);
console.log("Best solution:", bestSolution.join(" "));
console.log("Best solution fitness:", objectiveFunction(bestSolution));


Output

Best solution: [1, 2, 3, 4, 5]
Best solution fitness: 15






NOTE:  We can replace the objective_function implementation with your specific objective function and provide a suitable initial_solution to ensure the code works as intended.

Explanation of code:

  • Implementing the objective function and the neighborhood function unique to your situation is required by the code.
  • A solution’s fitness is quantified numerically by the objective function, which assesses a solution’s quality.
  • Using a given solution as a starting point, the neighborhood function creates neighboring solutions.
  • The best non-tabu neighbor is considered as the search space is iteratively explored using the Tabu Search algorithm.
  • According to the specifications of your problem, you should adjust the starting solution, maximum iterations, and tabu list size.
  • Depending on how difficult your problem is, the code provides a basic framework for implementing Tabu Search and might need more customization.
  • Don’t forget to replace the TODO placeholder sections with your actual implementation for your particular problem, describing the solution representation and fitness calculation in the objective function.

Complexity Analysis: 

As Tabu Search’s temporal complexity varies depending on the particular problem being solved and the scope of the search area, there is no universal method for calculating it. 

  • The worst-case time complexity of a Tabu Search problem, which is commonly expressed as O(2^n), where n is the size of the search space, nevertheless, can be exponential in the magnitude of the problem.
  • Although it depends on the size of the search space, Tabu Search’s space complexity is often substantially less than its worst-case time complexity. The size of the search space is denoted as O(k*n), where n is the size of the search space and k is the size of the Tabu List or the number of candidate solutions being assessed and stored in memory.

It’s important to keep in mind that the actual time and space complexity of Tabu Search might vary significantly depending on the particular problem being solved, the algorithm parameters chosen (such as the size of the Tabu List, the aspiration criteria, the diversification strategies, etc.), and the effectiveness of the implementation. To discover the greatest performance for a particular task, it is therefore frequently required to experiment with various parameter values and implementation methodologies.

Examples of Problems to Solve with Tabu Search:

  • N-Queens Problem
  • Traveling Salesman Problem (TSP)
  • Minimum Spanning Tree (MST)
  • Assignment Problems
  • Vehicle Routing
  • DNA Sequencing

Advantages of Tabu Search:

  • Can be applied to both discrete and continuous solutions.
  • The tabu list can be used to resist cycles and revert to old solutions.

Disadvantages of Tabu Search:

  • Hight Number of Iterations
  • Presence of tune-able parameters

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments