Exemplo n.º 1
0
        public void RunGoRoutineWithThreeParameters()
        {
            int i = 0;

            GoRoutines.Go((x, y, z) => i = x + y + z, 2, 3, 4);

            Thread.Sleep(100);

            Assert.AreEqual(9, i);
        }
Exemplo n.º 2
0
        public void RunGoRoutineWithTwoParameters()
        {
            int            i      = 0;
            AutoResetEvent handle = new AutoResetEvent(false);

            GoRoutines.Go((x, y) => { i = x + y; handle.Set(); }, 2, 3);

            handle.WaitOne();

            Assert.AreEqual(5, i);
        }
Exemplo n.º 3
0
        public void RunGoRoutineWithParameter()
        {
            int            i      = 0;
            AutoResetEvent handle = new AutoResetEvent(false);

            GoRoutines.Go(x => { i = x; handle.Set(); }, 2);

            handle.WaitOne();

            Assert.AreEqual(2, i);
        }
Exemplo n.º 4
0
        public void RunGoRoutine()
        {
            int            i      = 0;
            AutoResetEvent handle = new AutoResetEvent(false);

            GoRoutines.Go(delegate() { i++; handle.Set(); });

            handle.WaitOne();

            Assert.AreEqual(1, i);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Channel numbers = new Channel();

            GoRoutines.Go(() => { for (int k = 2; ; k++)
                                  {
                                      numbers.Send(k);
                                  }
                          });

            Channel channel = numbers;

            int prime = 0;

            while (prime < 1000)
            {
                prime = (int)channel.Receive();

                Console.WriteLine(prime);

                Channel newchannel = new Channel();

                GoRoutines.Go((input, output, p) =>
                {
                    while (true)
                    {
                        int number = (int)input.Receive();

                        if ((number % p) != 0)
                        {
                            output.Send(number);
                        }
                    }
                }, channel, newchannel, prime);

                channel = newchannel;
            }
        }
Exemplo n.º 6
0
        private void button1_Click(object sender, EventArgs e)
        {
            Channel genomas   = new Channel();
            Channel evaluated = new Channel();
            Channel bestsofar = new Channel();
            Channel mutator   = new Channel();

            // Generates the initial populations
            GoRoutines.Go(() =>
            {
                Genoma genoma = GenerateGenoma();

                for (int j = 0; j < PopulationSize * 10; j++)
                {
                    genomas.Send(this.Mutate(genoma));
                }
            });

            // Evaluate genomas
            GoRoutines.Go(() =>
            {
                while (true)
                {
                    Genoma genoma = (Genoma)genomas.Receive();
                    genoma.value  = this.CalculateValue(genoma);
                    evaluated.Send(genoma);
                }
            });

            // Collect and select
            GoRoutines.Go(() =>
            {
                GenomaComparer comparer = new GenomaComparer();

                while (true)
                {
                    List <Genoma> genomalist = new List <Genoma>();

                    for (int k = 0; k < PopulationSize; k++)
                    {
                        Genoma genoma = (Genoma)evaluated.Receive();
                        genomalist.Add(genoma);
                    }

                    genomalist.Sort(comparer);

                    GoRoutines.Go(() => bestsofar.Send(genomalist[0]));

                    for (int k = 0; k < PopulationSize / 5; k++)
                    {
                        GoRoutines.Go(() => evaluated.Send(genomalist[k]));
                    }

                    for (int k = 0; k < PopulationSize / 5; k++)
                    {
                        GoRoutines.Go(() => mutator.Send(genomalist[k]));
                        GoRoutines.Go(() => mutator.Send(genomalist[k]));
                        GoRoutines.Go(() => mutator.Send(genomalist[k]));
                        GoRoutines.Go(() => mutator.Send(genomalist[k]));
                    }
                }
            });

            // Mutates
            GoRoutines.Go(() =>
            {
                Random rnd = new Random();
                while (true)
                {
                    Genoma genoma    = (Genoma)mutator.Receive();
                    Genoma newgenoma = this.Mutate(genoma);

                    //if (rnd.Next(2) == 0)
                    //    newgenoma = this.Mutate(newgenoma);

                    while (newgenoma.value >= genoma.value)
                    {
                        if (rnd.Next(3) == 0)
                        {
                            break;
                        }

                        newgenoma = this.Mutate(genoma);
                    }

                    evaluated.Send(newgenoma);
                }
            });

            // Receives and draws the results
            GoRoutines.Go(() =>
            {
                Genoma best = null;

                while (true)
                {
                    Genoma genoma = (Genoma)bestsofar.Receive();

                    if (best == null || best.value > genoma.value)
                    {
                        best = genoma;
                        this.BestGenoma(genoma);
                    }
                }
            });
        }