Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 4.68 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Copyright (c) 2014, Simon Brodeur
  4. % All rights reserved.
  5. %
  6. % Redistribution and use in source and binary forms, with or without modification,
  7. % are permitted provided that the following conditions are met:
  8. %
  9. %  - Redistributions of source code must retain the above copyright notice,
  10. %    this list of conditions and the following disclaimer.
  11. %  - Redistributions in binary form must reproduce the above copyright notice,
  12. %    this list of conditions and the following disclaimer in the documentation
  13. %    and/or other materials provided with the distribution.
  14. %  - Neither the name of Simon Brodeur nor the names of its contributors
  15. %    may be used to endorse or promote products derived from this software
  16. %    without specific prior written permission.
  17. %
  18. % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. % ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. % WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. % IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. % INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. % NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  24. % OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. % WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. % POSSIBILITY OF SUCH DAMAGE.
  28. %
  29. 1;
  30. source ../torcs_opt.m
  31.  
  32. function pairs = pairSelection(values,iterations)
  33.   pairs = cell(1,iterations);
  34.   sumFitness = sum(cellfun(@double,values(:,2)));
  35.  
  36.   % Create Roulette
  37.   rouletteValues = zeros(iterations*2, 1);
  38.   for i=1:iterations*2
  39.     rouletteValues(i) = values{i,2}/sumFitness;
  40.   endfor
  41.   rouletteValues = sort(rouletteValues);
  42.   for i=2:iterations*2
  43.     rouletteValues(i) = rouletteValues(i-1) + rouletteValues(i);
  44.   endfor
  45.  
  46.   for k=1:iterations
  47.     idx1 = find(rouletteValues >= rand())(1);
  48.     idx2 = find(rouletteValues >= rand())(1);
  49.     pairs{k} = [values(idx1,1), values(idx2,1)];
  50.   endfor
  51. endfunction
  52.  
  53. function crossedPair = crossPairs(pair)
  54.   cutIdx = 4;
  55.   CP = cell(2,1);
  56.   if (rand() > 0.5)
  57.     CP(1,1) = [pair{1}{1}(1:cutIdx), pair{1}{2}(cutIdx+1:end)];
  58.     CP(2,1) = [pair{1}{2}(1:cutIdx), pair{1}{1}(cutIdx+1:end)];
  59.   else
  60.     CP(1,1) = pair{1}{1};
  61.     CP(2,1) = pair{1}{2};
  62.   endif
  63.   crossedPair = CP;
  64. endfunction
  65.  
  66. function mutantPair = mutatePair(pair, maxValues, minValues)
  67.   mutantPair = pair;
  68.   if (rand() > 0.90)
  69.     % Choose parameter to modify
  70.     bit = ceil(rand()*8);
  71.     %Modify the parameter
  72.     pair(bit) = pair(bit)*(rand()*(0.5 + rand()*1.0));
  73.     if (pair(bit) > maxValues(bit))
  74.       pair(bit) = maxValues(bit);
  75.     elseif (pair(bit) < minValues(bit))
  76.       pair(bit) = minValues(bit);
  77.     endif
  78.     mutantPair = pair;
  79.   endif
  80. endfunction
  81.  
  82.  
  83. % Connect to simulation server
  84. startSimulator(mode='nogui');
  85.  
  86. unwind_protect
  87.    
  88.   # Generate initial population
  89.   iterations = 40;
  90.   cvalues = cell(iterations,2);
  91.  
  92.   for i=1:iterations
  93.     cvalues(i,1) = (MAX_PARAM_VALUES - MIN_PARAM_VALUES) .* rand(1, NB_PARAMS) + MIN_PARAM_VALUES;
  94.   endfor
  95.  
  96.   epochs = 50;
  97.   newValues = cell(iterations,2);
  98.   maxIndiv = 0;
  99.   bestIndiv = cell(1);
  100.  
  101.  
  102.   %%% MODE SELECT %%%
  103.   %sportMode = 0; % Fuel Economy
  104.   sportMode = 1; % Performance
  105.  
  106.   for Z=1:epochs
  107.     % Calculate fitness
  108.     for j=1:iterations
  109.       [result, status] = evaluateParameters(cvalues{j,1}, maxEvaluationTime=1000);
  110.       if (sportMode)
  111.         cvalues{j,2} = result.topspeed;
  112.       else
  113.         cvalues{j,2} = result.distraced/result.fuelUsed;
  114.       endif
  115.     endfor
  116.     % Keep best individual
  117.     testValue = cell2mat(cvalues(:,2));
  118.     [x, ix] = max (testValue);
  119.     if (x > maxIndiv)
  120.       maxIndiv = x
  121.       bestIndiv = cvalues{ix, 1}
  122.     endif
  123.    
  124.     % Select new pairs
  125.     pairs = pairSelection(cvalues, iterations/2);
  126.    
  127.     % Cross pairs with each other ( 50% )
  128.     crossedPairs = cell(iterations/2,1);
  129.     for x=1:iterations/2
  130.       tmpPair = crossPairs(pairs(x));
  131.       crossedPairs{x} = tmpPair;
  132.     endfor
  133.  
  134.     % Mutation ( 10% )
  135.     for y=1:2:iterations/2
  136.       cvalues{y} = mutatePair(crossedPairs(y){1}{1},MAX_PARAM_VALUES,MIN_PARAM_VALUES);
  137.       cvalues{y+1} = mutatePair(crossedPairs(y){1}{2},MAX_PARAM_VALUES,MIN_PARAM_VALUES);
  138.     endfor
  139.   endfor
  140.  
  141.  
  142.   stopSimulator();
  143.  
  144. unwind_protect_cleanup
  145.     % Close the simulator
  146.     stopSimulator();
  147.   disp('All done.');
  148. end_unwind_protect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement