Ejemplo n.º 1
0
        protected unsafe override void HandleHuman(Human* human)
        {
            var spreading = human->IsSpreading();
            var infecting = human->IsInfected();
            var diseased = human->IsDiseased();

            human->DoDiseaseTick((short)_data.DiseaseToSimulate.SpreadingTime);

            if (human->IsSpreading() != spreading)
            {
                if (spreading)
                    lock (_data.Cells[human->CurrentCell]) _data.Cells[human->CurrentCell].Spreading--;
                else
                    lock (_data.Cells[human->CurrentCell]) _data.Cells[human->CurrentCell].Spreading++;
            }

            if (human->IsInfected() != infecting)
            {
                if (infecting)
                    lock (_data.Cells[human->CurrentCell]) _data.Cells[human->CurrentCell].Infecting--;
                else
                    lock (_data.Cells[human->CurrentCell]) _data.Cells[human->CurrentCell].Infecting++;
            }

            if (human->IsDiseased() != diseased)
            {
                if (diseased)
                    lock (_data.Cells[human->CurrentCell]) _data.Cells[human->CurrentCell].Diseased--;
                else
                    lock (_data.Cells[human->CurrentCell]) _data.Cells[human->CurrentCell].Diseased++;
            }
        }
Ejemplo n.º 2
0
        protected unsafe override void HandleHuman(Human* human)
        {
            if (!human->IsInfected())
                return;

            var rand = RANDOM.Next(100);
            int ageGroup = (int)human->GetAge() / 32;

            if (human->GetGender() == EGender.Female)
                ageGroup += 4;

            if (rand <= _data.DiseaseToSimulate.HealingFactor[ageGroup])
            {
                lock (_data.Cells[human->CurrentCell])
                {
                    if (human->IsSpreading())
                        --_data.Cells[human->CurrentCell].Spreading;
                    if(human->IsDiseased())
                        --_data.Cells[human->CurrentCell].Diseased;

                    --_data.Cells[human->CurrentCell].Infecting;
                }
                human->HealHuman();
            }
        }
Ejemplo n.º 3
0
        public void MoveHuman(Human* ptr, int destination)
        {
            lock(_data.Cells)
                {
                    PopulationCell currentcell = _data.Cells[ptr->CurrentCell];
                    PopulationCell destinationcell = _data.Cells[destination];

                    if (ptr->IsInfected())
                    {
                        --currentcell.Infecting;
                        ++destinationcell.Infecting;
                    }

                    if (ptr->IsSpreading())
                    {
                        --currentcell.Spreading;
                        ++destinationcell.Spreading;
                    }

                    if (ptr->IsDiseased())
                    {
                        --currentcell.Diseased;
                        ++destinationcell.Diseased;
                    }

                    if (ptr->GetGender() == EGender.Male)
                    {
                        switch (ptr->GetAge())
                        {
                            case EAge.Baby: --currentcell.MaleBabies;
                                ++destinationcell.MaleBabies; break;
                            case EAge.Child: --currentcell.MaleChildren;
                                ++destinationcell.MaleChildren; break;
                            case EAge.Adult: --currentcell.MaleAdults;
                                ++destinationcell.MaleAdults; break;
                            case EAge.Senior: --currentcell.MaleSeniors;
                                ++destinationcell.MaleSeniors; break;
                        }
                    }
                    else
                    {
                        switch (ptr->GetAge())
                        {
                            case EAge.Baby: --currentcell.FemaleBabies;
                                ++destinationcell.FemaleBabies; break;
                            case EAge.Child: --currentcell.FemaleChildren;
                                ++destinationcell.FemaleChildren; break;
                            case EAge.Adult: --currentcell.FemaleAdults;
                                ++destinationcell.FemaleAdults; break;
                            case EAge.Senior: --currentcell.FemaleSeniors;
                                ++destinationcell.FemaleSeniors; break;
                        }
                    }
                }

            ptr->CurrentCell = destination;
        }
Ejemplo n.º 4
0
        public SimulationData(DateTime startTime)
            : base("SD")
        {
            Cells = new PopulationCell[0];          //  10808574
            Humans = new Human[0];                  // ~82000000
            Deaths = new HumanSnapshot[0];
            DeathCount = 0;

            _time = startTime;
        }
Ejemplo n.º 5
0
        protected unsafe override void HandleHuman(Human* human)
        {
            if(!human->IsDiseased())
                return;

            //TODO: |f| pretty basic stuff right here might wanna spice this up a little
            var rand = RANDOM.Next(100);
            int ageGroup = (int)human->GetAge() / 32;

            if (human->GetGender() == EGender.Female)
                ageGroup += 4;

            if (rand <= _data.DiseaseToSimulate.MortalityRate[ageGroup])
            {
                human->KillHuman();
                lock (_data.Cells[human->CurrentCell])
                {
                    if (human->IsSpreading())
                        --_data.Cells[human->CurrentCell].Spreading;
                    --_data.Cells[human->CurrentCell].Infecting;
                    --_data.Cells[human->CurrentCell].Diseased;

                    int agegroup = (int)human->GetAge() / 32 +
                        ((human->GetGender() == 0)? 4 : 0);
                    _data.Cells[human->CurrentCell].Data[agegroup]--;
                }

                lock (_data.Deaths)
                    _data.AddDeadPeople(new List<HumanSnapshot>()
                    {
                        HumanSnapshot.InitializeFromRuntime(
                        (byte)human->GetGender(),
                        (byte)human->GetAgeInYears(),
                        (byte)human->GetProfession(),
                        human->HomeCell,
                        human->CurrentCell,
                        true)
                    });
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Generates the ultimative matrix. It spreads
        /// the people ALLL over their departments.
        /// 
        /// **In-Place**
        /// </summary>
        /// <param name="populationArray">The already initialized
        /// PopulationCell-matrix which will be used for most stuff.
        /// This array will be modified!</param>
        /// <param name="humanArray">The array of Humans. This array will be modified!</param>
        /// <param name="rawData">The raw DepartmentInfos. This array will be modified!</param>
        /// <param name="width">width of the populationArray (used for offsets)</param>
        /// <param name="height">height of the populationArray (used for offsets)</param>
        public void GenerateMatrix(
            PopulationCell[] populationArray,
            Human[] humanArray,
            IEnumerable<DepartmentInfo> rawData,
            int width, int height)
        {
            WIDTH = width;
            HEIGHT = height;

            if (populationArray.Length != WIDTH * HEIGHT)
                throw new ArgumentException(
                    "The argument has to be an array of PopulationCell with a length of '" + WIDTH * HEIGHT + "'.",
                    "populationArray");

            Combiner comb = new Combiner(populationArray, humanArray);
            int departmentsCount = rawData.Count();

            // the parallel call.
            Parallel.ForEach(rawData, Constants.DEFAULT_PARALLELOPTIONS,
                (item) => {
                    Human[] humans;
                    var res = _dummy? DummyPopulate(item, out humans)
                                    : Populate(item, out humans);         // populate the department.
                    comb.Enqueue(res, humans);
                    res = null;
                    humans = null;

                    DepartmentCalculationFinished.Raise(this, new GeneratorEventArgs
                    {
                        Name = item.Name,
                        Total = departmentsCount
                    });
                    WriteMessage("Calculated: " + item.Name);
                });
            comb.Wait();
            comb.Dispose();
        }
Ejemplo n.º 7
0
        private void MovePlumber(Human* ptr)
        {
            //Working Mindset -> Plumber going to different households each hour
            //Assert : Plumber is at Home at 0 o'clock;
            switch (ptr->GetMindset())
            {
                case PopulationData.EMindset.Working:
                    if (_data.CurrentHour > 7 && _data.CurrentHour < 18)
                    {
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 5, 74));
                    }
                    //return home at 18 o'clock
                    else if (_data.CurrentHour == 18)
                    {
                        if (!ptr->IsAtHome())
                            LetHumanTravelHome(ptr);
                        else
                            return;
                    }
                    else
                        return;

                    break;

                case PopulationData.EMindset.DayOff:
                    if (_data.CurrentHour < 11)
                    {
                        //sleep
                        return;
                    }
                    else if (_data.CurrentHour < 20 && ptr->IsAtHome())
                    {
                        if (RANDOM.Next(20) == 19)
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 10, 50));
                    }
                    else if (_data.CurrentHour == 20 && !ptr->IsAtHome())
                        LetHumanTravelHome(ptr);
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 8
0
        private void MovePupil(Human* ptr)
        {
            switch (ptr->GetMindset())
            {
                //Working Mindset -> Pupil going to school this day
                //Assert : Pupil is at Home at 0 o'clock
                case PopulationData.EMindset.Working:

                    //Pupil is at Home in the Morning
                    if (_data.CurrentHour < 7)
                    {
                        return;
                    }
                    //Pupil should be in school from 7-10
                    else if (_data.CurrentHour == 7)
                    {
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 2, 125));
                    }
                    else if (_data.CurrentHour < 11)
                    {
                        return; //Pupil should stay in school
                    }
                    //pupil has chance that school ends
                    else if (_data.CurrentHour < 14)
                    {
                        if (!ptr->IsAtHome())
                        {
                            if (RANDOM.Next(3) == 2)
                                LetHumanTravelHome(ptr);
                            else
                                return;
                        }
                        else
                            return;
                    }
                    //School ends definately
                    else if (_data.CurrentHour == 14)
                    {
                        if (!ptr->IsAtHome())
                            LetHumanTravelHome(ptr);
                        else
                            return;
                    }
                    else if (_data.CurrentHour == 15)
                    {
                        if (RANDOM.Next(2) == 1)
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 2, 25));
                        else
                            return;
                    }
                    //Return pupil home if he isn't already
                    else if (_data.CurrentHour == 18)
                    {
                        if (!ptr->IsAtHome())
                            LetHumanTravelHome(ptr);
                        else
                            return;
                    }
                    //in the evening pupil should be at home
                    else
                        return;

                    break;

                //Dayoff Behaviour
                //Wander around aimlessly
                case PopulationData.EMindset.DayOff:
                    if (_data.CurrentHour < 7)
                        return;
                    else if (_data.CurrentHour < 18)
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 0, 25));
                    else if (_data.CurrentHour == 18 && !ptr->IsAtHome())
                        LetHumanTravelHome(ptr);
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 9
0
        private void MoveDeskJobber(Human* ptr)
        {
            switch (ptr->GetMindset())
            {
                //Working Mindset -> Deskjobber has 9 to 5 job
                //Assert : Deskjobber is at Home at 0 o'clock;
                case PopulationData.EMindset.Working:
                    //Go to work
                    if (_data.CurrentHour == 9)
                    {
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 0, 75));
                    }
                    //Go home
                    else if (_data.CurrentHour == 18)
                    {
                        LetHumanTravelHome(ptr);
                    }
                    //In the morning and evening stay at home
                    else
                        return;

                    break;

                case PopulationData.EMindset.DayOff:

                    if (_data.CurrentHour < 11)
                    {
                        //sleep
                        return;
                    }
                    else if (_data.CurrentHour < 20 && ptr->IsAtHome())
                    {
                        if (RANDOM.Next(20) == 19)
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 10, 50));
                    }
                    else if (_data.CurrentHour == 20 && !ptr->IsAtHome())
                    {
                        LetHumanTravelHome(ptr);
                    }
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 10
0
        private void MoveHousewife(Human* ptr)
        {
            switch (ptr->GetMindset())
            {
                //Working Mindset -> Housewife doesn't go to work is either at home in the city or by friends
                //Assert : Housewife is at Home at 0 o'clock;
                case PopulationData.EMindset.DayOff:
                case PopulationData.EMindset.Working:
                    //Run around aimlessly^^
                    if (_data.CurrentHour > 6 && _data.CurrentHour < 20)
                    {
                        //chance to go shopping/visit friends/work
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 0, 5));
                    }
                    //return home at 20 o'clock
                    else if (_data.CurrentHour == 20)
                    {
                        if (!ptr->IsAtHome())
                            LetHumanTravelHome(ptr);
                        else
                            return;
                    }
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// The Dummy-implementation.
        /// </summary>
        private Tuple<int, PopulationCell>[] DummyPopulate(DepartmentInfo depInfo, out Human[] humanArray)
        {
            int areaSize = depInfo.Coordinates.Length;

            Tuple<int, PopulationCell>[] tmpArray = new Tuple<int, PopulationCell>[areaSize];
            int tmpCounter = 0;
            humanArray = new Human[depInfo.GetTotal()];
            int humanCounter = 0;

            int[] popsPerPoint = depInfo.Population.Select(x => x / areaSize).ToArray();

            foreach (Point point in depInfo.Coordinates)
            {
                PopulationCell cell = new PopulationCell();

                for (int i = 0; i < 8; i++)
                {
                    EGender gender = (i < 4) ? EGender.Male : EGender.Female;

                    var bounds = GetAgeBounds(i);
                    int lowerAgeBound = bounds.Item1;
                    int upperAgeBound = bounds.Item2;

                    for (int n = 0; n < popsPerPoint[i]; n++)
                    {
                        int thisAge = RANDOM.Next(lowerAgeBound, upperAgeBound + 1);
                        humanArray[humanCounter++] = Human.Create(gender, thisAge, point.Flatten(WIDTH));
                        cell.Data[i]++;
                    }
                }

                tmpArray[tmpCounter++] = new Tuple<int, PopulationCell>(point.Flatten(WIDTH), cell);

            }

            return tmpArray;
        }
Ejemplo n.º 12
0
        private void MoveCommuter(Human* ptr)
        {
            switch (ptr->GetMindset())
            {
                //Working Mindset -> Commuter goes to work in a city that is 0,5-2 hours away from home
                //Assert : Commuter is at Home at 0 o'clock;
                case PopulationData.EMindset.Working:
                    //travel to distant workplace
                    if (_data.CurrentHour == 6)
                    {
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 125, 400));
                    }
                    //return home
                    else if (_data.CurrentHour == 17)
                    {
                        LetHumanTravelHome(ptr);
                    }
                    //Stay home in the morning and evening
                    else
                        return;

                    break;

                case PopulationData.EMindset.DayOff:
                    if (_data.CurrentHour < 11)
                    {
                        //sleep
                        return;
                    }
                    else if (_data.CurrentHour < 20 && ptr->IsAtHome())
                    {
                        if (RANDOM.Next(20) == 19)
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 10, 50));
                    }
                    else if (_data.CurrentHour == 20 && !ptr->IsAtHome())
                    {
                        LetHumanTravelHome(ptr);
                    }
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 13
0
        private unsafe void AssignProfession(Human* hptr, EAge previousAge)
        {
            EAge newAgegroup = hptr->GetAge();

            if (newAgegroup != previousAge)
            {
                if (newAgegroup == EAge.Child)
                {
                    hptr->SetProfession(EProfession.Pupil);
                }
                else if (newAgegroup == EAge.Adult)
                {
                    int choice = RANDOM.Next(6);
                    switch (choice)
                    {
                        case 0: hptr->SetProfession(EProfession.Student); break;
                        case 1: hptr->SetProfession(EProfession.Plumber); break;
                        case 2: hptr->SetProfession(EProfession.Commuter); break;
                        case 3: hptr->SetProfession(EProfession.DeskJobber); break;
                        case 4: hptr->SetProfession(EProfession.TravellingSalesman); break;
                        case 5: hptr->SetProfession(EProfession.Housewife); break;
                    }
                }
                else if (newAgegroup == EAge.Senior)
                {
                    hptr->SetProfession(EProfession.Housewife);
                }
                else
                {
                    throw new Exception("Someone seemed to have found the fountain of Youth" +
                    "atleast he wasn't a baby before this Agetick and now he is. MAGIC!");
                }
            }
            else
            {
                if (hptr->GetProfession() == EProfession.Student && hptr->GetAgeInYears() >= 30)
                {
                    int choice = RANDOM.Next(5);
                    switch (choice)
                    {
                        case 0: hptr->SetProfession(EProfession.Plumber); break;
                        case 1: hptr->SetProfession(EProfession.Commuter); break;
                        case 2: hptr->SetProfession(EProfession.DeskJobber); break;
                        case 3: hptr->SetProfession(EProfession.TravellingSalesman); break;
                        case 4: hptr->SetProfession(EProfession.Housewife); break;
                    }
                }
            }
        }
Ejemplo n.º 14
0
        private void MoveTravellingSalesman(Human* ptr)
        {
            switch (ptr->GetMindset())
            {
                //Working Mindset -> Travelling Salesman travels through Germany, changes location each day,
                //                   returns home on friday
                case PopulationData.EMindset.Working:
                    //Returns home on friday
                    if (_data.CurrentDay == DayOfWeek.Friday)
                    {
                        //Travel Home on Friday
                        if (_data.CurrentHour == 4)
                            LetHumanTravelHome(ptr);
                    }
                    //on every other day travel through germany
                    else
                    {
                        if (_data.CurrentHour == 4)
                        {
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 400, 800));
                        }
                        else
                            return;
                    }

                    break;
                case PopulationData.EMindset.DayOff:

                    if (_data.CurrentHour < 12)
                    {
                        //sleep
                        return;
                    }
                    else if (_data.CurrentHour < 20 && ptr->IsAtHome())
                    {
                        if (RANDOM.Next(20) == 19)
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 10, 50));
                    }
                    else if (_data.CurrentHour == 20 && !ptr->IsAtHome())
                    {
                        LetHumanTravelHome(ptr);
                    }
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 15
0
        public void Enqueue(Tuple<int, PopulationCell>[] cells, Human[] humans)
        {
            _queueCells.Enqueue(cells);
            _queueHumans.Enqueue(humans);

            if (_taskCells == null || _taskCells.Status != TaskStatus.Running)
                _taskCells = Task.Run(() => AddCells());
            if (_taskHumans == null || _taskHumans.Status != TaskStatus.Running)
                _taskHumans = Task.Run(() => AddHumans());
        }
Ejemplo n.º 16
0
 protected unsafe abstract void HandleHuman(Human* human);
Ejemplo n.º 17
0
 public Combiner(PopulationCell[] cellArray, Human[] humanArray)
 {
     _cells = cellArray;
     _humans = humanArray;
     _humanPoint = 0;
     _queueCells = new ConcurrentQueue<Tuple<int, PopulationCell>[]>();
     _queueHumans = new ConcurrentQueue<Human[]>();
 }
Ejemplo n.º 18
0
        private Tuple<int, PopulationCell>[] PopulateDepartment(DepartmentInfo depInfo, out Human[] humanArray)
        {
            List<Human> humanList = new List<Human>(depInfo.GetTotal());

            int areaSize = depInfo.Coordinates.Length;                      // number of points to be populated.
            Tuple<int, PopulationCell>[] resultArray =                      // the array which will be returned.
                new Tuple<int,PopulationCell>[areaSize];
            int resultArrayIndex = 0;                                       // running index for the array.

            Point origin = CalculateInitialPoint(depInfo.Coordinates);      // the origin as an fixpoint for every point.

            int maximumDistance = depInfo.Coordinates.Max(                  // the maximum distance possible.
                point => point.Distance(origin)) + 1;

            int[] factors = new int[maximumDistance];                       // array of factor for each distance.
            factors[0] = 225;                                               // start factor.
            for (int i = 1; i < maximumDistance; i++)                       // creating every factor for each distance.
            {
                int previousFactor = factors[i - 1];
                int minus = (int)(previousFactor / 3f / 6);
                int random = RANDOM.Next(previousFactor / 3) - minus;
                random = (int)(random * (1 - i / (float)maximumDistance));
                if (previousFactor - random <= 0) random = 5;
                factors[i] = previousFactor - random;
            }

            foreach (Point currentPoint in depInfo.Coordinates)
            {
                int currentDistance = currentPoint.Distance(origin);        // distance of this point to the origin.
                PopulationCell currentCell = new PopulationCell();

                for (int i = 0; i < 8; i++)                                 // for each age-group.
                {
                    int additionalRand = RANDOM.Next(10) - 5;
                    int numberForEvenSpread =                               // number of humans if everything would be even/the same.
                        (int)(depInfo.Population[i] / (float)areaSize);
                    int numberOfPeopleToSet =                               // number of humans to be set for this age-group.
                        (int)(numberForEvenSpread *
                        (factors[currentDistance] + additionalRand) / 100f);

                    EGender gender = (i < 4) ?                              // the first four are male, the last female.
                        EGender.Male : EGender.Female;

                    var bounds = GetAgeBounds(i);                              // the age-boundaries for the corresponding age-group.
                    int lowerAgeBound = bounds.Item1;
                    int upperAgeBound = bounds.Item2;

                    for (int setCount = 0; setCount < numberOfPeopleToSet; setCount++)
                    {
                        int thisAge = RANDOM.Next(lowerAgeBound, upperAgeBound + 1);
                        Human thisHuman = Human.Create(gender, thisAge, currentPoint.Flatten(WIDTH));
                        humanList.Add(thisHuman);
                        currentCell.Data[i]++;                              // 'adds' the human to its cell.

                        depInfo.Population[i]--;                            // 'removes' the human out of the population.
                    }
                }

                areaSize--;                                                 // 'removes' point from rest-area.

                resultArray[resultArrayIndex++] = new Tuple<int, PopulationCell>(
                        currentPoint.Flatten(WIDTH), currentCell);
            }

            humanArray = humanList.ToArray();
            humanList = null;
            return resultArray;
        }
Ejemplo n.º 19
0
        /// <summary>
        /// The standard method to populate.
        /// </summary>
        private Tuple<int, PopulationCell>[] Populate(DepartmentInfo depInfo, out Human[] humanArray)
        {
            // the count of cells.
            int areaSize = depInfo.Coordinates.Length;

            // precalculating humans. (~ +- 10%)
            for (int i = 0; i < depInfo.Population.Length; ++i)
                depInfo.Population[i] = (int)Math.Round(
                    depInfo.Population[i] * (1 - (RANDOM.Next(20) - 10) / 100f));

            // initializing array.
            var resultArray = new Tuple<int, PopulationCell>[areaSize];
            int resultIndex = 0;

            // the origin used as fixpoint.
            Point origin = CalculateInitialPoint(depInfo.Coordinates);

            // maximum distance possible. (returns a relative small number)
            int maxDistance = depInfo.Coordinates.Max(p => p.Distance(origin)) + 1;

            // array of factors
            double[] factors = new double[maxDistance];
            factors[0] = 1.5;                           // start factor.
            for (int i = 1; i < maxDistance; i++)       // creating every factor for each distance.
            {
                //TODO | dj | randomise
                // (-(1/maxDistance * i)^2 + 1.75) * 100
                double one = 1d / maxDistance * i;
                double two = Math.Pow(one, 2);
                double three = factors[0];
                double four = three - two;
                factors[i] = four;
            }

            // array for each age-group, holding a list of Tuple of cell-indices and count.
            Queue<Tuple<int, ushort>>[] agesOfCells = new Queue<Tuple<int, ushort>>[8];
            agesOfCells.Initialize<Queue<Tuple<int, ushort>>>();

            int humanCount = 0;

            // going through all points and set the counts.
            foreach (Point point in depInfo.Coordinates)
            {
                PopulationCell cell = new PopulationCell();
                int cellIndex = point.Flatten(WIDTH);

                double factor = factors[point.Distance(origin)];
                for (int i = 0; i < 8; ++i)
                {
                    ushort toSet = (ushort)Math.Round(depInfo.Population[i] / (double)areaSize * factor);
                    cell.Data[i] = toSet;
                    agesOfCells[i].Enqueue(new Tuple<int,ushort>(cellIndex, toSet));
                    depInfo.Population[i] -= toSet;
                    //if (cell.Data[i] == 0)
                    //    Console.WriteLine("0 people in {0}", cellIndex);
                }
                if (cell.Total == 0 && depInfo.Population.Sum() != 0)
                {
                    int j = 0;
                    do
                    {
                        j = RANDOM.Next(8);
                    } while (depInfo.Population[j] == 0);

                    cell.Data[j] = 1;
                    agesOfCells[j].Enqueue(new Tuple<int,ushort>(cellIndex, 1));
                    depInfo.Population[j] -= 1;
                }
                areaSize--;

                humanCount += cell.Total;

                resultArray[resultIndex++] = new Tuple<int,PopulationCell>(cellIndex, cell);
            }

            // initializing human array
            humanArray = new Human[humanCount];
            int humanIndex = 0;

            // going through all ages and creating the humans.
            for (int i = 0; i < 8; ++i)
            {
                var queue = agesOfCells[i];
                var bounds = GetAgeBounds(i);

                // age-bounds (for random).
                int lowerAge = bounds.Item1;
                int upperAge = bounds.Item2;

                // gender.
                EGender gender = (i < 4) ? EGender.Male : EGender.Female;

                // for each cell.
                while (queue.Count > 0)
                //foreach (Tuple<int, ushort> cellTuple in queue)
                {
                    var cellTuple = queue.Dequeue();

                    int cellIndex = cellTuple.Item1;
                    ushort count = cellTuple.Item2;

                    // for each human (who shall be created).
                    ushort c = count;
                    while (c-- > 0)
                    //for (int c = 0; c < count; ++c)
                    {
                        int thisHumanAge = RANDOM.Next(lowerAge, upperAge + 1);
                        Human thisHuman = Human.Create(gender, thisHumanAge, cellIndex);
                        humanArray[humanIndex++] = thisHuman;
                    }
                }

                agesOfCells[i] = null;  // not necessary, but who knows :P
            }

            return resultArray;
        }
Ejemplo n.º 20
0
        private unsafe void TryInfection(Human* human, Disease disease, int probability)
        {
            // get index for FactorContainer
            int ageIndex = (byte)human->GetAge() / 32;
            ageIndex += human->GetGender() == EGender.Male ? 0 : 4;

            int resistance = disease.ResistanceFactor.Data[ageIndex];

            if (resistance < probability)
            {
                int factor = probability - resistance;
                int rand = RANDOM.Next(100);
                if (rand <= factor)
                {
                    human->Infect((short)disease.IncubationPeriod, (short)disease.IdleTime);
                    lock (_data.Cells[human->CurrentCell])
                        _data.Cells[human->CurrentCell].Infecting++;
                }
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Moves a selected Human to another Area in the PopulationCellGrid.
        /// </summary>
        /// <param name="data">Reference to SimulationData</param>
        /// <param name="currentcell">The cell the human to move is currently in</param>
        /// <param name="human">The index of the selected human in the current cell</param>
        /// <param name="destinationcell">The index of the cell the human should be moved to</param>
        private void LetHumanTravel(Human* ptr, int destinationcell)
        {
            int maxcelldiffernce = Math.Max(Math.Abs(ptr->CurrentCell % _data.ImageWidth - destinationcell % _data.ImageWidth),
                                      Math.Abs(ptr->CurrentCell / _data.ImageWidth - destinationcell / _data.ImageWidth));

            if (maxcelldiffernce < 75)
            {
                //Add the selected Human in the Destinationcell
                MoveHuman(ptr, destinationcell);
            }
            else
            {
                ptr->TravellingCounter = (byte)((maxcelldiffernce / 5) / 70);
                ptr->DesiredCell = destinationcell;
                ptr->SetTravelling(true);
            }
        }
Ejemplo n.º 22
0
        private void MoveStudent(Human* ptr)
        {
            switch (ptr->GetMindset())
            {
                //Working Mindset -> Student going to University this day
                //Assert : Student is at Home at 0 o'clock; It isn't suturday or sunday; University traveltime <= 1h
                case PopulationData.EMindset.Working:
                    //Student can have lectures during day including breaks
                    if (_data.CurrentHour > 8 && _data.CurrentHour < 18)
                    {
                        if (ptr->IsAtHome())
                        {
                            //chance to go to university
                            if (RANDOM.Next(3) == 2)
                                LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 3, 75));
                            else
                                return;
                        }
                        else
                        {
                            //chance to go home
                            if (RANDOM.Next(9) == 8)
                                LetHumanTravelHome(ptr);
                            else
                                return;

                        }
                    }
                    //No lectures after 18 o'clock
                    else if (_data.CurrentHour == 18)
                    {
                        LetHumanTravelHome(ptr);
                    }
                    //in the morning/evening student is at home
                    else
                        return;

                    break;

                //DayOff Mindset :
                case EMindset.DayOff:

                    if (_data.CurrentHour == 6 && !ptr->IsAtHome())
                    {
                        LetHumanTravelHome(ptr);
                    }
                    else if (_data.CurrentHour > 6 && _data.CurrentHour < 19)
                    {
                        if (ptr->IsAtHome() && RANDOM.Next(6) == 5)
                        {
                            LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 0, 74));
                        }
                    }
                    else if (_data.CurrentHour == 19 && !ptr->IsAtHome())
                    {
                        LetHumanTravelHome(ptr);
                    }
                    else if (_data.CurrentHour == 22 && RANDOM.Next(2) == 1)
                    {
                        //Party
                        LetHumanTravel(ptr, FindCellInReach(ptr->CurrentCell, 25, 75));
                    }
                    else
                        return;

                    break;
            }
        }
Ejemplo n.º 23
0
 private void LetHumanTravelHome(Human* ptr)
 {
     LetHumanTravel(ptr, ptr->HomeCell);
 }
Ejemplo n.º 24
0
 private void SetUp()
 {
     r = new Random((int)DateTime.Now.Ticks);
     h1 = Human.Create(EGender.Female, AGE_FEMALE, HOMECELL);
     h2 = Human.Create(EGender.Male, AGE_MALE, HOMECELL);
 }
Ejemplo n.º 25
0
        private unsafe void UpdateHumanChangeToCell(Human* hptr, SimulationData data, EAge previousAge)
        {
            EAge newAge = hptr->GetAge();

            if (previousAge != newAge)
            {

                lock (data.Cells[hptr->CurrentCell])
                {
                    PopulationCell currentcell = data.Cells[hptr->CurrentCell];

                    if (hptr->GetGender() == EGender.Female)
                    {
                        if (newAge == EAge.Child)
                        {
                            --currentcell.FemaleBabies;
                            ++currentcell.FemaleChildren;
                        }
                        else if (newAge == EAge.Adult)
                        {
                            --currentcell.FemaleChildren;
                            ++currentcell.FemaleAdults;
                        }
                        else if (newAge == EAge.Senior)
                        {
                            --currentcell.FemaleAdults;
                            ++currentcell.FemaleSeniors;
                        }
                    }
                    else
                    {
                        if (newAge == EAge.Child)
                        {
                            --currentcell.MaleBabies;
                            ++currentcell.MaleChildren;
                        }
                        else if (newAge == EAge.Adult)
                        {
                            --currentcell.MaleChildren;
                            ++currentcell.MaleAdults;
                        }
                        else if (newAge == EAge.Senior)
                        {
                            --currentcell.MaleAdults;
                            ++currentcell.MaleSeniors;
                        }
                    }
                }
            }
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Factory method from which Humans are created
        /// </summary>
        /// <param name="gender">Gender of the Human to create</param>
        /// <param name="age">Age in Years of the Human to create (1-110)</param>
        /// <param name="homeCell">HomeCell of the Human to create</param>
        /// <returns>The newly created Human struct</returns>
        public static Human Create(EGender gender, int age, int homeCell)
        {
            var human = new Human(homeCell);

            human.SetGender(gender);
            human.SetAge(age);
            human.SetDeath(false);
            if (human.GetAge() == EAge.Baby)
            {
                human.SetMindset(EMindset.Stationary);
            }
            else if (human.GetAge() == EAge.Child)
            {
                human.SetProfession(EProfession.Pupil);
                human.SetMindset(EMindset.Working);
            }
            else if (human.GetAge() == EAge.Adult)
            {
                int result = RANDOM.Next(100);
                if (result < 10)
                {
                    human.SetProfession(EProfession.Student); //10%
                    human.SetMindset(EMindset.Working);
                }
                else if (result < 35)
                {
                    human.SetProfession(EProfession.Plumber); //25%
                    human.SetMindset(EMindset.Working);
                }
                else if (result < 60)
                {
                    human.SetProfession(EProfession.DeskJobber); //25%
                    human.SetMindset(EMindset.Working);
                }
                else if (result < 80)
                {
                    human.SetProfession(EProfession.Commuter); //20%
                    human.SetMindset(EMindset.Working);
                }
                else if (result < 90)
                {
                    human.SetProfession(EProfession.TravellingSalesman);//10%
                    human.SetMindset(EMindset.Working);
                }
                else
                {
                    human.SetProfession(EProfession.Housewife); //10%
                    human.SetMindset(EMindset.Working);
                }
            }
            else if (human.GetAge() == EAge.Senior)
            {
                human.SetProfession(EProfession.Unemployed);
                human.SetMindset(EMindset.HomeStaying);
            }

            return human;
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Reads the information out of
        /// the given file and sets the
        /// corresponding values.
        /// </summary>
        /// <param name="filePath">The path to the .dep-file.</param>
        /// <exception cref="IOException">Might be thrown.</exception>
        public void InitializeFromFile(string filePath)
        {
            var reader = new DepartmentMapReader(filePath);
            reader.IterationPassed += ReadFileIterationPassed.Raise;

            DepartmentInfo[] deps = reader.ReadFile();

            Image img = reader.ReadImage();
            ImageWidth = img.Width;
            ImageHeight = img.Height;

            WriteMessage("Calculating Total-Population...");
            int maxPopulation = deps.Sum(x => x.GetTotal());        // getting maximum population
            Humans = new Human[(int)(maxPopulation * 1.05f)];       // adding 5% :)

            WriteMessage("Generating Matrix...");
            Cells = new PopulationCell[ImageWidth * ImageHeight];

            MatrixGenerator mg = new MatrixGenerator(false);
            mg.DepartmentCalculationFinished += DepartmentCalculated.Raise;
            mg.GenerateMatrix(Cells, Humans, deps, ImageWidth, ImageHeight);

            WriteMessage("...Matrix generated.");
        }