THMMY's "Optimization Techniques" course assignments.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

2 тижднів тому
2 тижднів тому
2 тижднів тому
2 тижднів тому
2 тижднів тому
2 тижднів тому
2 тижднів тому
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. % Genetic Algorithm for Minimizing Network Traversal Time
  2. clc;
  3. clear;
  4. close all;
  5. % Problem Parameters
  6. N = 17; % Number of roads
  7. t = 1.5 * ones(1, N); % Fixed time for each road
  8. a = [1.25 * ones(1, 5), 1.5 * ones(1, 5), ones(1, 7)]; % Weighting factor
  9. c = [
  10. 54.13, 21.56, 34.08, 49.19, 33.03, 21.84, 29.96, 24.87, 47.24, 33.97, ...
  11. 26.89, 32.76, 39.98, 37.12, 53.83, 61.65, 59.73]; % Road capacities
  12. V = 100; % Incoming vehicle rate
  13. % Travel Time Function
  14. travelTime = @(xi, ti, ai, ci) ti + ai * xi / (1 - xi / ci);
  15. % Normalization Function (Infinite norm normalized to S)
  16. normalizeSum = @(x, S) (x ./ sum(x)) * S; % Ensure sum of single row x equals S
  17. normalizeSum2 = @(x, S) (x ./ sum(x, 2)) * S; % Ensure sum of each row of 2D matrix x equals S
  18. % Genetic Algorithm Parameters
  19. popSize = 36; % Population size
  20. maxGen = 2000; % Maximum number of generations
  21. mutationRate = 0.1; % Mutation probability
  22. % Initialize Population
  23. pop = rand(popSize, N) .* c; % Random initial solutions (0 <= x <= c)
  24. pop = normalizeSum2(pop, V); % Ensure sum of each solution equals V
  25. newPop = zeros(popSize, N); % Pre-allocate new population buffer
  26. bestFitness = zeros(maxGen, 1); % Result array
  27. % Genetic Algorithm Execution
  28. for gen = 1:maxGen
  29. % Fitness Calculation
  30. fitness = arrayfun(@(i) fitnessFunction(pop(i, :), t, a, c, V, travelTime), 1:popSize);
  31. % Selection
  32. [~, idx] = sort(fitness); % Sort based on fitness (ascending order)
  33. pop = pop(idx, :); % Retain the best solutions
  34. % Keep the best chromosome
  35. bestFitness(gen) = fitnessFunction(pop(1, :), t, a, c, V, travelTime);
  36. % Crossover
  37. newPop(1:popSize/2, :) = pop(1:popSize/2, :); % Retain top half
  38. for i = 1:popSize/2
  39. parent1 = newPop(randi(popSize/2), :);
  40. parent2 = newPop(randi(popSize/2), :);
  41. crossPoint = randi(N);
  42. child = [parent1(1:crossPoint), parent2(crossPoint+1:end)];
  43. child = normalizeSum(child, V);
  44. newPop(popSize/2 + i, :) = child;
  45. end
  46. % Mutation
  47. for i = 1:popSize
  48. if rand < mutationRate
  49. mutationIdx = randi(N);
  50. newPop(i, mutationIdx) = rand * c(mutationIdx);
  51. newPop(i, :) = normalizeSum(newPop(i, :), V);
  52. end
  53. end
  54. % Replacement
  55. pop = newPop;
  56. end
  57. % Results
  58. bestSolution = pop(1, :);
  59. disp('Best Solution [veh/min]:');
  60. disp(bestSolution);
  61. disp(['Best Objective Value: ', num2str(bestFitness(end)), ' [min]']);
  62. figure('Name', 'Time over generations', 'NumberTitle', 'off');
  63. set(gcf, 'Position', [100, 100, 960, 640]); % Set the figure size
  64. plot(1:maxGen, bestFitness, '-b', 'LineWidth', 1);
  65. % Customize the plot
  66. title(['Population = ', num2str(popSize), ' - Mutation = ', num2str(mutationRate)], 'Interpreter', 'latex', 'FontSize', 16); % Title of the plot
  67. xlabel('Generations') ;
  68. ylabel('T_{total}');
  69. % save the figure
  70. print(gcf, ['figures/constV_pop_', num2str(popSize), 'mut_', num2str(mutationRate), '.png'], '-dpng', '-r300');
  71. % Fitness Function
  72. function T_total = fitnessFunction(x, t, a, c, V, travelTime)
  73. if abs(sum(x) - V) > 1e-6 || any(x < 0) || any(x > c)
  74. T_total = inf; % Infeasible solutions
  75. return;
  76. end
  77. T = arrayfun(@(xi, ti, ai, ci) travelTime(xi, ti, ai, ci), x, t, a, c); % Apply function to all elements
  78. T_total = sum(T .* x); % Total traversal time
  79. end