项目作者: chen0040

项目描述 :
Grammatical evolution implemented using C#
高级语言: C#
项目地址: git://github.com/chen0040/cs-grammatical-evolution.git
创建时间: 2017-10-31T01:11:12Z
项目社区:https://github.com/chen0040/cs-grammatical-evolution

开源协议:MIT License

下载


cs-grammatical-evolution

Grammatical evolution implemented using C#

Install

  1. Install-Package cs-grammatical-evolution -Version 1.0.4

Usage

The sample code belows show how to use the Grammatical Evolution to solve the spiral classification problem:

  1. class Program
  2. {
  3. static DataTable LoadData(string filename)
  4. {
  5. DataTable table = new DataTable();
  6. table.Columns.Add("X");
  7. table.Columns.Add("Y");
  8. table.Columns.Add("Label");
  9. int line_count = 0;
  10. using (StreamReader reader = new StreamReader(filename))
  11. {
  12. string line = reader.ReadLine();
  13. int.TryParse(line, out line_count);
  14. while ((line = reader.ReadLine()) != null)
  15. {
  16. string[] elements = line.Split(new char[] { '\t' });
  17. double x, y;
  18. int label;
  19. double.TryParse(elements[0].Trim(), out x);
  20. double.TryParse(elements[1].Trim(), out y);
  21. int.TryParse(elements[2].Trim(), out label);
  22. table.Rows.Add(x, y, label);
  23. }
  24. }
  25. return table;
  26. }
  27. static void CreateGrammar(GEPop<GESolution> pop)
  28. {
  29. pop.GERuleSet.RemoveAllRules();
  30. //pop.GERuleSet.AddRule("<expr>", "<lbracket>", "<expr>", "<biop>", "<expr>", "<rbracket>");
  31. pop.GERuleSet.AddRule("<expr>", "<lbracket>", "<expr>", "<biop>", "<expr>", "<rbracket>");
  32. pop.GERuleSet.AddRule("<expr>", "<pre-op>", "<lbracket>", "<expr>", "<rbracket>");
  33. //pop.GERuleSet.AddRule("<expr>", "<pre-op-2>", "<lbracket>", "<expr>", "<comma>", "<expr>", "<rbracket>");
  34. pop.GERuleSet.AddRule("<expr>", "<var>");
  35. pop.GERuleSet.AddRule("<biop>", "+");
  36. pop.GERuleSet.AddRule("<biop>", "-");
  37. pop.GERuleSet.AddRule("<biop>", "/");
  38. pop.GERuleSet.AddRule("<biop>", "*");
  39. //pop.GERuleSet.AddRule("<comma>", ",");
  40. //pop.GERuleSet.AddRule("<pre-op-2>", "Pow");
  41. pop.GERuleSet.AddRule("<pre-op>", "Sin");
  42. //pop.GERuleSet.AddRule("<pre-op>", "Tan");
  43. pop.GERuleSet.AddRule("<pre-op>", "Cos");
  44. pop.GERuleSet.AddRule("<lbracket>", "(");
  45. pop.GERuleSet.AddRule("<rbracket>", ")");
  46. pop.GERuleSet.AddRule("<var>", "X");
  47. pop.GERuleSet.AddRule("<var>", "Y");
  48. pop.GERuleSet.AddRule("<var>", "C1");
  49. }
  50. static void CreateTerminalSet(GEPop<GESolution> pop)
  51. {
  52. pop.OperatorSet.AddOperator("+");
  53. pop.OperatorSet.AddOperator("-");
  54. pop.OperatorSet.AddOperator("/");
  55. pop.OperatorSet.AddOperator("*");
  56. pop.OperatorSet.AddOperator("Sin");
  57. pop.OperatorSet.AddOperator("Cos");
  58. pop.OperatorSet.AddOperator("(");
  59. pop.OperatorSet.AddOperator(")");
  60. //pop.OperatorSet.AddOperator("Tan");
  61. //pop.OperatorSet.AddOperator("Pow");
  62. pop.ConstantSet.AddConstant("C1", 8);
  63. pop.VariableSet.AddVariable("X");
  64. pop.VariableSet.AddVariable("Y");
  65. pop.BuildTerminalSet();
  66. }
  67. static void Main(string[] args)
  68. {
  69. DataTable table = LoadData("dataset.txt");
  70. GEConfig config = new GEConfig("GEConfig.xml");
  71. GEPop<GESolution> pop = new GEPop<GESolution>(config);
  72. CreateTerminalSet(pop);
  73. //User can load BNF grammar from file or create programmatic ally
  74. pop.GERuleSet.LoadGrammar("grammar.bnf");
  75. Console.WriteLine(pop.GERuleSet.ToString());
  76. //CreateGrammar(pop);
  77. //Xianshun: Say if you implement a operator named "Move(X, Y)",
  78. //Which returns a value 1 if X > Y otherwise 0
  79. //use the following code to enable this use-defined operator in the BNF
  80. /*
  81. pop.EvaluateFunction += (function_name, function_args) =>
  82. {
  83. if (function_name == "Move")
  84. {
  85. double X = Convert.ToDouble(function_args.Parameters[0].Evaluate());
  86. double Y = Convert.ToDouble(function_args.Parameters[1].Evaluate());
  87. if (X > Y)
  88. {
  89. function_args.Result = 1;
  90. }
  91. else
  92. {
  93. function_args.Result = 0;
  94. }
  95. }
  96. };
  97. */
  98. pop.CreateFitnessCase += (index) =>
  99. {
  100. SpiralFitnessCase fitness_case = new SpiralFitnessCase();
  101. fitness_case.X = double.Parse(table.Rows[index]["X"].ToString());
  102. fitness_case.Y = double.Parse(table.Rows[index]["Y"].ToString());
  103. fitness_case.Label = int.Parse(table.Rows[index]["Label"].ToString());
  104. return fitness_case;
  105. };
  106. pop.GetFitnessCaseCount += () =>
  107. {
  108. return table.Rows.Count;
  109. };
  110. pop.EvaluateObjectiveForSolution += (fitness_cases, s, objective_index) =>
  111. {
  112. double fitness = 0;
  113. for (int i = 0; i < fitness_cases.Count; i++)
  114. {
  115. SpiralFitnessCase fitness_case = (SpiralFitnessCase)fitness_cases[i];
  116. int correct_y = fitness_case.Label;
  117. int computed_y = fitness_case.ComputedLabel;
  118. fitness += (correct_y == computed_y) ? 0 : 1;
  119. }
  120. return fitness;
  121. };
  122. pop.BreedInitialPopulation();
  123. while (!pop.IsTerminated)
  124. {
  125. pop.Evolve();
  126. Console.WriteLine("Spiral Classification Generation: {0}", pop.CurrentGeneration);
  127. Console.WriteLine("Global Fitness: {0}\tCurrent Fitness: {1}", pop.GlobalBestProgram.Fitness, pop.FindFittestProgramInCurrentGeneration().Fitness);
  128. Console.WriteLine("Global Best Solution:\n{0}", pop.GlobalBestProgram);
  129. //Console.WriteLine("Current Best Solution:\n{0}", pop.FindFittestProgramInCurrentGeneration());
  130. }
  131. Console.WriteLine(pop.GlobalBestProgram.ToString());
  132. }
  133. }

The GEConfig.xml and its child configuration files will be automatically generated if they do not exist, otherwise the configuration will be loaded from the existing GEConfig.xml and its child configuration files.