コード例 #1
0
        async Task <AssessResponse> PostFitnessAssess(AssessRequest areq)
        {
            // ... UDOO
            await Task.Delay(0);

            var ares = new AssessResponse();

            return(ares);
        }
コード例 #2
0
        async void GeneticAlgorithm(TryRequest treq)
        {
            WriteLine($"..... GeneticAlgorithm {treq}");
            await Task.Delay(0);

            var id      = treq.id;
            var monkeys = treq.monkeys;

            if (monkeys % 2 != 0)
            {
                monkeys += 1;
            }
            var length = 0;

            if (treq.length == 0)
            {
                var nullText = new[] { "" }.ToList();
                var r = new AssessRequest {
                    id = id, genomes = nullText
                };
                AssessResponse ares = await PostFitnessAssess(r);

                var scores = ares.scores;
                length = scores[0];
            }
            else
            {
                length = treq.length;
            }
            var crossover = treq.crossover / 100.0;
            var mutation  = treq.mutation / 100.0;
            var limit     = treq.limit;

            if (limit == 0)
            {
                limit = 1000;
            }
            var topscore = int.MaxValue;
            var genomes  = Enumerable.Range(0, monkeys).Select(i => {
                var genome = Enumerable.Range(0, length).Select(n => {
                    return((char)NextInt(32, 127));
                }).ToList();
                return(string.Join("", genome));
            }).ToList();

            var obj1 = new AssessRequest {
                id = id, genomes = genomes
            };

            for (int loop = 0; loop < limit; loop++)
            {
                AssessResponse x = await PostFitnessAssess(obj1);

                var Id          = x.id;
                var scores      = x.scores;
                var smallest    = scores.Min();
                var largest     = scores.Max();
                var index       = scores.IndexOf(smallest);
                var genome_list = obj1.genomes;
                var genome      = genome_list[index];
                if (smallest < topscore)
                {
                    var obj2 = new TopRequest {
                        id = Id, loop = loop, score = smallest, genome = genome
                    };
                    await PostClientTop(obj2);

                    topscore = smallest;
                }

                if (smallest == 0)
                {
                    break;
                }
                else
                {
                    var weights = scores.Select(s => {
                        return(largest - s + 1);
                    });
                    var para = treq.parallel;

                    if (para)
                    {
                        var newGenomes = ParallelEnumerable.Range(1, monkeys / 2).SelectMany <int, string>(i => {
                            var c1     = "";
                            var c2     = "";
                            var index1 = ProportionalRandom(weights.ToArray(), weights.Sum());
                            var index2 = ProportionalRandom(weights.ToArray(), weights.Sum());
                            var p1     = genome_list[index1];
                            var p2     = genome_list[index2];

                            if (NextDouble() < crossover)
                            {
                                var crossoverIndex = NextInt(0, length);
                                c1 = p1.Substring(0, crossoverIndex) + p2.Substring(crossoverIndex, p2.Length - crossoverIndex);
                                c2 = p2.Substring(0, crossoverIndex) + p1.Substring(crossoverIndex, p1.Length - crossoverIndex);
                            }
                            else
                            {
                                c1 = p1;
                                c2 = p2;
                            }

                            if (NextDouble() < mutation)
                            {
                                char[] c = c1.ToCharArray();
                                c[NextInt(0, c1.Length)] = (char)NextInt(32, 127);
                                c1 = new string(c);
                            }
                            if (NextDouble() < mutation)
                            {
                                char[] c = c2.ToCharArray();
                                c[NextInt(0, c2.Length)] = (char)NextInt(32, 127);
                                c2 = new string(c);
                            }
                            var mylist = new List <string>();
                            mylist.Add(c1);
                            mylist.Add(c2);
                            return(mylist);
                        }).ToList();
                        obj1.id      = Id;
                        obj1.genomes = newGenomes;
                    }
                    else
                    {
                        var newGenomes = Enumerable.Range(1, monkeys / 2).SelectMany <int, string>(i => {
                            var c1     = "";
                            var c2     = "";
                            var index1 = ProportionalRandom(weights.ToArray(), weights.Sum());
                            var index2 = ProportionalRandom(weights.ToArray(), weights.Sum());
                            var p1     = genome_list[index1];
                            var p2     = genome_list[index2];

                            if (NextDouble() < crossover)
                            {
                                var crossoverIndex = NextInt(0, length);
                                c1 = p1.Substring(0, crossoverIndex) + p2.Substring(crossoverIndex, p2.Length - crossoverIndex);
                                c2 = p2.Substring(0, crossoverIndex) + p1.Substring(crossoverIndex, p1.Length - crossoverIndex);
                            }
                            else
                            {
                                c1 = p1;
                                c2 = p2;
                            }

                            if (NextDouble() < mutation)
                            {
                                char[] c = c1.ToCharArray();
                                c[NextInt(0, c1.Length)] = (char)NextInt(32, 127);
                                c1 = new string(c);
                            }
                            if (NextDouble() < mutation)
                            {
                                char[] c = c2.ToCharArray();
                                c[NextInt(0, c2.Length)] = (char)NextInt(32, 127);
                                c2 = new string(c);
                            }

                            var mylist = new List <string>();
                            mylist.Add(c1);
                            mylist.Add(c2);
                            return(mylist);
                        }).ToList();

                        obj1.id      = Id;
                        obj1.genomes = newGenomes;
                    }
                }
            }
        }