在随机选择中,如何在此Matlab代码的[-10,10]之间限制x分量? [英] How can I constrain x components between [-10,10] in this matlab code, on random selection?

查看:102
本文介绍了在随机选择中,如何在此Matlab代码的[-10,10]之间限制x分量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function [xval] = simulated_annealing()
tic
for i= 0:3
    x(i+1)=floor(random(''unif'',-10,10));
end

xval=x(1)^2+x(2)^2+x(3)^2+x(4)^2;

disp (x);
t = 1 ;
bool =true;
while bool
    
   t=t*.9;
   disp (t);
  temp=true;
  count=0;
   while (temp && count<11) 
       count=count+1;
      %random selection from neighbors
      newx(1)= floor(random(''unif'',x(1)-1,x(1)+1));
      newx(2)= floor(random(''unif'',x(2)-1,x(2)+1));
      newx(3)=floor( random(''unif'',x(3)-1,x(3)+1));
      newx(4)= floor(random(''unif'',x(4)-1,x(4)+1));
      %evaluate new x value
      newxval=x(1)^2+x(2)^2+x(3)^2+x(4)^2;
      
      disp(x);
      disp(newx);
      delta = newxval- xval ;
      if delta<0
       x=newx;
       temp=false;
      else 
        p =exp(-delta/t);
         if p > 0.3
            x=newx;
            temp=false;
     
         end
      end
   
   end
   
   if count>11
       bool=false;
   end
   
   
end

toc
disp (toc);

推荐答案

认为您想使用randi而不是random:
http://www.mathworks.com/help/techdoc/ref/randi.html [ ^ ]
think you want to use randi instead of random:
http://www.mathworks.com/help/techdoc/ref/randi.html[^]


这篇关于在随机选择中,如何在此Matlab代码的[-10,10]之间限制x分量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆