Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.92 KB | None | 0 0
  1. function [ fitness ] = twoPole_test( wMat, activateNet, targetFitness, varargin)
  2. %TWOPOLE_TEST Double pole balancing
  3. %   See cart_pole2.m for details and original authors
  4. %   state = [ x           <- the cart position
  5. %             x_dot       <- the cart velocity
  6. %             theta       <- the angle of the pole
  7. %             theta_dot   <- the angular velocity of the pole.
  8. %             theta2      <- the angle of the 2nd pole
  9. %             thet2a_dot  <- the angular velocity of the 2nd pole.
  10. %
  11. % environment parameters (these MUST match the corresponding values in cart_pole.m)
  12.  
  13. %% Initialization
  14. vis = false;
  15. initial_state = [0 0 .017 0 0.0 0]';
  16. simLength = targetFitness;
  17.  
  18. if nargin > 3
  19.     if strcmp(varargin{end},'vis')
  20.         vis = true;
  21.         axis_range = [-3 3 0 2];
  22.         hf = figure; % generate a new figure to be used by cpvisual()
  23.         %hf2 =figure; % generate a new figure to be used by cpvisual2()
  24.         tic; % initialize timer, which is used to make animation with cpvisual()
  25.         % more realistic.
  26.     end
  27.     if strcmp(varargin{1},'setInit')
  28.         initial_state = varargin{2};
  29.     end
  30. end
  31.  
  32.  
  33. pole_length = 0.5*2; % full pole length (not just half as in cart_pole.m)
  34. pole_length2 = 0.05*2; % full pole length (not just half as in cart_pole.m)
  35. tau = 0.01; % time between each step (in s)
  36. state = initial_state; % initial state (note, it is a column vector) (1 degree = .017 rad)
  37. force = 10;
  38. steps = 0;
  39.  
  40. bias = 1;
  41.  
  42. scaling = [ 2.4 10.0 0.628329 5 0.628329 16]';
  43. activation = zeros(1,length(wMat));
  44.  
  45. %% Sim loop
  46. while ( abs(state(3)) < pi/2 && ...    % Pole #1 Balanced
  47.         abs(state(5)) < pi/2 && ...    % Pole #2 Balanced
  48.         abs(state(1)) < 2.16 && ...    % Cart still on track
  49.         abs(state(2)) < 1.35 && ...    % Cart never too fast
  50.         steps < simLength)
  51.    
  52.     steps = steps + 1;
  53.    
  54.     %% Action Selection
  55.     scaledInput = state./scaling;
  56.     input = scaledInput';
  57.  
  58.     activation = feval(activateNet,wMat,[bias input],activation);
  59.     output = activation(end);
  60.    
  61.     action = (-0.5+output).*2.*force;
  62.    
  63.     if vis
  64.         % Visualization
  65.         while toc < tau, end % wait until at least the time between state
  66.         % changes, tau, has passed since the last graph.
  67.         cpvisual( hf, pole_length, state(1:4), axis_range, action );
  68.         %cpvisual( hf2, pole_length2, state([1 2 5 6]), axis_range, action );
  69.         tic; % reset timer (used for realistic visualization)
  70.     end
  71.    
  72.     % Take action and return new state:
  73.     state = cart_pole2( state, action );
  74.    
  75.     scaledState = state./scaling;
  76.     f2_den(steps,:) = scaledState([1:4]);
  77.     %% Fitness Calculation
  78.     % Oscillation penalizing fitness.
  79.     f1 = steps; %/ targetFitness;
  80.     if steps > 100
  81.         f2 = sum((sum (abs(  f2_den((end-99:end),:)  ))));
  82.         f2 = (1/f2) * 0.75;
  83.     else
  84.         f2 = 0;
  85.     end
  86.     fitness = 0.1*f1 + 0.9*f2;
  87. end
  88.  
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement