Assignment 03 Ai
Assignment 03 Ai
ASSIGNMENT # 03
Solution:
a. Hill climbing algorithm: Local beam search algorithm keeps track of k states
rather than just one. It begins with randomly generated states and at each step all
the successors of all the states are generated. If any one is a goal, the algorithm
halts otherwise it selects the k best successors from the complete list and repeats
(Pages 125-126). If k=1 then it is the same as Hill climbing algorithm.
b. Breadth-first search (BFS) algorithm: Local beam search with one initial state
and no limit on the number of states retained is similar to Breadth-First Search as it
starts from one initial state and it adds one complete layer of nodes before adding
the next layer.
c. First-Choice Hill Climbing algorithm: Simulated annealing with T=0 at all times
(and omitting the termination test) is First-Choice Hill Climbing algorithm since every
downward successor would be rejected with the probability 11.
d. Random walk search algorithm: Simulated annealing with T=∞ at all times will
always accept every successor then it is similar to Random walk search algorithm.
e. Random walk search algorithm: In Genetic algorithm with population size N=1,
the two selected parents will be the same and crossover will produce the same
individual and thus there is no mutation. Hence, this is similar to random walk.
Question 4.2
The heuristic path algorithm is a best-first search in which the objective
function is f(n) = (2w)g(n) + wh(n). For what values of w is this algorithm
guaranteed to be optimal? (You may assume that his admissable.) What kind
of search does this perform when w = 0? When w = 1? When w = 27.
Solution:
The heuristic path algorithm is guaranteed to be optimal when 0≤w≤1. This is
because for w=1, the algorithm becomes A* search, a well-known informed search
algorithm that is guaranteed to find the optimal solution if the heuristic function h(n) is
admissible (i.e., it never overestimates the true cost to reach the goal from node n).
When w=0,
the algorithm becomes a pure greedy best-first search, where only the heuristic
value h(n) is considered. Greedy best-first search tends to be fast but is not
guaranteed to find the optimal solution.
For 0<w<1,
the algorithm balances between the actual cost g(n) and the heuristic estimate h(n).
It takes into account both the current cost and the estimated cost to the goal, making
it a weighted combination of a greedy search and an optimal search.
When w>1,
the algorithm becomes suboptimal. The higher the value of w, the more the algorithm
prioritizes the heuristic estimate over the actual cost. In your example, when w=27,
the algorithm heavily weighs the heuristic value, potentially making it more greedy
and less concerned about the true cost to reach the goal. This can lead to
suboptimal solutions.
Question 4.3
Prove each of the following statements:
a. Breadth-first search is a special case of uniform-cost search.
b. Breadth-first search, depth-first search, and uniform-cost search are special
cases of best-first search.
c. Uniform-cost search is a special case of A search.
Solution:
Let's prove each statement:
In uniform-cost search, the cost function is the actual cost from the start node to the
current node. In breadth-first search (BFS), the cost of each edge is typically
considered to be the same (often equal to 1). Therefore, for BFS, the cost function is
equivalent to the level of the node in the search tree.
In uniform-cost search, the cost is the sum of edge costs. If we assign a cost of 1 to
each edge in a BFS tree, then the cost in uniform-cost search will be equal to the
level of the node in the BFS tree. Thus, BFS is a special case of uniform-cost search
where all edge costs are equal.
Best-first search is a general search strategy where nodes are expanded based on
some evaluation function. For breadth-first search and uniform-cost search, the
evaluation function is the cost of the path to the current node. For depth-first search,
the evaluation function is the depth or level of the current node.
1. BFS as a Special Case:
In BFS, the evaluation function is the level of the node.
If we set the edge costs to 1, then the level of the node is also the cost
of the path.
Therefore, BFS is a special case of best-first search.
2. DFS as a Special Case:
In DFS, the evaluation function is the depth or level of the node.
If we set all edge costs to 1, then the depth of the node is also the cost
of the path.
Therefore, DFS is a special case of best-first search.
3. Uniform-Cost Search as a Special Case:
In uniform-cost search, the evaluation function is the cost of the path to
the current node.
If we set all edge costs to 1, then the cost of the path is equal to the
number of edges traversed.
Therefore, uniform-cost search is a special case of best-first search.
In A* search, the evaluation function is f(n)=g(n)+h(n), where g(n) is the cost of the
path from the start node to n, and h(n) is the heuristic estimate of the cost from n to
the goal.
For uniform-cost search, there is no heuristic h(n)=0), and the evaluation function
becomes f(n)=g(n). In other words, the cost function in uniform-cost search only
considers the actual cost of the path.
Therefore, uniform-cost search is a special case of A* search where the heuristic is
always zero h(n)=0), and the evaluation function simplifies to f(n)=g(n).
Question 4.6
Invent a heuristic function for the 8-puzzle that sometimes overestimates, and
show how it can lead to a suboptimal solution on a particular problem. (You
can use a computer to help if you want.) Prove that, if h never overestimates
by more than c, A* using h returns a solution whose cost exceeds that of the
optimal solution by no more than c.
Solution:
Let's consider the 8-puzzle, where you have a 3x3 grid with 8 numbered tiles and an
empty space. The goal is to reach a specified goal state from a given initial state by
sliding tiles into the empty space.
This heuristic counts each misplaced tile twice, effectively overestimating the true
cost to reach the goal.
Now, let's demonstrate how this heuristic can lead to a suboptimal solution. Consider
the following initial state:
1 2 3
0 4 5
6 7 8
1 2 3
4 5 6
7 8 0
Here, '0' represents the empty space. The optimal solution is to move the empty
space (0) to the bottom-right corner. However, using hoverestimate, the heuristic value is
4 (since 4 tiles are misplaced). This overestimation might mislead A* into exploring
paths that seem shorter but lead to suboptimal solutions.
Now, let's prove the statement:
Statement: If h never overestimates by more than c, A* using h returns a solution
whose cost exceeds that of the optimal solution by no more than c.
Proof:
Let h(n) be the true cost to reach the goal state from node n, and let h∗(n) be the
actual cost. Then, h(n)≤h∗(n)+c for all nodes n and some constant c.
A* always selects the node with the lowest f(n)=g(n)+h(n) to expand. If h never
overestimates by more than c, then f(n) for any node n is at most g(n)+h∗(n)+c.
Now, let n optimal be the node on the optimal path to the goal. The cost of the
optimal solution is g(n optimal). A* is guaranteed to find this node and expand it.
Let n suboptimal be the node selected by A* that leads to a suboptimal solution.
The cost of the suboptimal solution is g(n suboptimal). Since A* always selects the
node with the lowest f(n), we have:
Rearranging terms:
This proves that the cost of the suboptimal solution is within c of the optimal
solution's cost.
Question 4.7
Prove that if a heuristic is consistent, it must be admissible. Construct an
admissible heuristic that is not consistent.
Solution:
Let's start with the definitions:
1. Admissible Heuristic:
A heuristic h is admissible if it never overestimates the true cost to
reach the goal from any given state. Formally, for every node n,
h(n)≤h∗(n), where h∗(n) is the true cost to reach the goal from n.
2. Consistent Heuristic (or Monotonic Heuristic):
A heuristic h is consistent if, for every node n and every successor n′ of
n generated by any action a, the estimated cost of reaching the goal
from n is no greater than the cost of getting from n to n′ plus the
estimated cost of reaching the goal from n′. Formally, h(n)≤c(n, a,
n′)+h(n′), where c(n, a, n′) is the cost of the action a from n to n′.
Proof:
Let's consider the last action a∗ on the optimal path from n to the goal state, leading
to the goal state n′. According to consistency:
This heuristic counts the number of tiles that are not in their goal position. It is
admissible because it never overestimates the cost to reach the goal; however, it is
not consistent.
For example,
Consider the state n with one misplaced tile. Suppose the optimal path involves a
single action to move this tile to its correct position. According to the consistency
condition, we would expect h(n) to be less than or equal to the cost of this action,
which is 1, plus h(n′) where n′ is the state after the action. But h(n′) is 0 (since it's the
goal state), and h(n) is 1, violating consistency.
This demonstrates that hadmissible is admissible but not consistent.
Question 4.11
Give the name of the algorithm that results from each of the following special
cases:
a. Local beam search with k = 1.
b. Local beam search with one initial state and no limit on the number of states
retained.
c. Simulated annealing with T = 0 at all times (and omitting the termination
test).
d. Genetic algorithm with population size N = 1.
Solution:
Here are the names of the algorithms for each of the given special cases:
b. Local beam search with one initial state and no limit on the number of states
retained:
This scenario is essentially a hill-climbing search. In hill climbing, only the
best successor is retained at each step, and there is only one initial state.
Question 4.12
Sometimes there is no good evaluation function for a problem, but there is a
good comparison method: a way to tell whether one node is better than
another, without assigning numerical values to either. Show that this is
enough to do a best-first search. Is there an analog of A*?
Solution:
Analog of A* :
While there might not be a numerical evaluation function, the comparison method
should be able to order nodes by preference or desirability. The algorithm relies on
the comparison method to determine the order in which nodes are explored.
Regarding an analog of A*, which combines best-first search with the benefits of an
admissible heuristic, it becomes more challenging without a numerical evaluation
function. A* uses the evaluation function f(n)=g(n)+h(n) to guide the search, where
g(n) is the cost to reach node n from the start, and h(n) is the heuristic estimate of
the cost to reach the goal from n.
Without numerical values, it's difficult to incorporate a heuristic in the same way A*
does.
However, one might consider a variation of A* that uses a qualitative or ordinal
heuristic. The comparison method could still determine which nodes are better than
others based on the heuristic, even if the heuristic values are not directly comparable
in a numerical sense.
Question 4.13
Relate the time complexity of LRTA* to its space complexity.
Solution:
LRTA* (Learning Real-Time A*) is an online search algorithm that combines ideas
from A* search and reinforcement learning. Unlike traditional A*, LRTA* doesn't
require a complete heuristic or a predefined admissible heuristic function. Instead, it
updates its heuristic estimates based on the actual costs encountered during the
search.
Time Complexity:
Question 4.14
Suppose that an agent is in a 3 x 3 maze environment like the one shown in
Fig- ure 4.18. The agent knows that its initial location is (1,1), that the goal is at
(3,3), and that the four actions Up, Down, Left, Right have their usual effects
unless blocked by a wall. The agent does not know where the internal walls
are. In any given state, the agent perceives the set of legal actions; it can also
tell whether the state is one it has visited before or a new state.
a. Explain how this online search problem can be viewed as an offline search
in belief state space, where the initial belief state includes all possible
environment configurations. How large is the initial belief state? How large is
the space of belief states?
b. How many distinct percepts are possible in the initial state?
c. Describe the first few branches of a contingency plan for this problem. How
large (roughly) is the complete plan?
Notice that this contingency plan is a solution for every possible environment
fitting the given description. Therefore, interleaving of search and execution is
not strictly necessary even in unknown environments.
Solution:
In this online search problem, the agent perceives the set of legal actions and
whether the state is new or previously visited. To view this as an offline search
problem in belief state space, we consider the agent's belief about the environment's
configuration.
Initial Belief State: The initial belief state includes all possible configurations
of the environment, given that the agent does not know the locations of
internal walls. This means there are multiple possible mazes consistent with
the agent's knowledge.
Size of Initial Belief State: If each cell in the maze can be either open or
blocked, there are 2m × n possible configurations, where m × n is the size of
the maze (in this case, 3×33×3). So, the initial belief state is
23×3=29=51223×3=29=512 possible configurations.
Space of Belief States: The space of belief states evolves as the agent
explores the environment. At each step, the belief state is updated based on
the agent's observations and actions.
c. Contingency Plan:
The complete plan size is influenced by the branching factor at each step and the
depth of the search. Given the uncertainty and contingency planning involved, the
complete plan may be quite large, especially as the agent explores different
possibilities in belief state space.
Interleaving of search and execution is not strictly necessary because the
contingency plan considers all possible environment configurations, allowing the
agent to adapt to the actual environment as it explores.
Question 5.4
Consider the problem of constructing (not solving) crossword puzzles: fitting
words into a rectangular grid. The grid, which is given as part of the problem,
specifics which squares are blank and which are shaded. Assume that a list of
words (i.e., a dictionary) is provided and that the task is to fill in the blank
squares using any subset of the list. Formulate this problem precisely in two
ways:
a. As a general search problem. Choose an appropriate search algorithm, and
specify a heuristic function, if you think one is needed. Is it better to fill in
blanks one letter at a time or one word at a time?
b. As a constraint satisfaction problem. Should the variables be words or
letters? Which formulation do you think will be better? Why?
Solution:
State Space:
The state space consists of different configurations of words filled into the crossword
grid. Each state represents a possible arrangement of words in the grid.
Initial State:
The initial state is an empty grid.
Actions:
The actions involve placing words from the dictionary into the blank squares of the
grid. The order in which words are placed can be determined by the search
algorithm.
Goal Test:
The goal is to fill in all the blank squares such that the words form a valid crossword
puzzle.
Search Algorithm:
Depth-First Search (DFS) can be a suitable algorithm for this problem. It explores a
branch of the search space deeply before moving on to another branch. The order in
which words are placed can be crucial, and DFS allows exploring different
possibilities efficiently.
Heuristic Function:
A heuristic function can be designed to estimate the remaining number of blank
squares that can be filled by the words in the dictionary. This heuristic can guide the
search to prioritize filling blank squares that are part of multiple potential words.
Filling Blanks:
It might be more efficient to fill in blanks one word at a time rather than one letter at a
time. This approach allows the search to explore configurations where words interact
with each other, potentially leading to more coherent and interesting crossword
puzzles.
Variables:
The variables represent the words to be placed in the grid. Each variable
corresponds to a word from the dictionary.
Domains:
The domain of each variable is the set of possible locations where the word can be
placed in the grid.
Constraints:
Constraints enforce that the words placed in the grid do not conflict with each other.
This includes constraints on the intersections of words, ensuring that letters match
where words intersect.
CSP Formulation:
The crossword puzzle problem can be naturally represented as a CSP, where the
constraints capture the relationships between words.
The CSP formulation is likely to be more suitable for the crossword puzzle problem.
This is because CSPs provide a natural way to model constraints on the
relationships between variables (words) and allow for systematic exploration of
possible configurations. The constraints in the CSP formulation capture the
interactions between words, ensuring that the puzzle is coherent and follows the
rules of crossword construction.
Additionally, the use of backtracking or other constraint satisfaction algorithms can
efficiently explore the solution space and handle the interdependencies between
words in the grid.
Question 5.5
Give precise formulations for each of the following as constraint satisfaction
problems:
a. Rectilinear floor-planning: find nonoverlapping places in a large rectangle
for a num- ber of smaller rectangles.
b. Class scheduling: There is a fixed number of professors and classrooms, a
list of classes to be offered, and a list of possible time slots for classes. Each
professor has a set of classes that he or she can teach.
Solution:
a. Rectilinear Floor-Planning:
Variables:
Let Xi represent the placement of rectangle i in the large rectangle.
Domains:
The domain of each variable Xi includes all possible non-overlapping positions within
the large rectangle.
Constraints:
1. Non-Overlapping Constraints:
For each pair of rectangles i and j, Xi and Xj must not overlap.
2. Fit Within Large Rectangle:
The placement of each rectangle Xi must fit within the boundaries of
the large rectangle.
Objective:
The objective is to find a configuration of non-overlapping placements for all
rectangles that maximizes the overall utilization of the large rectangle.
b. Class Scheduling:
Variables:
Let Xijt represent the assignment of class i to professor j at time slot t.
Domains:
The domain of each variable Xijt includes all possible combinations of professors,
classrooms, and time slots
.
Constraints:
Objective:
The objective is to find a feasible assignment of classes to professors, classrooms,
and time slots that satisfies the constraints.
Note: Additional constraints or considerations, such as room capacity, class size, and
preferences, can be incorporated based on specific requirements.
Question 5.12
Suppose that a graph is known to have a cycle cutset of no more than k nodes.
Describe a simple algorithm for finding a minimal cycle cutset whose runtime
is not much more than O(n) for a CSP with n variables. Search the literature for
methods for finding approximately minimal cycle cutsets in time that is
polynomial in the size of the cutset. Does the existence of such algorithms
make the cycle cutset method practical?
Solution:
A cycle cutset is a set of nodes whose removal from a graph makes it acyclic. The
problem of finding a minimal cycle cutset in a graph can be challenging, but certain
algorithms aim to address this efficiently. Here's a simple algorithm and an overview
of methods for finding approximately minimal cycle cutsets:
1. Detect Cycles:
Use a cycle detection algorithm (e.g., depth-first search) to find cycles
in the graph.
2. Identify Cutsets:
For each cycle found, identify the set of nodes that form a cutset when
removed.
3. Select Minimal Cutset:
Among the identified cutsets, choose the one with the minimum
cardinality (size).
This simple algorithm relies on the fact that if a graph has a cycle cutset of no more
than k nodes, the removal of a minimal cycle cutset results in an acyclic graph.
Several methods aim to find approximately minimal cycle cutsets efficiently. These
methods often involve heuristic or approximation algorithms. Here are a few
approaches:
1. Iterative Improvement Methods:
Methods that iteratively improve the quality of the cutset by adding or
removing nodes while maintaining acyclicity.
2. Greedy Algorithms:
Greedy algorithms that make locally optimal decisions to construct a
cycle cutset.
3. Randomized Algorithms:
Randomized algorithms that use randomness to quickly find a good
approximation of a minimal cycle cutset.
4. Hybrid Methods:
Hybrid methods that combine different techniques for better efficiency
and accuracy.
Question 5.12
Consider the following logic puzzle: In five houses, each with a different color,
live 5 persons of different nationalities, each of whom prefer a different brand
of cigarette, a different drink, and a different pet. Given the following facts, the
question to answer is "Where does the zebra live, and in which house do they
drink water?" The Englishman lives in the red house. The Spaniard owns the
dog. The Norwegian lives in the first house on the left. Kools are smoked in the
yellow house. The man who smokes Chesterfields lives in the house next to
the man with the fox. The Norwegian lives next to the blue house. The Winston
smoker owns snails. The Lucky Strike smoker drinks orange juice. The
Ukrainian drinks tea. The Japanese smokes Parliaments. Kools are smoked in
the house next to the house where the horse is kept. Coffee is drunk in the
green house. The Green house is immediately to the right (your right) of the
ivory house. Milk is drunk in the middle house. Discuss different
representations of this problem as a CSP. Why would one prefer one repre-
sentation over another?
Solution:
Representing the Zebra Puzzle as a Constraint Satisfaction Problem (CSP) involves
defining variables, domains, and constraints to capture the given facts and
requirements. Different representations of the problem are possible, each with its
advantages and considerations. Here are a few possible representations:
Variables:
Color
Nationality
Cigarette
Drink
Pet
Domains:
Each variable has a domain of five values (one for each house).
Constraints:
1. Uniqueness Constraints:
Each variable must take on a unique value within the domain.
2. Fact-Based Constraints:
Express the given facts as constraints, such as "The Englishman lives
in the red house," "The Norwegian lives in the first house on the left,"
etc.
3. Neighbor Constraints:
Constraints based on the proximity of houses, such as "The Norwegian
lives next to the blue house."
4. Relative Position Constraints:
Expressions like "The Green house is immediately to the right of the
ivory house."
This representation emphasizes the relationship between variables and values,
making it easy to encode constraints based on the given facts.
Variables:
Houses (1 to 5)
Domains:
Each house has attributes (color, nationality, cigarette, drink, pet).
Constraints:
1. Uniqueness Constraints:
Each attribute within a house must be unique.
2. Fact-Based Constraints:
Encode the given facts using specific entries in the matrix.
3. Neighbor Constraints:
Constraints involving the proximity of houses, such as "The Norwegian
lives next to the blue house."
4. Relative Position Constraints:
Use the matrix to represent relative positions, such as "The Green
house is immediately to the right of the ivory house."
This representation organizes the information into a matrix, making it easier to see
relationships between houses and attributes. However, it may require more complex
indexing and is less symbolic than the variable-value representation.