Skip to main content

Featured Posts

What is Artificial Intelligence

 Introduction to Artificial Intelligence Definition of Artificial Intelligence (AI)? Artificial Intelligence (AI) refers to the creation of intelligent machines that can work and think like humans. History The historical backdrop of man-made consciousness (computer-based intelligence) traces all the way back to the 1950s when scientists initially started investigating the idea of making machines that could perform undertakings that commonly require human knowledge, like grasping the normal language, perceiving pictures, and simply deciding. Early AI research focused on developing algorithms and programs that could mimic the problem-solving abilities of human brains. This led to the creation of early AI applications such as expert systems and decision-making systems. During the 1980s and 1990s, artificial intelligence research moved towards the advancement of "AI" calculations, which permitted PCs to gain from information without being expressly customized. This led to the cre...

Learn Problem Solving Informed Search Algorithms

Problem Solving - Hill Climbing and Genetic Algorithm

Informed Search Algorithms : Hill-Climbing and Generic

2.3 Hill Climbing Search:

Hill Climbing Search is an informed search algorithm that always chooses the neighbour that appears to be closest to the goal node according to the heuristic function. Here's how Hill Climbing Search works step-by-step:

  • Evaluate the current state using the heuristic function h(n).
  • If the current state is the goal state, return it.
  • Otherwise, generate the neighbour states of the current state.
  • Evaluate each neighbour state using the heuristic function h(n).
  • If any neighbour state has a lower h-value than the current state, choose the neighbour state with the lowest h-value and repeat from step 2.
  • If no neighbour state has a lower h-value than the current state, return failure.

algorithm that  chooses the neighbour that appears to be closest to the goal node according to the heuristic function.

Here's an example of using Hill Climbing Search to find a path from node A to node G in the graph above:

Hill Climbing Search would start at node A and evaluate its heuristic function h(A) = 10. It would choose node B as its neighbour since h(B) = 9, which is lower than h(C) = 8 and h(D) = 7. It would then evaluate h(B) = 9 and choose node C as its neighbour since h(C) = 6, which is lower than h(D) = 7. Furthermore, it would then evaluate h(C) = 6 and choose node E as its neighbour since h(E) = 4, which is lower than h(F) = 5. It would then evaluate h(E) = 4 and choose node G as its neighbour, since h(G) = 0. It would find the goal node G with a cost of 14.

Here's the Python code for Hill Climbing Search:

Python code

def hill_climbing(graph, start, goal, heuristic):
current = start
while current != goal:
neighbors = graph[current]
next_node = None
min_h = float('inf')
for a neighbour in neighbours:
h = heuristic(neighbor, goal)
if h < min_h:
next_node = neighbor
min_h = h
if min_h >= heuristic(current, goal):
return None
current = next_node
return [start, current, goal]

I hope this helps you understand these informed search

4. Local Search (Simulated Annealing, Genetic Algorithm) 

4.1 Simulated Annealing:

Simulated Annealing is a metaheuristic optimization algorithm that can be used to solve optimization problems where the solution space is too large or too complex to be searched exhaustively. It is based on the physical process of annealing, where a metal is heated and then slowly cooled to reduce its defects and improve its strength. The algorithm starts with an initial solution and then iteratively explores the solution space by making random moves. It accepts moves that improve the objective function value and occasionally accepts moves that worsen the objective function value, to avoid getting trapped in local optima.

Here's how Simulated Annealing works step-by-step:

Initialize the current state x and the temperature T.

Repeat until the stopping criterion is met:

Generate a new state x' by making a small random change to x.

If f(x') > f(x), accept x' as the new current state.

Otherwise, calculate the probability of accepting x's as the new current state using the Boltzmann distribution: P = e^(-ΔE/T), where ΔE = f(x') - f(x).

Create a random number, r, in the range of 0 and 1.

If r < P, except x' as the new current state.

Decrease the temperature T according to a cooling schedule.

Here's an example of using Simulated Annealing to find the global minimum of the function f(x) = x^2 - 2x + 1:

Initialize the current state x = 0 and the temperature T = 1.

Repeat until the stopping criterion is met:

Generate a new state x' by adding a random number between -0.1 and 0.1 to x.

If f(x') < f(x), accept x' as the new current state.

Otherwise, calculate the probability of accepting x' using the Boltzmann distribution: P = e^(-(f(x') - f(x))/T).

Create a random number, r, in the range of 0 and 1.

If r < P, except x' as the new current state.

Decrease the temperature T according to a cooling schedule (e.g., T = T * 0.99).

Here's the Python code for Simulated Annealing:

optimization algorithm that can be used to solve optimization problems where the solution space is too large or too complex to be searched


python code

import random

import math

def simulated_annealing(f, x0, T0, cooling_schedule):  

x = x0

T = T0

while T > 0:

x_prime = x + random.uniform(-0.1, 0.1)

delta_E = f(x_prime) - f(x)

if delta_E < 0:

x = x_prime

else:

p = math.exp(-delta_E/T)

r = random.uniform(0, 1)

if r < p:

x = x_prime

T = cooling_schedule(T)

return x

4.2  Genetic Algorithm:

Genetic Algorithm is another metaheuristic optimization algorithm that can be used to solve optimization problems where the solution space is too large or too complex to be searched exhaustively. It is inspired by the process of natural selection and evolution and operates by evolving a population of candidate solutions over multiple generations. Each candidate solution is represented as a string of genes, and the algorithm applies genetic operators such as mutation and crossover to create new candidate solutions. The fitness of each candidate solution is evaluated based on its objective function value, and the best solutions are selected for the next generation.

Here's how Genetic Algorithm works step-by-step:

Initialize a population of candidate solutions randomly.

Evaluate the fitness of each candidate solution based on its objective function value.

Repeat until the stopping criterion is met:

Select a subset of the population to be parents for the next generation, using a selection method, such as roulette wheel selection or tournament selection.

Create a new generation of candidate solutions by applying genetic operators such as mutation and crossover to the selected parents.

Evaluate the fitness of each new candidate solution based on its objective function value.

Replace the old population with the new population, using a replacement method such as elitism or generational replacement.

Here's an example of using a Genetic Algorithm to find the global minimum of the function f(x) = x^2 - 2x + 1:

1. Initialize a population of 100 candidate solutions randomly, represented as binary strings of length 10.

2. Evaluate the fitness of each candidate solution by converting its binary string to a decimal number and evaluating f(x).

3.   Continue until the halting condition is satisfied:

a. Select a subset of the population to be parents for the next generation, using a roulette wheel selection based on fitness.

b. Create a new generation of candidate solutions by applying genetic operators such as mutation and single-point crossover to the selected parents.

c. Evaluate the fitness of each new candidate solution by converting its binary string to a decimal number and evaluating f(x).

d. Replace the old population with the new population, using generational replacement.

e. If a solution with a fitness of 0 is found, stop and return it as the optimal solution.

Another example:

Here's an example of using a Genetic Algorithm to find the global minimum of the function f(x) = x^2 - 2x + 1:

1. Initialize a population of 10 candidate solutions randomly, represented as strings of binary digits: 010010, 101101, 110011, 001100, 111000, 000111, 010101, 101010, 111111, 000000.

2. Evaluate the fitness of each candidate solution by converting its binary string to a decimal number and calculating f(x).

3.  Continue until the halting condition is satisfied:

a. Select the best candidate solutions for the next generation based on their fitness: 010010, 001100, 000111, 000000.

b. Apply genetic operators (mutation and crossover) to create new candidate solutions: 010010 -> 010011 (mutation), 001100 -> 001111 (mutation), 000111 -> 000110 (mutation), 000000 -> 110011 (crossover with 010101).

c. Evaluate the fitness of each new candidate solution.

d. Replace the current population with the new population of candidate solutions.

to solve optimization problems where the solution space is too large or too complex to be searched

Here's the Python code for Genetic Algorithm:

python code

import random

def genetic_algorithm(f, n, m, k, p_mut, p_cross):

    # n: population size

    # m: number of genes per candidate solution

    # k: number of selected candidates for reproduction

    # p_mut: the probability of mutation

    # p_cross: the probability of crossover

    # initialize the population randomly

    population = [[random.randint(0, 1) for _ in range(m)] for _ in range(n)]

    # Repeat until the stopping criterion is met

    while True:

        # evaluate the fitness of each candidate's solution

        fitness = [f(candidate) for candidate in population]

        # select the best candidate solutions for reproduction

        selected_indices = sorted(range(n), key=lambda i: fitness[i])[:k]

        selected_population = [population[i] for I in selected_indices]

        # create new candidate solutions by applying genetic operators

        new_population = selected_population[:]

        while len(new_population) < n:

    parent1, parent2 = random.choices(selected_population, k=2)

            if random.random() < p_cross:

                child1 = parent1[:m//2] + parent2[m//2:]

                child2 = parent2[:m//2] + parent1[m//2:]

            else:

                child1, child2 = parent1[:], parent2[:]

            if random.random() < p_mut:

                gene_index = random.randint(0, m-1)

                child1[gene_index] = 1 - child1[gene_index]

                child2[gene_index] = 1 - child2[gene_index]

            new_population.append(child1)

            new_population.append(child2)

        # check the stopping criterion

        if f(selected_population[0]) == f(new_population[0]):

            break

        # update the population

        population = new_population[:n]

    return selected_population[0]

To Problem-Solving Uninformed Search                                

                                                   Continue  (Knowledge Representation)

Comments

Popular posts from this blog

What is Artificial Intelligence

 Introduction to Artificial Intelligence Definition of Artificial Intelligence (AI)? Artificial Intelligence (AI) refers to the creation of intelligent machines that can work and think like humans. History The historical backdrop of man-made consciousness (computer-based intelligence) traces all the way back to the 1950s when scientists initially started investigating the idea of making machines that could perform undertakings that commonly require human knowledge, like grasping the normal language, perceiving pictures, and simply deciding. Early AI research focused on developing algorithms and programs that could mimic the problem-solving abilities of human brains. This led to the creation of early AI applications such as expert systems and decision-making systems. During the 1980s and 1990s, artificial intelligence research moved towards the advancement of "AI" calculations, which permitted PCs to gain from information without being expressly customized. This led to the cre...

Artificial Intelligence Study Material

Artificial Intelligence  Learning Material  Contents of Artificial Intelligence 1.  Introduction to Artificial Intelligence What is Artificial Intelligence Brief History of AI Types of AI Applications of AI   2. Problem-Solving Uninformed Search Problem-solving methods Uninformed search algorithms (BFS, DFS, Uniform-Cost)  3.  Problem-Solving informed Search Informed search algorithms (A*, Greedy Best First, Hill Climbing) Local Search (Simulated Annealing, Genetic Algorithm)  4. Knowledge Representation and Reasoning Knowledge representation in AI Logic and Inference (Propositional Logic, First-Order Logic) Ontologies and Semantic Web Expert Systems  5. Machine Learning                 What is Machine Learning? Types of Machine Learning (Supervised, Unsupervised, Reinforcement) Regression (Linear, Logistic) Decision Trees and Random Forests Neural Networks (Perceptron, MLP, CNN, RNN)       ...

Computer Vision and Future Extraction

Computer Vision,  Image Processing  and Object Detection Computer Vision Concepts •   What is Computer Vision? •    Image Processing (Filters, Edge detection, Segmentation) •    Feature Extraction (SIFT, SURF) •    Object Detection (Haar Cascade, R-CNN) •   Deep Learning in Computer Vision (CNN)   1. What is Computer Vision? Computer vision is a field of study that involves enabling computers to interpret and understand visual data from the world around them. This includes a wide range of tasks, such as object recognition, image classification, and scene reconstruction. Computer vision is used in a variety of applications, such as self-driving cars, surveillance systems, and medical imaging. 2. Image Processing Image processing is the process of manipulating digital images to improve their quality or extract information from them. This can include techniques such as filtering, edge detection, and segmentation. Filters Image filter...

Explore Expert Systems Architecture

Expert Systems and Fuzzy Logic Expert System Topics • What are Expert Systems? • Expert Systems Architecture • Inference Engines and Rule-Based Systems • Case-Based Reasoning • Fuzzy Logic Systems 1 . What are Expert Systems Expert systems are computer programmes that simulate a human expert's decision-making process. They use a knowledge base of information and a set of rules to make decisions and provide advice in a specific domain. They are made to reason about knowledge, which is mostly represented as if-then rules, rather than to carry out a set of preprogrammed instructions, to solve complex issues. They are used in various fields such as medicine, engineering, finance, and many more. 2. Expert Systems Architecture The architecture of an expert system typically includes a knowledge base, a reasoning engine, and a user interface. The knowledge base stores the information and rules necessary to make decisions, while the reasoning engine uses that knowledge to reason about...

Research in Artificial Intelligence

Artificial Intelligence Research Topics A.I. Research and Issues  1 . Explainable AI :  This research area aims to develop AI models that can provide a clear explanation of their decision-making processes. This is important for increasing transparency, accountability, and trust in AI systems, especially in high-stakes domains such as healthcare, finance, and justice. 2. AI Safety :  The development of AI systems that are safe and reliable is a critical research area. The goal is to ensure that AI systems are secure and can operate as intended, with minimal risk of errors, bias, or harm to humans and the environment. 3. Autonomous AI :  Research in autonomous AI focuses on developing intelligent agents that can operate independently in complex, dynamic, and uncertain environments. This involves designing algorithms that can reason, plan, learn, and adapt to changing conditions, without human intervention. 4. AI for Social Good :  This research area aims...

Interview Questions and Answers in Artificial Intelligence

Artificial intelligence interview questions and answers A.I. Questions and Answers Definition of AI Artificial intelligence is the ability of machines to perform tasks that often require human intelligence (AI). This covers activities like speech recognition, language translation, and visual perception. AI is achieved by training algorithms on large datasets of relevant information, allowing machines to learn and improve their performance over time. There are various subfields of AI, including machine learning, natural language processing, and computer vision. Applications of AI A wide range of industries and applications are utilizing AI. AI is being used in healthcare to develop personalized treatment plans and improve diagnostics. Artificial intelligence is being used in finance to find fraud and predict market trends. In the transportation industry. AI is being used to improve traffic flow and enhance safety. In retail, AI is being used to provide personalized recommendations and i...

Learn Knowledge Representation of Propositional Logic

  Knowledge Representation in Prepositional  Logic and Inference Knowledge representation concepts Knowledge representation in AI Logic and Inference (Propositional Logic, First-Order Logic) Ontologies and Semantic Web Expert Systems 1. Knowledge Representation in AI: Knowledge representation is the process of transforming information into a format that can be understood and utilized by an artificial intelligence system. To enable robots to reason, learn, and make decisions based on the information they have gathered is the goal of knowledge representation in AI. There are different types of knowledge representation techniques, including: a) Semantic Networks :  A semantic network is a graphical representation of knowledge that uses nodes to represent objects and edges to represent the relationships between them. For example, a semantic network could be used to represent the relationships between different species of animals. b) Frames :  A frame is a structure tha...