How to plot a straight line graph that ends up with a plateau using simple hill climber method

5 次查看(过去 30 天)
Hello! I'm stuck at trying to plot a graph based on a simple hill climbing method. i understand the algorithm correctly but idk how to start. as of now i'm working on trying to select a value (for the current best solution) using a random generator and then evaluate it if it's equal to 10 (goal state). if it's 10 then quit, if it isnt then select that as the best current solution.
Secondly from there take a new number and evaluate if it's equal to 10. (correct me if i'm wrong) and then if it's not equal to 10 compare it with the best current solution and see which is better. If the new number is of greater value than the current best solution then replace it with the new number as the current best solution and continue from there.
This is what I understand from the whole algorithm. Do correct me if otherwise.
The goal state is 10. The numbers i'm setting at the Y axis would be a vector of 10 integers starting from 0 to 10 and as for the x axis it will be an iteration number from 0 to 12 with 2 as the interval value.

回答(1 个)

KSSV
KSSV 2020-4-8
You have to proceed something like this:
sol = 0:10 ;
val0 = randsample(sol,1) ;
iter = 1 ;
y = zeros([],1) ;
x = zeros([],1) ;
x(1) = iter ;
y(iter) = val0 ;
while val0 ~= 10
val = randsample(sol,1) ;
if val > val0
val0 = val ;
iter = iter+1 ;
iter
x(iter) = iter ;
y(iter) = val0 ;
end
end
plot(x,y)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by