Пример #1
0
        public InfectionSpeciman Clone()
        {
            InfectionSpeciman copy = new InfectionSpeciman(this.type);

            copy.DeadEvent += this.DeadEvent;

            return(copy);
        }
Пример #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;
        }
Пример #3
0
 public void Infect(InfectionSpeciman inf)
 {
     Logger.Instance.Add("victim", this.GetHashCode().ToString(), String.Format("Infected with {0}", inf.GetHashCode()));
       this.infections.Add(inf);
 }
Пример #4
0
 public void Cure(InfectionSpeciman inf)
 {
     Logger.Instance.Add("victim", this.GetHashCode().ToString(), String.Format("Cured from {0}", inf.GetHashCode()));
       this.infections.Remove(inf);
 }
Пример #5
0
 public void Cure(InfectionSpeciman inf)
 {
     Logger.Instance.Add("victim", this.GetHashCode().ToString(), String.Format("Cured from {0}", inf.GetHashCode()));
     this.infections.Remove(inf);
 }
Пример #6
0
 public void Infect(InfectionSpeciman inf)
 {
     Logger.Instance.Add("victim", this.GetHashCode().ToString(), String.Format("Infected with {0}", inf.GetHashCode()));
     this.infections.Add(inf);
 }
Пример #7
0
        public InfectionSpeciman Clone()
        {
            InfectionSpeciman copy = new InfectionSpeciman(this.type);
              copy.DeadEvent += this.DeadEvent;

              return copy;
        }
Пример #8
0
        public void Start()
        {
            if (this.state != State.Stopped)
            {
                return;
            }

            this.state = State.Started;

            Thread t = new Thread(() =>
            {
                Logger.Instance.Add("global", "", "----Start----");
                bool activityPresent = true;
                while (activityPresent)
                {
                    Logger.Instance.Add("global", "", "-----");

                    activityPresent = false;

                    for (int i = 0; i < sizeX; i++)
                    {
                        for (int j = 0; j < sizeY; j++)
                        {
                            Victim v = this.data[i, j];
                            if (v.IsInfected && !v.IsDead)
                            {
                                foreach (InfectionSpeciman inf in v.Infections)
                                {
                                    if (inf.IsDead)
                                    {
                                        continue;
                                    }
                                    activityPresent = true;

                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), String.Format("On victim cell: {0}x{1}", i, j));

                                    int eaten    = Math.Min(v.Health, inf.Type.Aggression);
                                    v.Health    -= eaten;
                                    inf.Balance += eaten;
                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Eaten: " + eaten);

                                    inf.Balance -= Consumption.ofSize(inf.Type);  // self-feeding
                                    inf.Balance -= Consumption.ofStore(inf.Type); // each store slot consumes one point (doesn't store, just consume)
                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Spent on self: " + Consumption.ofSize(inf.Type));
                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Spent on store: " + Consumption.ofStore(inf.Type));

                                    inf.SpreadCounter++;
                                    if (!inf.IsDead && inf.IsSpreadTime)
                                    {
                                        List <KeyValuePair <Victim, double> > nb =
                                            this.getNeighbours(i, j, inf.Type.SpreadDistance)
                                            .Where((v1) => !v1.Key.IsInfected && !v1.Key.IsDead && v1.Key.Health <= inf.Type.Size * Consumption.POWER)
                                            .ShuffleNeighbours((kvp) => kvp.Key.Health, inf.Type.StrengthPref)
                                            .ToList();
                                        for (int k = 0; k < inf.Type.SpreadArea; k++)
                                        {
                                            if (k < nb.Count)
                                            {
                                                inf.Balance -= Consumption.ofSpread(inf.Type, nb[k].Value);
                                                Logger.Instance.Add("infection", inf.GetHashCode().ToString(), "Spent on spread: " + Consumption.ofSpread(inf.Type, nb[k].Value));
                                                if (!inf.IsDead)
                                                {
                                                    InfectionSpeciman newSpec = inf.Clone();
                                                    nb[k].Key.Infect(newSpec);
                                                    Logger.Instance.Add("infection", inf.GetHashCode().ToString(), String.Format("Spread to victim with health: {0}", nb[k].Key.Health));
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    if (!inf.IsDead && inf.Balance > 0) // storing left
                                    {
                                        inf.Balance = Math.Min(inf.Balance, inf.Type.StoreSize);
                                    }
                                }

                                if (!v.IsDead)
                                {
                                    List <InfectionSpeciman> dead = v.Infections.Where((inf) => inf.IsDead).ToList();
                                    foreach (var inf in dead)
                                    {
                                        v.Cure(inf);
                                    }
                                }
                            }
                        }
                    }

                    if (this.FieldProgressEvent != null)
                    {
                        this.FieldProgressEvent();
                    }

                    if (this.stopEvent.WaitOne(Field.STEP_TIME))
                    {
                        break;
                    }
                    this.step++;
                }

                Logger.Instance.Add("global", "", "-----End-----");
                this.state = State.Stopped;

                if (this.FieldProgressEvent != null)
                {
                    this.FieldProgressEvent();
                }
            });

            t.Start();
        }
Пример #9
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);
        }