Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. function [fitness, steps] = twoPole_test(wMat, activateNet, p, 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 = p.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.  
  57. if p.velInfo
  58. input = scaledInput';
  59. else
  60. input = scaledInput([1 2 3 5])';
  61. end
  62.  
  63. for loops=1:2
  64. activation = feval(activateNet,wMat,[bias input],activation);
  65. end
  66. output = activation(end);
  67. action = (-0.5+output).*2.*force;
  68.  
  69. if vis
  70. % Visualization
  71. while toc < tau, end % wait until at least the time between state
  72. % changes, tau, has passed since the last graph.
  73. cpvisual( hf, pole_length, state(1:4), axis_range, action );
  74. cpvisual( hf, pole_length2, state([1 2 5 6]), axis_range, action );
  75. tic; % reset timer (used for realistic visualization)
  76. end
  77.  
  78. % Take action and return new state:
  79. state = cart_pole2( state, action );
  80. scaledState = state./scaling;
  81. f2_den(steps,:) = scaledState([1:4]);
  82. end
  83.  
  84. %% Fitness Calculation
  85. % Oscillation penalizing fitness.
  86. f1 = steps/p.targetFitness;
  87. if steps > 100
  88. f2 = sum((sum ( abs(f2_den((end-99:end),:)) )));
  89. f2 = (1/f2) * 0.75;
  90. else
  91. f2 = 0;
  92. end
  93. fitness = 0.1 * f1 + 0.9 * f2;
  94.  
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement