THMMY's "Optimization Techniques" course assignments.

84 строки
2.9 KiB

  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 = 100; % Population size
  20. maxGen = 2000; % Maximum number of generations
  21. mutationRate = 0.05; % 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. % Genetic Algorithm Execution
  27. for gen = 1:maxGen
  28. % Fitness Calculation
  29. fitness = arrayfun(@(i) fitnessFunction(pop(i, :), t, a, c, V, travelTime), 1:popSize);
  30. % Selection
  31. [~, idx] = sort(fitness); % Sort based on fitness (ascending order)
  32. pop = pop(idx, :); % Retain the best solutions
  33. % Crossover
  34. newPop(1:popSize/2, :) = pop(1:popSize/2, :); % Retain top half
  35. for i = 1:popSize/2
  36. parent1 = newPop(randi(popSize/2), :);
  37. parent2 = newPop(randi(popSize/2), :);
  38. crossPoint = randi(N);
  39. child = [parent1(1:crossPoint), parent2(crossPoint+1:end)];
  40. child = normalizeSum(child, V);
  41. newPop(popSize/2 + i, :) = child;
  42. end
  43. % Mutation
  44. for i = 1:popSize
  45. if rand < mutationRate
  46. mutationIdx = randi(N);
  47. newPop(i, mutationIdx) = rand * c(mutationIdx);
  48. newPop(i, :) = normalizeSum(newPop(i, :), V);
  49. end
  50. end
  51. % Replacement
  52. pop = newPop;
  53. end
  54. % Final Solution
  55. bestSolution = pop(1, :);
  56. bestFitness = fitnessFunction(bestSolution, t, a, c, V, travelTime);
  57. % Results
  58. disp('Best Solution [veh/min]:');
  59. disp(bestSolution);
  60. disp(['Best Objective Value: ', num2str(bestFitness), ' [min]']);
  61. % Fitness Function
  62. function T_total = fitnessFunction(x, t, a, c, V, travelTime)
  63. if abs(sum(x) - V) > 1e-6 || any(x < 0) || any(x > c)
  64. T_total = inf; % Infeasible solutions
  65. return;
  66. end
  67. T = arrayfun(@(xi, ti, ai, ci) travelTime(xi, ti, ai, ci), x, t, a, c); % Apply function to all elements
  68. T_total = sum(T .* x); % Total traversal time
  69. end