{"id":23080602,"url":"https://github.com/chen0040/cs-grammatical-evolution","last_synced_at":"2025-10-04T20:32:12.350Z","repository":{"id":85883548,"uuid":"108927218","full_name":"chen0040/cs-grammatical-evolution","owner":"chen0040","description":"Grammatical evolution implemented using C#","archived":false,"fork":false,"pushed_at":"2017-12-03T01:44:56.000Z","size":656,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-27T19:28:44.244Z","etag":null,"topics":["grammatical-evolution","symbolic-regression"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chen0040.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-10-31T01:11:12.000Z","updated_at":"2022-08-19T22:42:33.000Z","dependencies_parsed_at":"2023-03-13T07:18:26.751Z","dependency_job_id":null,"html_url":"https://github.com/chen0040/cs-grammatical-evolution","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chen0040/cs-grammatical-evolution","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fcs-grammatical-evolution","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fcs-grammatical-evolution/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fcs-grammatical-evolution/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fcs-grammatical-evolution/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chen0040","download_url":"https://codeload.github.com/chen0040/cs-grammatical-evolution/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chen0040%2Fcs-grammatical-evolution/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278078563,"owners_count":25926122,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-02T02:00:08.890Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grammatical-evolution","symbolic-regression"],"created_at":"2024-12-16T13:15:27.246Z","updated_at":"2025-10-04T20:32:12.345Z","avatar_url":"https://github.com/chen0040.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cs-grammatical-evolution\n\nGrammatical evolution implemented using C#\n\n# Install\n\n```bash\nInstall-Package cs-grammatical-evolution -Version 1.0.4\n```\n\n# Usage\n\nThe sample code belows show how to use the Grammatical Evolution to solve the spiral classification problem:\n\n```cs \nclass Program\n{\n\tstatic DataTable LoadData(string filename)\n\t{\n\t\tDataTable table = new DataTable();\n\t\ttable.Columns.Add(\"X\");\n\t\ttable.Columns.Add(\"Y\");\n\t\ttable.Columns.Add(\"Label\");\n\n\t\tint line_count = 0;\n\t\tusing (StreamReader reader = new StreamReader(filename))\n\t\t{\n\t\t\tstring line = reader.ReadLine();\n\t\t\tint.TryParse(line, out line_count);\n\n\t\t\twhile ((line = reader.ReadLine()) != null)\n\t\t\t{\n\t\t\t\tstring[] elements = line.Split(new char[] { '\\t' });\n\n\t\t\t\tdouble x, y;\n\t\t\t\tint label;\n\t\t\t\tdouble.TryParse(elements[0].Trim(), out x);\n\t\t\t\tdouble.TryParse(elements[1].Trim(), out y);\n\t\t\t\tint.TryParse(elements[2].Trim(), out label);\n\n\t\t\t\ttable.Rows.Add(x, y, label);\n\t\t\t}\n\t\t}\n\t\treturn table;\n\t}\n\n\tstatic void CreateGrammar(GEPop\u003cGESolution\u003e pop)\n\t{\n\t\tpop.GERuleSet.RemoveAllRules();\n\n\t\t//pop.GERuleSet.AddRule(\"\u003cexpr\u003e\", \"\u003clbracket\u003e\", \"\u003cexpr\u003e\", \"\u003cbiop\u003e\", \"\u003cexpr\u003e\", \"\u003crbracket\u003e\");\n\t\tpop.GERuleSet.AddRule(\"\u003cexpr\u003e\", \"\u003clbracket\u003e\", \"\u003cexpr\u003e\", \"\u003cbiop\u003e\", \"\u003cexpr\u003e\", \"\u003crbracket\u003e\");\n\t\tpop.GERuleSet.AddRule(\"\u003cexpr\u003e\", \"\u003cpre-op\u003e\", \"\u003clbracket\u003e\", \"\u003cexpr\u003e\", \"\u003crbracket\u003e\");\n\t\t//pop.GERuleSet.AddRule(\"\u003cexpr\u003e\", \"\u003cpre-op-2\u003e\", \"\u003clbracket\u003e\", \"\u003cexpr\u003e\", \"\u003ccomma\u003e\", \"\u003cexpr\u003e\", \"\u003crbracket\u003e\");\n\t\tpop.GERuleSet.AddRule(\"\u003cexpr\u003e\", \"\u003cvar\u003e\");\n\n\t\tpop.GERuleSet.AddRule(\"\u003cbiop\u003e\", \"+\");\n\t\tpop.GERuleSet.AddRule(\"\u003cbiop\u003e\", \"-\");\n\t\tpop.GERuleSet.AddRule(\"\u003cbiop\u003e\", \"/\");\n\t\tpop.GERuleSet.AddRule(\"\u003cbiop\u003e\", \"*\");\n\n\t\t//pop.GERuleSet.AddRule(\"\u003ccomma\u003e\", \",\");\n\t\t//pop.GERuleSet.AddRule(\"\u003cpre-op-2\u003e\", \"Pow\");\n\t\tpop.GERuleSet.AddRule(\"\u003cpre-op\u003e\", \"Sin\");\n\t\t//pop.GERuleSet.AddRule(\"\u003cpre-op\u003e\", \"Tan\");\n\t\tpop.GERuleSet.AddRule(\"\u003cpre-op\u003e\", \"Cos\");\n\t\tpop.GERuleSet.AddRule(\"\u003clbracket\u003e\", \"(\");\n\t\tpop.GERuleSet.AddRule(\"\u003crbracket\u003e\", \")\");\n\n\t\tpop.GERuleSet.AddRule(\"\u003cvar\u003e\", \"X\");\n\t\tpop.GERuleSet.AddRule(\"\u003cvar\u003e\", \"Y\");\n\t\tpop.GERuleSet.AddRule(\"\u003cvar\u003e\", \"C1\");\n\t}\n\n\tstatic void CreateTerminalSet(GEPop\u003cGESolution\u003e pop)\n\t{\n\t\tpop.OperatorSet.AddOperator(\"+\");\n\t\tpop.OperatorSet.AddOperator(\"-\");\n\t\tpop.OperatorSet.AddOperator(\"/\");\n\t\tpop.OperatorSet.AddOperator(\"*\");\n\t\tpop.OperatorSet.AddOperator(\"Sin\");\n\t\tpop.OperatorSet.AddOperator(\"Cos\");\n\t\tpop.OperatorSet.AddOperator(\"(\");\n\t\tpop.OperatorSet.AddOperator(\")\");\n\t\t//pop.OperatorSet.AddOperator(\"Tan\");\n\t\t//pop.OperatorSet.AddOperator(\"Pow\");\n\n\t\tpop.ConstantSet.AddConstant(\"C1\", 8);\n\n\t\tpop.VariableSet.AddVariable(\"X\");\n\t\tpop.VariableSet.AddVariable(\"Y\");\n\n\t\tpop.BuildTerminalSet();\n\t}\n\n\tstatic void Main(string[] args)\n\t{\n\t\tDataTable table = LoadData(\"dataset.txt\");\n\n\t\tGEConfig config = new GEConfig(\"GEConfig.xml\");\n\n\t\tGEPop\u003cGESolution\u003e pop = new GEPop\u003cGESolution\u003e(config);\n\n\t\tCreateTerminalSet(pop);\n\n\t\t//User can load BNF grammar from file or create programmatic ally\n\t\tpop.GERuleSet.LoadGrammar(\"grammar.bnf\");\n\n\t\tConsole.WriteLine(pop.GERuleSet.ToString());\n\n\t\t//CreateGrammar(pop);\n\n\t\t//Xianshun: Say if you implement a operator named \"Move(X, Y)\",\n\t\t//Which returns a value 1 if X \u003e Y otherwise 0\n\t\t//use the following code to enable this use-defined operator in the BNF\n\t\t/*\n\t\tpop.EvaluateFunction += (function_name, function_args) =\u003e\n\t\t\t{\n\t\t\t\tif (function_name == \"Move\")\n\t\t\t\t{\n\t\t\t\t\tdouble X = Convert.ToDouble(function_args.Parameters[0].Evaluate());\n\t\t\t\t\tdouble Y = Convert.ToDouble(function_args.Parameters[1].Evaluate());\n\t\t\t\t\tif (X \u003e Y)\n\t\t\t\t\t{\n\t\t\t\t\t\tfunction_args.Result = 1;\n\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\t\tfunction_args.Result = 0;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t};\n\t\t */\n\n\t\tpop.CreateFitnessCase += (index) =\u003e\n\t\t{\n\t\t\tSpiralFitnessCase fitness_case = new SpiralFitnessCase();\n\t\t\tfitness_case.X = double.Parse(table.Rows[index][\"X\"].ToString());\n\t\t\tfitness_case.Y = double.Parse(table.Rows[index][\"Y\"].ToString());\n\t\t\tfitness_case.Label = int.Parse(table.Rows[index][\"Label\"].ToString());\n\n\t\t\treturn fitness_case;\n\t\t};\n\n\t\tpop.GetFitnessCaseCount += () =\u003e\n\t\t{\n\t\t\treturn table.Rows.Count;\n\t\t};\n\n\t\tpop.EvaluateObjectiveForSolution += (fitness_cases, s, objective_index) =\u003e\n\t\t{\n\t\t\tdouble fitness = 0;\n\t\t\tfor (int i = 0; i \u003c fitness_cases.Count; i++)\n\t\t\t{\n\t\t\t\tSpiralFitnessCase fitness_case = (SpiralFitnessCase)fitness_cases[i];\n\t\t\t\tint correct_y = fitness_case.Label;\n\t\t\t\tint computed_y = fitness_case.ComputedLabel;\n\t\t\t\tfitness += (correct_y == computed_y) ? 0 : 1;\n\t\t\t}\n\n\t\t\treturn fitness;\n\t\t};\n\n\n\t\tpop.BreedInitialPopulation();\n\n\n\t\twhile (!pop.IsTerminated)\n\t\t{\n\t\t\tpop.Evolve();\n\t\t\tConsole.WriteLine(\"Spiral Classification Generation: {0}\", pop.CurrentGeneration);\n\t\t\tConsole.WriteLine(\"Global Fitness: {0}\\tCurrent Fitness: {1}\", pop.GlobalBestProgram.Fitness, pop.FindFittestProgramInCurrentGeneration().Fitness);\n\t\t\tConsole.WriteLine(\"Global Best Solution:\\n{0}\", pop.GlobalBestProgram);\n\t\t\t//Console.WriteLine(\"Current Best Solution:\\n{0}\", pop.FindFittestProgramInCurrentGeneration());\n\t\t}\n\n\t\tConsole.WriteLine(pop.GlobalBestProgram.ToString());\n\t}\n}\n```\n\nThe 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen0040%2Fcs-grammatical-evolution","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchen0040%2Fcs-grammatical-evolution","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchen0040%2Fcs-grammatical-evolution/lists"}