Given the following Hill Climber Algorithm, convert it to a
Random Restart Hill Climber (problem, count.limit,
restart.limit) using R language.
HillClimber = function(problem, count.limit){
result = list(method = "HC", final.state = c(),status =
"",
max.frontier = 0, max.depth = 0, iterations = 0)
node = list(parent=c(), state=problem$state.initial,
actions=c(), depth=0, cost=0,
evaluation =
get.evaluation(problem$state.initial, problem))
frontier = list(node)
count = 1
while (TRUE){
if (length(frontier)==0){result$status = "No nodes in
the frontier";break}
if (count==count.limit){result$status = "HC Exceeded
Iterations";break}
firstnode = frontier[[1]];
frontier[[1]] = NULL
if
(is.final.state(firstnode$state,problem)){result$status="Solution
Found";break}
newnodes = expand.node(firstnode,
problem)
newnodes = newnodes[order(sapply(newnodes,function
(x) x$evaluation))]
newnode = newnodes[[1]]
if (firstnode$evaluation >
newnode$evaluation){
frontier = list(newnode)
} else{
result$status="Local Optimum
Found";break
}
result$max.frontier =
max(result$max.frontier,length(frontier))
result$max.depth =
max(result$max.depth,firstnode$depth)
result$iterations = count
count = count+1
}
result$final.state = firstnode
return(result)
}
Given the following Hill Climber Algorithm, convert it to a Random Restart Hill Climber (problem, count.limit, restart.l
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Given the following Hill Climber Algorithm, convert it to a Random Restart Hill Climber (problem, count.limit, restart.l
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!