Beispiel #1
0
        private void initInfections()
        {
            Random rand = new Random();

            Infection inf1 = new Infection()
            {
                Size           = 5,
                StoreSize      = 2,
                Aggression     = 12,
                SpreadSpeed    = 3,
                SpreadDistance = 1,
                SpreadArea     = 1,
                StrengthPref   = 0.75f
            };
            InfectionSpeciman s1 = new InfectionSpeciman(inf1);

            this.infections.Add(inf1, Color.Red);
            Victim v = field.Data[rand.Next(0, SIZE_X), rand.Next(0, SIZE_Y)];

            //v.Health = Field.MAX_HEALTH;
            v.Infect(s1);

            Infection inf2 = new Infection()
            {
                Size           = 1,
                StoreSize      = 4,
                Aggression     = 9,
                SpreadSpeed    = 5,
                SpreadDistance = 2,
                SpreadArea     = 2,
                StrengthPref   = 0.75f
            };

            this.infections.Add(inf2, Color.Green);
            InfectionSpeciman s2 = new InfectionSpeciman(inf2);

            field.Data[rand.Next(0, SIZE_X), rand.Next(0, SIZE_Y)].Infect(s2);

            field.FieldProgressEvent += field_FieldProgressEvent;

            Infection inf3 = new Infection()
            {
                Size           = 1,
                StoreSize      = 2,
                Aggression     = 5,
                SpreadSpeed    = 5,
                SpreadDistance = 1,
                SpreadArea     = 1,
                StrengthPref   = 0.75f
            };

            this.infections.Add(inf3, Color.DarkOrange);
            InfectionSpeciman s3 = new InfectionSpeciman(inf3);

            field.Data[rand.Next(0, SIZE_X), rand.Next(0, SIZE_Y)].Infect(s3);
        }
Beispiel #2
0
        static Dictionary <Infection, Result> startRound(Field fieldO, Population infections, Dictionary <Infection, Result> top)
        {
            AutoResetEvent allEnded = new AutoResetEvent(false);
            int            counter  = 0;

            foreach (Infection inf in infections)
            {
                ThreadPool.QueueUserWorkItem((state) =>
                {
                    Field field = (Field)fieldO.Clone();
                    AutoResetEvent infectionLifeEnded = new AutoResetEvent(false);

                    Field.State oldState      = field.FieldState;
                    field.FieldProgressEvent += () =>
                    {
                        lock (field)
                        {
                            Field.State newState = field.FieldState;
                            if (newState == Field.State.Stopped && oldState != Field.State.Stopped)
                            {
                                infectionLifeEnded.Set();
                            }
                            oldState = field.FieldState;
                        }
                    };

                    // field.Reset();
                    DateTime startTime = DateTime.Now;

                    // Console.WriteLine("{0} : Next infection: {1}: \n{2}", startTime.ToString(), inf.Id.ToString(), inf.ToString());

                    foreach (Point s in START)
                    {
                        InfectionSpeciman infs = new InfectionSpeciman(inf);
                        //infs.DeadEvent += () =>
                        //{
                        //  DateTime endTime = DateTime.Now;
                        //  Console.WriteLine("{0} : {1} : Dead : Lifespan: {2}", endTime.ToString(), infs.Id.ToString(), endTime - startTime);
                        //};
                        //Console.WriteLine("{0} : {1} : Born", startTime.ToString(), infs.Id.ToString());
                        field.Data[s.X, s.Y].Infect(infs);
                    }

                    field.Start();

                    infectionLifeEnded.WaitOne();

                    Result singleResult = new Result(field.Step, DateTime.Now - startTime, field.InfectedCount, field.DeadCount);
                    // Console.WriteLine("{0} : {1}: End : Lifespan: {2}", endTime2.ToString(), inf.Id.ToString(), span);

                    lock (top)
                    {
                        if (top.Count((kvp) => kvp.Key.Equals(inf)) == 0) // Top should contain unique elements
                        {
                            if (top.Count >= TOP_SIZE)
                            {
                                // Replacing worst top entry

                                IEnumerable <KeyValuePair <Infection, Result> > foundInTop = top.Where(((kvp) => kvp.Value.CompareTo(singleResult) <= 0)).OrderBy((kvp) => kvp.Value);
                                int count = foundInTop.Count();
                                if (count > 0)
                                {
                                    KeyValuePair <Infection, Result> found = foundInTop.ElementAt(0);
                                    top.Remove(found.Key);
                                    top[inf] = singleResult;
                                }
                            }
                            else
                            {
                                // Top is not filled yet - just adding

                                top[inf] = singleResult;
                            }
                        }

                        counter++;
                        if (counter == infections.Count)
                        {
                            allEnded.Set();
                        }
                    }
                });
            }

            allEnded.WaitOne();

            Console.Clear();
            Console.WriteLine("Top {0}:", DateTime.Now.ToString());
            foreach (KeyValuePair <Infection, Result> kvp in top)
            {
                Console.WriteLine(kvp.Key);
                Console.WriteLine("\t{0}", kvp.Value.ToString());
            }

            return(top);
        }