Пример #1
0
        public static Range InitMonteCarlo(MainProperties properties)
        {
            AllGrainsTypes = new Grain[properties.AmountOfGrains];
            Range tempRange = new Range(properties.RangeWidth, properties.RangeHeight);

            // border
            AddBlackBorder(tempRange);

            // set random starting coordinates [x,y] and color for grains
            for (int grainNumber = 1; grainNumber <= properties.AmountOfGrains; grainNumber++)
            {
                AllGrainsTypes[grainNumber - 1] = new Grain()
                {
                    Color = Color.FromArgb(Random.Next(10, 240), Random.Next(10, 240), Random.Next(2, 240)),
                    Id    = grainNumber,
                };
            }

            for (int i = 1; i < tempRange.Width - 1; i++)
            {
                for (int j = 1; j < tempRange.Height - 1; j++)
                {
                    int r = Random.Next(AllGrainsTypes.Length);
                    tempRange.GrainsArray[i, j] = new Grain()
                    {
                        Id    = AllGrainsTypes[r].Id,
                        Color = AllGrainsTypes[r].Color,
                    };
                }
            }

            tempRange.StructureBitmap = new Bitmap(properties.RangeWidth, properties.RangeHeight);
            tempRange.IsFull          = false;
            return(tempRange);
        }
        public static Range InitCellularAutomata(MainProperties properties, int id, Color grainColor)
        {
            Range tempRange = new Range(properties.RangeWidth, properties.RangeHeight);

            InitBlankArea(tempRange);

            Point coordinates = GetRandomPoint(tempRange.Width, tempRange.Height);
            if (properties.StartingPointsType == StartingPointsType.RandomBoundary)
            {
                // set random starting coordinates [x,y] and color for grains on circle boundary in random way
                for (int grainNumber = 1; grainNumber <= properties.AmountOfGrains; grainNumber++)
                {
                    do
                    {
                        coordinates = GetRandomPoint(tempRange.Width, tempRange.Height);
                    }
                    while (tempRange.GrainsArray[coordinates.X, coordinates.Y].Id != (int)SpecialId.Id.Border);

                    tempRange.GrainsArray[coordinates.X, coordinates.Y].Id = id;
                    tempRange.GrainsArray[coordinates.X, coordinates.Y].Color = grainColor;
                }
            }
            else if (properties.StartingPointsType == StartingPointsType.RegularBoundary)
            {
                // set random starting coordinates [x,y] and color for grains on circle boundary (equal sections*)
                for (int grainNumber = 1; grainNumber <= properties.AmountOfGrains; grainNumber++)
                {
                    double angle = (double)grainNumber * (360.0 / (double)properties.AmountOfGrains);
                    coordinates.X = (int)(Constants.MIDDLE_POINT.X + (Constants.RADIUS-2) * Math.Cos(angle * Math.PI / 180.0));
                    coordinates.Y = (int)(Constants.MIDDLE_POINT.Y + (Constants.RADIUS-2) * Math.Sin(angle * Math.PI / 180.0));

                    tempRange.GrainsArray[coordinates.X, coordinates.Y].Id = id;
                    tempRange.GrainsArray[coordinates.X, coordinates.Y].Color = grainColor;
                }
            }
            else
            {
                // set random starting coordinates [x,y] and color for grains inside circle
                for (int grainNumber = 1; grainNumber <= properties.AmountOfGrains; grainNumber++)
                {
                    do
                    {
                        coordinates = GetRandomPoint(tempRange.Width, tempRange.Height);
                    }
                    while (tempRange.GrainsArray[coordinates.X, coordinates.Y].Id != (int)SpecialId.Id.Empty);

                    tempRange.GrainsArray[coordinates.X, coordinates.Y].Id = id;
                    tempRange.GrainsArray[coordinates.X, coordinates.Y].Color = grainColor;
                }
            }
            return tempRange;
        }
Пример #3
0
        public static Range InitCellularAutomata(MainProperties properties)
        {
            AllGrainsTypes = new Grain[properties.AmountOfGrains];

            Range tempRange = new Range(properties.RangeWidth, properties.RangeHeight);

            tempRange.IsFull = false;

            // border
            AddBlackBorder(tempRange);

            // init grains array by white color (default)
            for (int i = 1; i < tempRange.Width - 1; i++)
            {
                for (int j = 1; j < tempRange.Height - 1; j++)
                {
                    if (tempRange.GrainsArray[i, j] == null)
                    {
                        tempRange.GrainsArray[i, j] = new Grain()
                        {
                            Id    = (int)SpecialId.Id.Empty,
                            Color = Color.White,
                        }
                    }
                }
            }
            ;

            tempRange.StructureBitmap = new Bitmap(properties.RangeWidth, properties.RangeHeight);

            // set random starting coordinates [x,y] and color for grains
            Point coordinates;

            for (int grainNumber = 1; grainNumber <= properties.AmountOfGrains; grainNumber++)
            {
                do
                {
                    coordinates = RandomCoordinates.Get(tempRange.Width, tempRange.Height, Random);
                }while (tempRange.GrainsArray[coordinates.X, coordinates.Y].Id != 0);

                AllGrainsTypes[grainNumber - 1] = new Grain()
                {
                    Color = Color.FromArgb(Random.Next(10, 240), Random.Next(10, 240), Random.Next(2, 240)),
                    Id    = grainNumber,
                };

                tempRange.GrainsArray[coordinates.X, coordinates.Y].Color = AllGrainsTypes[grainNumber - 1].Color;
                tempRange.GrainsArray[coordinates.X, coordinates.Y].Id    = AllGrainsTypes[grainNumber - 1].Id;
            }
            return(tempRange);
        }
 private void SetProperties()
 {
     properties = new MainProperties()
     {
         RangeWidth        = (int)Image.Width,
         RangeHeight       = (int)Image.Height,
         AmountOfGrains    = Converters.StringToInt(NumOfGrainsTextBox.Text),
         NeighbourhoodType = ChooseNeighbourhoodType(),
         GrowthProbability = Converters.StringToInt(GrowthProbabilityTextBox.Text),
         MCS = Converters.StringToInt(MCSTextBox.Text),
         SubstructuresType = ChooseSubstructuresType(),
         MethodType        = ChooseMethodType()
     };
     inclusions = new InitInclusions()
     {
         CreationTime       = ChooseInlcusionCreationTime(),
         InclusionsType     = ChooseInclusionsType(),
         IsEnable           = (bool)InclusionsCheckBox.IsChecked,
         AmountOfInclusions = Converters.StringToInt(NumOfInclusionsTextBox.Text),
         Size = Converters.StringToInt(SizeOfInclusionsTextBox.Text)
     };
     boundaries = new InitBoundaries(properties);
     nucleons   = new InitNucleons()
     {
         IsEnable              = (bool)SRXCheckBox.IsChecked,
         AmountOfNucleons      = Converters.StringToInt(NumOfNucleonsTextBox.Text),
         NucleonsStates        = new Color[Converters.StringToInt(NumOfStatesTextBox_SRX.Text)],
         TypeOfcreation        = ChooseTypeOfNucleonsCreation(),
         EnergyDistribution    = ChooseEnegryDistribution(),
         EnergyInside          = Converters.StringToInt(EnergyInsideTextBox.Text),
         EnergyOnEdges         = Converters.StringToInt(EnergyOnEdgesTextBox.Text),
         PositionDistribiution = ChoosePositionDistribution(),
         EnergyRange           = new Range()
     };
     tempIteration = Converters.StringToInt(MCSTextBox.Text);
 }
Пример #5
0
        private int?[] Vj = new int?[10]; //beta

        public MainWindow()
        {
            InitializeComponent();
            properties = new MainProperties();
        }
Пример #6
0
        public Range Grow(Range prevRange, MainProperties properties)
        {
            var currRange = new Range(prevRange.Width, prevRange.Height, true);

            InitStructures.AddBlackBorder(currRange);

            var isGrowthMoore2 = properties.NeighbourhoodType == NeighbourhoodType.Moore2 ? true : false;

            List <Grain> neighbourhood = new List <Grain>();

            for (int i = 1; i < prevRange.Width - 1; i++)
            {
                for (int j = 1; j < prevRange.Height - 1; j++)
                {
                    if (prevRange.GrainsArray[i, j].Id != (int)SpecialId.Id.Empty)
                    {
                        // just init if there is already some color (not white)
                        currRange.GrainsArray[i, j] = prevRange.GrainsArray[i, j];
                    }
                    else
                    {
                        if (!isGrowthMoore2)
                        {
                            // ordinary types of growth - list of Moore or Neuman neighbourhood
                            switch (properties.NeighbourhoodType)
                            {
                            case NeighbourhoodType.Moore:
                                neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);
                                break;

                            case NeighbourhoodType.Neumann:
                                neighbourhood = TakeNeumannNeighbourhood(i, j, prevRange.GrainsArray);
                                break;
                            }

                            var most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                       .GroupBy(g => g.Id);

                            if (most.Any())
                            {
                                // assign grain which are the most in the list of neighborhoods
                                currRange.GrainsArray[i, j] = most.OrderByDescending(g => g.Count())
                                                              .Select(g => g.First()).First();
                            }
                            else
                            {
                                currRange.GrainsArray[i, j] = new Grain()
                                {
                                    Id    = (int)SpecialId.Id.Empty,
                                    Color = Color.White
                                };
                                currRange.IsFull = false;
                            }
                        }
                        else
                        {
                            // MOORE 2

                            var grainGrowth = false;

                            // rule 1 - ordinary moore
                            neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);

                            var most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                       .GroupBy(g => g.Id);

                            if (most.Any())
                            {
                                most = most.OrderByDescending(g => g.Count());

                                if (most.First().Count() >= 5 && most.First().Count() <= 8)
                                {
                                    currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                    grainGrowth = true;
                                }
                                else
                                {
                                    // rule 2 - nearest moore
                                    neighbourhood = TakeNearestMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                    most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                           .GroupBy(g => g.Id);

                                    if (most.Any())
                                    {
                                        most = most.OrderByDescending(g => g.Count());
                                        if (most.First().Count() == 3)
                                        {
                                            currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                            grainGrowth = true;
                                        }
                                    }
                                    if (!grainGrowth)
                                    {
                                        // rule 3 - further moore
                                        neighbourhood = TakeFurtherMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                        most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                               .GroupBy(g => g.Id);

                                        if (most.Any())
                                        {
                                            most = most.OrderByDescending(g => g.Count());
                                            if (most.First().Count() == 3)
                                            {
                                                currRange.GrainsArray[i, j] = most.Select(g => g.First()).First();
                                                grainGrowth = true;
                                            }
                                        }
                                    }
                                    if (!grainGrowth)
                                    {
                                        // rule 4 - ordinary moore with probability
                                        neighbourhood = TakeMooreNeighbourhood(i, j, prevRange.GrainsArray);

                                        most = neighbourhood.Where(g => (!SpecialId.IsIdSpecial(g.Id)))
                                               .GroupBy(g => g.Id);

                                        var randomProbability = Random.Next(0, 100);
                                        if (most.Any() && (randomProbability <= properties.GrowthProbability))
                                        {
                                            currRange.GrainsArray[i, j] = most.OrderByDescending(g => g.Count())
                                                                          .Select(g => g.First()).First();
                                            grainGrowth = true;
                                        }
                                    }
                                }
                            }
                            if (!grainGrowth)
                            {
                                // if grain not exist
                                currRange.GrainsArray[i, j] = new Grain()
                                {
                                    Id    = (int)SpecialId.Id.Empty,
                                    Color = Color.White
                                };
                                currRange.IsFull = false;
                            }
                        }
                    }
                }
            }
            UpdateBitmap(currRange);
            return(currRange);
        }
Пример #7
0
        private void SetProperties()
        {
            MainBitmap = new Bitmap((int)PelletImage.Width, (int)PelletImage.Height);

            // properties
            Properties = new MainProperties()
            {
                RangeWidth             = (int)PelletImage.Width,
                RangeHeight            = (int)PelletImage.Height,
                AmountOfGrains         = int.Parse(AmountOfGrainsTextBox.Text),
                AmountOfInclusions     = int.Parse(AmountOfInclusionsTextBox.Text),
                PelletSize             = int.Parse(PelletSizeTextBox.Text),
                CurrGrowthProbability  = int.Parse(ConstGrowthProbabilityTextBox.Text),
                ConstGrowthProbability = int.Parse(ConstGrowthProbabilityTextBox.Text),
                CurrTemperature        = 0,
                TemperatureRiseRate    = int.Parse(TemperatureRiseRateTextBox.Text),
                MaxTemperature         = int.Parse(MaxTemperatureTextBox.Text),
                BufferTemperature      = 0,
                NeighbourhoodType      = ChooseNeighbourhoodType(),
                StartingPointsType     = ChooseStartingPointsType(),
            };

            // update constants
            Constants.UpdateConstants(Properties.PelletSize);

            // initialization inclusion points inside update area
            InclusionHandler.InitPoints(Properties.AmountOfInclusions);

            // initialization inclusion points inside update area
            StructureHandler.PointsArea = StructureHandler.GetPointsInsideCircle((int)Constants.RADIUS, Constants.MIDDLE_POINT);

            // phases
            Phase1NameLabel.Content = Phase1NameTextBox.Text + " [%]";
            Phase2NameLabel.Content = Phase2NameTextBox.Text + " [%]";
            Phase3NameLabel.Content = Phase3NameTextBox.Text + " [%]";
            Phase4NameLabel.Content = Phase4NameTextBox.Text + " [%]";

            Phases[0].Name = Phase1NameTextBox.Text;
            Phases[1].Name = Phase2NameTextBox.Text;
            Phases[2].Name = Phase3NameTextBox.Text;
            Phases[3].Name = Phase4NameTextBox.Text;

            Phases[0].TemperaturePoint = int.Parse(Phase1TextBox.Text);
            Phases[1].TemperaturePoint = int.Parse(Phase2TextBox.Text);
            Phases[2].TemperaturePoint = int.Parse(Phase3TextBox.Text);
            Phases[3].TemperaturePoint = int.Parse(Phase4TextBox.Text);

            Phases[0].Color = Converters.WindowsToDrawingColor(Phase1ColorPicker.SelectedColor.Value);
            Phases[1].Color = Converters.WindowsToDrawingColor(Phase2ColorPicker.SelectedColor.Value);
            Phases[2].Color = Converters.WindowsToDrawingColor(Phase3ColorPicker.SelectedColor.Value);
            Phases[3].Color = Converters.WindowsToDrawingColor(Phase4ColorPicker.SelectedColor.Value);

            Phases[0].GrowthProbability = int.Parse(Phase1PropabilityTextBox.Text);
            Phases[1].GrowthProbability = int.Parse(Phase2PropabilityTextBox.Text);
            Phases[2].GrowthProbability = int.Parse(Phase3PropabilityTextBox.Text);
            Phases[3].GrowthProbability = int.Parse(Phase4PropabilityTextBox.Text);

            for (int i = 0; i < Constants.NUMBER_OF_PHASES; i++)
            {
                Phases[i].Range = new Range(Properties.RangeWidth, Properties.RangeWidth);
                StructureHandler.InitBlankArea(Phases[i].Range);
                InclusionHandler.AddInclusions(Phases[i].Range);
                Phases[i].Percentage = 0;

                if (ConstantGrowthRadioButton.IsChecked == true)
                {
                    Phases[i].GrowthProbability = Properties.ConstGrowthProbability;
                }
                else if (ProgresiveGrowthRadioButton.IsChecked == true)
                {
                    Phases[i].GrowthProbability = (100 / Constants.NUMBER_OF_PHASES) * (i + 1);
                }

                Properties.CurrGrowthProbability = Phases[i].GrowthProbability;
                Phases[i].Properties             = ObjectCopier.Clone(Properties);

                Phases[i].PrevRange = StructureHandler.InitCellularAutomata(Properties, i + 1, Phases[i].Color);
                InclusionHandler.AddInclusions(Phases[i].PrevRange);
            }

            // !! PHASE 1 - INITIALIZATION !! //
            StructureHandler.InstantFillColor(Phases[0], 1, Phases[0].Color);

            // temperature dispatcher
            TempteratureDispatcher.Interval = new TimeSpan(0, 0, 0, 0, Properties.TemperatureRiseRate);

            // .csv file
            csv = new StringBuilder();
            var newLine = string.Format("{0};{1};{2};{3};{4};{5}", "Time", "Temperature", Phases[0].Name, Phases[1].Name, Phases[2].Name, Phases[3].Name);

            csv.AppendLine(newLine);
        }
        public Range Grow(Range prevRange, MainProperties properties)
        {
            var currRange = new Range(prevRange.Width, prevRange.Height);

            StructureHandler.GrainsArrayInit(currRange);
            StructureHandler.UpdateInsideCircle(currRange);

            foreach (var p in StructureHandler.PointsArea)
            {
                if (prevRange.GrainsArray[p.X, p.Y].Id != (int)SpecialId.Id.Empty)
                {
                    // change if there is already some color (not white)
                    currRange.GrainsArray[p.X, p.Y].Id    = prevRange.GrainsArray[p.X, p.Y].Id;
                    currRange.GrainsArray[p.X, p.Y].Color = prevRange.GrainsArray[p.X, p.Y].Color;
                }
                else
                {
                    if (properties.NeighbourhoodType != NeighbourhoodType.Moore2)
                    {
                        if (properties.NeighbourhoodType == NeighbourhoodType.Moore)
                        {
                            NeighborhoodList = TakeMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray);
                        }
                        else
                        {
                            NeighborhoodList = TakeNeumannNeighbourhood(p.X, p.Y, prevRange.GrainsArray);
                        }

                        var MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id));

                        if (MostOfCells.Any())
                        {
                            // assign grain which are the most in the list of neighborhood
                            currRange.GrainsArray[p.X, p.Y] = MostOfCells.GroupBy(g => g.Id).OrderByDescending(g => g.Count())
                                                              .Select(g => g.First()).First();
                        }
                    }
                    else
                    {
                        // MOORE 2
                        IsGrainUpdate = false;

                        // rule 1 - ordinary Moore
                        NeighborhoodList = TakeMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray);

                        MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id))
                                      .GroupBy(g => g.Id).OrderByDescending(g => g.Count());

                        if (MostOfCells.Any())
                        {
                            if (MostOfCells.First().Count() >= 5 && MostOfCells.First().Count() <= 8)
                            {
                                currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First();
                            }
                            else
                            {
                                // rule 2 - nearest Moore (Von Neumann)
                                NeighborhoodList = TakeNearestMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray);

                                MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id))
                                              .GroupBy(g => g.Id).OrderByDescending(g => g.Count());

                                if (MostOfCells.Any())
                                {
                                    if (MostOfCells.First().Count() == 3)
                                    {
                                        currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First();
                                        IsGrainUpdate = true;
                                    }
                                }
                                if (!IsGrainUpdate)
                                {
                                    // rule 3 - further Moore
                                    NeighborhoodList = TakeFurtherMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray);

                                    MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id))
                                                  .GroupBy(g => g.Id).OrderByDescending(g => g.Count());

                                    if (MostOfCells.Any())
                                    {
                                        if (MostOfCells.First().Count() == 3)
                                        {
                                            currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First();
                                            IsGrainUpdate = true;
                                        }
                                    }
                                    if (!IsGrainUpdate)
                                    {
                                        // rule 4 - ordinary Moore with probability
                                        NeighborhoodList = TakeMooreNeighbourhood(p.X, p.Y, prevRange.GrainsArray);

                                        MostOfCells = NeighborhoodList.Where(g => !SpecialId.IsSpecialId(g.Id))
                                                      .GroupBy(g => g.Id).OrderByDescending(g => g.Count());

                                        if (MostOfCells.Any() && Random.Next(0, 100) <= properties.CurrGrowthProbability)
                                        {
                                            currRange.GrainsArray[p.X, p.Y] = MostOfCells.Select(g => g.First()).First();
                                            IsGrainUpdate = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(currRange);
        }