Metaheuristics.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Metaheuristics.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <thread>
  7. #include <chrono>
  8. #include <random>
  9. #include "ObjectiveFunction.h"
  10. #include "BinaryAntColonyAlgoritm.h"
  11. #include "GeneticAlgorithm.h"
  12. #include <fstream>
  13. #include <functional>
  14. #include <algorithm>
  15. #include <iterator>
  16. void onemax() {
  17. std::fstream file("ga_setting3.csv", std::ios::out);
  18. if (!file.is_open()) {
  19. std::cout << "Cant open file";
  20. }
  21. objective::OneMax onemax(100, 1);
  22. auto f = std::bind(&objective::OneMax::function, &onemax, std::placeholders::_1);
  23. ///*Baco instance with parameter: iteration, population, vaporization, resetThreshhold, ostream*/
  24. //Baco instance(file, 1000, 100, 0.05, 0.99);
  25. /*GeneticAlgorithm instance with parameter: iteration, population, mutateProbability, swapProbability, tournamentsize*/
  26. GeneticAlgorithm instance(file, 101, 20, 0.005, 0.7, 2.0);
  27. Solution sol = instance.execute(10, 100, f, BinaryHeuristic::ObjectiveFunctionGoal::max);
  28. std::cout << "Value: " << sol.objectiveFunctionValue << std::endl;
  29. file.close();
  30. }
  31. void knapsack() {
  32. std::fstream file("ga_setting1.csv", std::ios::out);
  33. if (!file.is_open()) {
  34. std::cout << "Cant open file";
  35. }
  36. /*GeneticAlgorithm instance with parameter: iteration, population, mutateProbability, swapProbability, tournamentsize*/
  37. GeneticAlgorithm instance(file, 100, 20, 0.01, 0.7, 2.0);
  38. //Generate Problem
  39. objective::KnapsackProblem knapsack(275.0, 2000);
  40. //generate Items
  41. double value = 5;
  42. double weight = 0;
  43. for (int i = 0; i < 100; i++) {
  44. if (i % 10 == 0) {
  45. value++;
  46. weight++;
  47. }
  48. objective::Item item;
  49. item.value = value;
  50. item.weight = weight;
  51. knapsack.itemVec.push_back(item);
  52. }
  53. //Bind as callable object
  54. auto f1 = std::bind(&objective::KnapsackProblem::function, &knapsack, std::placeholders::_1);
  55. std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
  56. Solution sol = instance.execute(10, 100, f1, BinaryHeuristic::ObjectiveFunctionGoal::max);
  57. std::cout << "Value: " << sol.objectiveFunctionValue << std::endl;
  58. std::chrono::high_resolution_clock::time_point endPoint = std::chrono::high_resolution_clock::now();
  59. std::chrono::milliseconds time = std::chrono::duration_cast<std::chrono::milliseconds>(endPoint - start);
  60. std::cout << "Time: " << time.count() << "ms";
  61. file.close();
  62. }
  63. void knapsack2() {
  64. std::fstream file("onemaxtrigger.csv", std::ios::out);
  65. if (!file.is_open()) {
  66. std::cout << "Cant open file";
  67. }
  68. /*GeneticAlgorithm instance with parameter: iteration, population, mutateProbability, swapProbability, tournamentsize*/
  69. GeneticAlgorithm instance(file, 100, 20, 0.01, 0.6, 7);
  70. /*Baco instance with parameter : iteration, population, vaporization, resetThreshhold, ostream */
  71. //Baco instance(file, 1000, 20, 0.10, 0.999);
  72. //Generate Problem
  73. objective::KnapsackProblem knapsack(275.0, 2000);
  74. //generate Items
  75. double value = 5;
  76. double weight = 0;
  77. for (int i = 1; i < 99; i++) {
  78. objective::Item item;
  79. bool trigger = false;
  80. //item.value = normal_pdf(i/100.0, 0.5, 0.5) * 10.0 * (trigger? 0:1);
  81. //std::cout << item.value << " ";
  82. item.weight = weight;
  83. knapsack.itemVec.push_back(item);
  84. }
  85. //Bind as callable object
  86. auto f1 = std::bind(&objective::KnapsackProblem::function, &knapsack, std::placeholders::_1);
  87. Solution sol = instance.execute(10, 100, objective::onemaxTrigger, BinaryHeuristic::ObjectiveFunctionGoal::max);
  88. std::cout << "Value: " << sol.objectiveFunctionValue << std::endl;
  89. file.close();
  90. }
  91. int main()
  92. {
  93. onemax();
  94. return 0;
  95. }
  96. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  97. // Debug program: F5 or Debug > Start Debugging menu
  98. // Tips for Getting Started:
  99. // 1. Use the Solution Explorer window to add/manage files
  100. // 2. Use the Team Explorer window to connect to source control
  101. // 3. Use the Output window to see build output and other messages
  102. // 4. Use the Error List window to view errors
  103. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  104. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file