⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnd13hd.m

📁 Martin T.Hagan等著,戴葵等译,神经网络设计,机械工业出版社,一书的所有例程
💻 M
📖 第 1 页 / 共 2 页
字号:
  if (rand > 0.25)
    % BANANA
    fruit_id = 3;
    fruit_x = 4+[8 6 3 1 0 1 3 6 8 6 5 6 8];
    fruit_y = [15 15 13 11 7.5 4 2 0 0 3 7.5 12 15];
    fruit_c = nnyellow;
    p1 = 1;
    p2 = 1;
  elseif (rand > 0.5)
    % ORANGE
    fruit_id = 1;
    fruit_x = [5 0 0 5 10 15 15 10];
    fruit_y = [15 10 5 0 0 5 10 15];
    fruit_c = [1 0.5 0];
    p1 = 0;
    p2 = 0;
  else
    % APPLE
    fruit_id = 2;
    fruit_x = [7.5 5 0 0 5 7.5 10 15 15 10];
    fruit_y = [13 15 10 5 0 2 0 5 10 15];
    fruit_c = [1 0 0];
    p1 = 0;
    p2 = 0;
  end

  % FLASH FRUIT INDICATOR TWICE
  set(fig,'nextplot','add')
  nnsound(blip,Fs);
  set(fruit_indicator,...
    'color',[1 1 1])
  box = plot([10 60 60 10 10],[72 72 88 88 72],...
    'color',[1 1 1],...
    'erasemode','none');

  nnpause(pause2)
  nnsound(bloop,Fs);
  set(fruit_indicator,...
    'color',[0.8 0.8 0])
  set(box,...
    'color',nndkblue)
  delete(box)
  nnpause(pause2)

  set(arrows(1),...
    'facecolor',nnred)
  t1 = clock;
  nnsound(wind,Fs);
  while etime(clock,t1) < 1, end
  nnpause(pause2)
  set(arrows(1),...
    'facecolor',nndkblue)

  set(fruit_indicator,...
    'color',[0.8 0.8 0])

  % CREATE FRUIT
  fruit = fill(x+fruit_x,y+fruit_y,fruit_c,...
    'edgecolor',fruit_c*0.5,...
    'erasemode','none');
  set(fig,'nextplot','new')
  nnsound(knock,Fs);
  nnpause(pause2)

  % MOVE FRUIT TO SENSOR #1
  deg = pi/180;
  for dx=10:10:70
    t1 = clock;
    nnsound(blp,Fs);
    set(fruit,...
      'facecolor',nnltgray,...
      'edgecolor',nnltgray);
    set(fruit,...
      'facecolor',fruit_c,...
      'edgecolor',fruit_c*0.5,...
      'xdata',fruit_x + x + dx)
    angle = angle + 20;
    adata = [0 pi]-angle*deg;
    xdata = cos(adata)*8;
    ydata = sin(adata)*8+30;
    set(lines,'color',nnred);
    set(lines(1),...
      'xdata',xdata+50,...
      'ydata',ydata,...
      'color',nndkblue)
    set(lines(2),...
      'xdata',xdata+330,...
      'ydata',ydata,...
      'color',nndkblue)
    while etime(clock,t1) < 0.1, end
  end

  % SCAN FRUIT #1
  nnpause(pause2)
  working = get(rb1,'value');
  set(fig,'nextplot','add')
  if (working)
    h1 = fill(10+[170 130 130 170],[35 35 39 39],[1 1 1],...
      'edgecolor','none',...
      'erasemode','none');
    h2 = fill(10+[170 130 130 170],[41 41 58 58],[1 1 1],...
      'edgecolor','none',...
      'erasemode','none');
    set(fruit,...
     'facecolor',[1 1 1])
    t1 = clock;
    nnsound(scan,Fs);
    while etime(clock,t1) < 1; end
  else
    t1 = clock;
    nnsound(tribble,Fs);
    while etime(clock,t1) < 1, end
    p1 = 0;
  end

  t1 = clock;
  set(p1_box,'facecolor',[1 1 1]);
  set(p1_text,'color',nndkblue);
  nnsound(blip,Fs);
  while etime(clock,t1) < 1; end
  t1 = clock;
  set(p1_box,'facecolor',nnltgray);
  set(p1_text,'string',num2str(p1),'color',nndkblue);
  nnsound(bloop,Fs);
  while etime(clock,t1) < 1; end

  if (working)
    set(h1,'facecolor',nnltgray)
    set(h2,'facecolor',nnltgray)
    delete(h1)
    delete(h2)
    set(fruit,...
      'facecolor',fruit_c)
    set(fig,'nextplot','new')
  end
  nnpause(pause2)

  % MOVE FRUIT TO SENSOR #2
  deg = pi/180;
  for dx=80:10:130
    t1 = clock;
    nnsound(blp,Fs);
    set(fruit,...
      'facecolor',nnltgray,...
      'edgecolor',nnltgray);
    set(fruit,...
      'facecolor',fruit_c,...
      'edgecolor',fruit_c*0.5,...
      'xdata',fruit_x + x + dx)
    angle = angle + 20;
    adata = [0 pi]-angle*deg;
    xdata = cos(adata)*8;
    ydata = sin(adata)*8+30;
    set(lines,'color',nnred);
    set(lines(1),...
      'xdata',xdata+50,...
      'ydata',ydata,...
      'color',nndkblue)
    set(lines(2),...
      'xdata',xdata+330,...
      'ydata',ydata,...
      'color',nndkblue)
    while etime(clock,t1) < 0.1, end
  end

  % SCAN FRUIT #2
  nnpause(pause2)
  set(fig,'nextplot','add')
  h1 = fill(70+[170 130 130 170],[35 35 39 39],[1 1 1],...
    'edgecolor','none',...
    'erasemode','none');
  h2 = fill(70+[170 130 130 170],[41 41 58 58],[1 1 1],...
    'edgecolor','none',...
    'erasemode','none');
  set(fruit,...
   'facecolor',[1 1 1])

  t1 = clock;
  nnsound(scan,Fs);
  while etime(clock,t1) < 1, end

  t1 = clock;
  set(p2_box,'facecolor',[1 1 1]);
  set(p2_text,'color',nndkblue);
  nnsound(blip,Fs);
  while etime(clock,t1) < 1; end
  t1 = clock;
  set(p2_box,'facecolor',nnltgray);
  set(p2_text,'string',num2str(p2),'color',nndkblue);
  nnsound(bloop,Fs);
  while etime(clock,t1) < 1; end

  set(h1,'facecolor',nnltgray)
  set(h2,'facecolor',nnltgray)
  delete(h1)
  delete(h2)
  set(fruit,...
    'facecolor',fruit_c)
  set(fig,'nextplot','new')
  nnpause(pause2)

  % CLASSIFY FRUIT
  W2 = get(W2_ptr,'userdata');
  n = p1+W2*p2-0.5;
  a = hardlim(n);
  if (a == 1)
    result = 1;
  else
    result = 2;
  end

  t1 = clock;
  set(n_box,'facecolor',[1 1 1]);
  set(n_text,'color',nndkblue);
  nnsound(blip,Fs);
  while etime(clock,t1) < 1; end
  t1 = clock;
  set(n_box,'facecolor',nnltgray);
  set(n_text,'string',sprintf('%5.2f',n),'color',nndkblue);
  nnsound(bloop,Fs);
  while etime(clock,t1) < 1; end

  t1 = clock;
  set(a_box,'facecolor',[1 1 1]);
  set(a_text,'color',nndkblue);
  nnsound(blip,Fs);
  while etime(clock,t1) < 1; end
  t1 = clock;
  set(a_box,'facecolor',nnltgray);
  set(a_text,'string',num2str(a),'color',nndkblue);
  nnsound(bloop,Fs);
  while etime(clock,t1) < 1; end

  % MOVE DATA FROM NETWORK TO EXIT
  if (result == 1)
    indicator = top_indicator;
    box_x = 84;
  else
    indicator = bottom_indicator;
    box_x = 62;
  end
  set(indicator,...
    'color',[1 1 1])
  axes(fig_axis)
  set(fig,'nextplot','add')
  box = plot([0 62 62 0 0]+314,[0 0 14 14 0]+box_x,...
    'color',[1 1 1],...
    'erasemode','none');

  nnpause(pause2);
  nnsound(bloop,Fs);

  % MOVE FRUIT TO EXIT
  for dx=140:10:200
    t1 = clock;
    nnsound(blp,Fs);
    set(fruit, ...
      'facecolor',nnltgray, ...
      'edgecolor',nnltgray);
    set(fruit, ...
      'facecolor',fruit_c, ...
      'edgecolor',fruit_c*0.5, ...
      'xdata',fruit_x + x + dx)
    angle = angle + 20;
    adata = [0 pi]-angle*deg;
    xdata = cos(adata)*8;
    ydata = sin(adata)*8+30;
    set(lines,'color',nnred);
    set(lines(1),...
      'xdata',xdata+50,...
      'ydata',ydata,...
      'color',nndkblue)
    set(lines(2),...
      'xdata',xdata+330,...
      'ydata',ydata,...
      'color',nndkblue)
    while etime(clock,t1) < 0.1, end
  end

  % REMOVE FRUIT
  nnpause(pause1)
  nnsound(blip,Fs);
  set(fruit,...
    'facecolor',nnltgray,...
    'edgecolor',nnltgray);
  delete(fruit)
  set(arrows(2),...
    'facecolor',nnred)
  nnsound(wind,Fs);
  set(arrows(2),...
    'facecolor',nndkblue)

  % UNLIGHT APPROPRIATE INDICATOR
  nnpause(pause1)
  nnsound(bloop,Fs);
  set(indicator,...
    'color',[0.8 0.8 0])
  set(box,...
    'color',nndkblue)
  delete(box)
  set(fig,'nextplot','new')

  % CHANGE GO BUTTON
  set(go_button,'string','Update');
  set(mode_ptr,'userdata',2);

  % SAVE DATA
  set(angle_ptr,'userdata',angle);
  set(fruit_ptr,'userdata',fruit);
  set(p1_ptr,'userdata',p1);
  set(p2_ptr,'userdata',p2);
  set(a_ptr,'userdata',a);  

%==================================================================
% Respond to fruit.
%
% ME('update')
%==================================================================

elseif strcmp(cmd,'go') & (fig) & (nargin == 1) & (mode == 2);

  % GET DATA
  blip = get(sounds(5),'userdata');
  bloop = get(sounds(6),'userdata');
  p2 = get(p2_ptr,'userdata');
  W2 = get(W2_ptr,'userdata');
  a = get(a_ptr,'userdata');

  % UPDATE WEIGHT
  W2 = W2 + 0.2*a*p2'-0.1*W2;
  if (W2 >= 10)
    W2_str = '>=10';
  else
    W2_str = sprintf('%5.2f',W2);
  end

  t1 = clock;
  set(W2_box,'facecolor',[1 1 1]);
  set(W2_text,'color',nndkblue);
  nnsound(blip,Fs);
  while etime(clock,t1) < 1; end
  t1 = clock;
  set(W2_box,'facecolor',nnltgray);
  set(W2_text,'string',W2_str,'color',nndkblue);
  nnsound(bloop,Fs);
  while etime(clock,t1) < 1; end

  % CLEAR INPUT AND OUTPUT
  set(p1_text,'color',nnltgray);
  set(p2_text,'color',nnltgray);
  set(a_text,'color',nnltgray);
  set(n_text,'color',nnltgray);
  set(p1_text,'color',nndkblue,'string','?');
  set(p2_text,'color',nndkblue,'string','?');
  set(a_text,'color',nndkblue,'string','?');
  set(n_text,'color',nndkblue,'string','?');
  
  % CHANGE GO BUTTON
  set(go_button,'string','Fruit');
  set(mode_ptr,'userdata',1);

  % SAVE DATA
  set(W2_ptr,'userdata',W2);

%==================================================================
% Clear weights.
%
% ME('clear')
%==================================================================

elseif strcmp(cmd,'clear') & (fig) & (nargin == 1)
  
  % GET DATA
  blip = get(sounds(5),'userdata');
  bloop = get(sounds(6),'userdata');

  % CLEAR WEIGHT
  W2 = 0;

  t1 = clock;
  set(W2_box,'facecolor',[1 1 1]);
  set(W2_text,'color',nndkblue);
  nnsound(blip,Fs);
  while etime(clock,t1) < 1; end
  t1 = clock;
  set(W2_box,'facecolor',nnltgray);
  set(W2_text,'string',sprintf('%5.2f',W2),'color',nndkblue);
  nnsound(bloop,Fs);
  while etime(clock,t1) < 1; end

  % CLEAR INPUT AND OUTPUT
  set(p1_text,'color',nnltgray);
  set(p2_text,'color',nnltgray);
  set(a_text,'color',nnltgray);
  set(n_text,'color',nnltgray);
  set(p1_text,'color',nndkblue,'string','?');
  set(p2_text,'color',nndkblue,'string','?');
  set(a_text,'color',nndkblue,'string','?');
  set(n_text,'color',nndkblue,'string','?');
  
  % CHANGE GO BUTTON
  set(go_button,'string','Fruit');
  set(mode_ptr,'userdata',1);

  % SAVE DATA
  set(W2_ptr,'userdata',W2);

%==================================================================
% "Working" radio button.
%
% ME('working')
%==================================================================

elseif strcmp(cmd,'working') & (fig)

  set(rb1,'value',1);
  set(rb2,'value',0);
  set(cross,'color',nndkblue);

%==================================================================
% "Not Working" radio button.
%
% ME('working')
%==================================================================

elseif strcmp(cmd,'notworking') & (fig)

  set(rb1,'value',0);
  set(rb2,'value',1);
  set(cross,'color',nnred);
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -