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));
OutputBest 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:
- High Number of Iterations
- Presence of tune-able parameters
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem