Esempio n. 1
0
        async Task <AssessResponse> PostFitnessAssess(AssessRequest areq)
        {
            // ... UDOO
            await Task.Delay(0);

            var ares = new AssessResponse();

            return(ares);
        }
        async Task <AssessResponse> PostFitnessAssess(AssessRequest a)
        {
            await Task.Delay(0);

            var client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:8091/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            var hrm = await client.PostAsJsonAsync("/assess", a);

            hrm.EnsureSuccessStatusCode();
            var ares = await hrm.Content.ReadAsAsync <AssessResponse>();

            return(ares);
        }
        string target = "";  // simplified Fitness stuff, shouldn't be here

        async Task <AssessResponse> PostFitnessAssess(AssessRequest areq)
        {
            // fake Fitness post request&response
            // replace this code by actual POST /assess request&response
            // var client = new HttpClient ();
            // ...

            await Task.Delay(0);

            var scores = areq.genomes.Select(g => {
                var len = Math.Min(target.Length, g.Length);
                var h   = Enumerable.Range(0, len)
                          .Sum(i => Convert.ToInt32(target[i] != g[i]));
                h = h + Math.Max(target.Length, g.Length) - len;
                return(h);
            }).ToList();

            return(new AssessResponse {
                id = areq.id, scores = scores
            });
        }
        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;
                    }
                }
            }
        }