Ejemplo n.º 1
0
        private void Canvas_OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (Condition == null)
            {
                return;
            }
            var ic = PointMatrixPick.CreateICFromPick(Condition);
            var p  = e.GetPosition(Canvas);
            var X  = _x + _scale * p.X / (Canvas.Width / (ic.Grid.CellGrid.GetLength(0)));

            if (X >= ic.Grid.CellGrid.GetLength(0))
            {
                return;
            }
            var Y = _y + _scale * p.Y / (Canvas.Height / (ic.Grid.CellGrid.GetLength(1)));

            if (Y >= ic.Grid.CellGrid.GetLength(1))
            {
                return;
            }
            _scale += Math.Sign(-e.Delta) * 0.1;
            if (_scale < 0.1)
            {
                _scale = 0.1;
            }
            if (_scale > 1)
            {
                _scale = 1;
            }

            var width   = ic.Grid.CellGrid.GetLength(0);
            var height  = ic.Grid.CellGrid.GetLength(1);
            var nwidth  = (int)(width * _scale);
            var nheight = (int)(height * _scale);
            var x       = (int)X - (nwidth / 2);
            var y       = (int)Y
                          - (nheight / 2);
            var xx = (int)X + (nwidth / 2);
            var yy = (int)Y + (nheight / 2);

            if (xx >= width)
            {
                x -= (xx - width) + 1;
            }
            if (yy >= height)
            {
                y -= (yy - height) + 1;
            }
            if (x < 0)
            {
                x = 0;
            }
            if (y < 0)
            {
                y = 0;
            }
            _x        = x;
            _y        = y;
            Condition = PointMatrixPick.CreatePickFromIC(ic, Condition);
        }
Ejemplo n.º 2
0
        public PointMatrixPick Resize(int Size)
        {
            PointMatrixPick result = new PointMatrixPick
            {
                Matrices = Matrices.ToList(),
                Indices  = new int[Size, Size]
            };

            double d = (double)Size / this.Size;

            for (int i = 0; i < Size; i++)
            {
                double index = i / d;
                int    k     = (int)Math.Floor(index);
                int    l     = k + 1;
                double prob  = 1 - (index - k);
                int    I     = random.NextDouble() < prob ? k : l;
                for (int j = 0; j < Size; j++)
                {
                    index = i / d;
                    k     = (int)Math.Floor(index);
                    l     = k + 1;
                    prob  = 1 - (index - k);
                    int J = random.NextDouble() < prob ? k : l;
                    result.Indices[i, j] = Indices[I, J];
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            double[] d = Validate();

            PointMatrix baseMatrix = new PointMatrix((float)d[3], (float)d[2], (float)d[1], (float)d[0]);
            SPDView     spdView    = new SPDView(AdvancedPointMatrix?pointMatrix:PointMatrixPick.SingularMatrixCondition(baseMatrix, _ic.Grid.CellGrid.GetLength(0), _ic.Grid.CellGrid.GetLength(1)), Transform(_ic.Grid), GetNeighboursCount((Neighbourhoods)NeighbourBox.SelectedItem, (int)Slider1.Value), GetNeighbourhood((Neighbourhoods)NeighbourBox.SelectedItem, (Shape)ShapeBox.SelectedItem, (int)Slider1.Value, _ic.Grid.CellGrid.GetLength(0), _ic.Grid.CellGrid.GetLength(1)));

            spdView.ShowDialog();
        }
Ejemplo n.º 4
0
 public static PointMatrixPick SingularMatrixCondition(PointMatrix M, int size1,int size2)
 {
     PointMatrixPick p = new PointMatrixPick
     {
         Indices = new int[size1, size2],
         Matrices = new List<PointMatrix> {M}
     };
     for (int i = 0; i < size1; i++) for (int j = 0; j < size2; j++) p.Indices[i, j] = 0;
     return p;
 }
Ejemplo n.º 5
0
 public void ReCreateCondition()
 {
     ResetScale();
     if (ComboBoxCopy.SelectedIndex < 0)
     {
         return;
     }
     Condition = PointMatrixPick.CreatePickFromIC(
         _conditions[_conditionNames[ComboBoxCopy.SelectedIndex].Item2](
             _conditionNames[ComboBoxCopy.SelectedIndex].Item2.Item2, Size, (int)MatrixCount, false), Condition);
 }
Ejemplo n.º 6
0
        private void UpdateScreen()
        {
            Canvas.Children.Clear();
            var ic = PointMatrixPick.CreateICFromPick(Condition);

            Canvas.Children.Add(new Image
            {
                Source =
                    GenerateImage(_x, _y, (int)(ic.Grid.CellGrid.GetLength(0) * _scale),
                                  (int)(ic.Grid.CellGrid.GetLength(1) * _scale))
            });
        }
Ejemplo n.º 7
0
 public static PointMatrixPick CreatePickFromIC(InitialConditions IC,PointMatrixPick _pick)
 {
     PointMatrixPick Condition=new PointMatrixPick();
     Condition.Matrices = _pick.Matrices.ToList();
     Condition.Indices = new int[IC.Grid.CellGrid.GetLength(0), IC.Grid.CellGrid.GetLength(1)];
     for (int i = 0; i < IC.Grid.CellGrid.GetLength(0); i++)
     {
         for (int j = 0; j < IC.Grid.CellGrid.GetLength(1); j++)
         {
             Condition.Indices[i, j] = IC.Grid.CellGrid[i, j].Value >= Condition.Matrices.Count ? Condition.Matrices.Count - 1 : IC.Grid.CellGrid[i, j].Value;
         }
     }
     return Condition;
 }
Ejemplo n.º 8
0
        public static PointMatrixPick CreatePickFromIC(InitialConditions IC, PointMatrixPick _pick)
        {
            PointMatrixPick Condition = new PointMatrixPick();

            Condition.Matrices = _pick.Matrices.ToList();
            Condition.Indices  = new int[IC.Grid.CellGrid.GetLength(0), IC.Grid.CellGrid.GetLength(1)];
            for (int i = 0; i < IC.Grid.CellGrid.GetLength(0); i++)
            {
                for (int j = 0; j < IC.Grid.CellGrid.GetLength(1); j++)
                {
                    Condition.Indices[i, j] = IC.Grid.CellGrid[i, j].Value >= Condition.Matrices.Count ? Condition.Matrices.Count - 1 : IC.Grid.CellGrid[i, j].Value;
                }
            }
            return(Condition);
        }
Ejemplo n.º 9
0
 public static InitialConditions CreateICFromPick(PointMatrixPick Condition)
 {
     var IC = new InitialConditions();
     var IG = new InitialConditionsGrid();
     IG.CellGrid = new InitialConditionCell[Condition.Indices.GetLength(0), Condition.Indices.GetLength(1)];
     for (int i = 0; i < Condition.Indices.GetLength(0); i++)
     {
         for (int j = 0; j < Condition.Indices.GetLength(1); j++)
         {
             IG.CellGrid[i, j] = new InitialConditionCell(i, j, Condition.Indices[i, j], Condition.Indices[i, j]);
         }
     }
     IC.Grid = IG;
     return IC;
 }
Ejemplo n.º 10
0
 private void RandomSize_DragCompleted(object sender, DragCompletedEventArgs e)
 {
     ResetScale();
     if (Condition == null)
     {
         return;
     }
     if (ComboBoxCopy.SelectedIndex < 0)
     {
         Condition = PointMatrixPick.CreatePickFromIC(InitialConditions.GenerateRandom(Size, (int)MatrixCount), Condition);
         return;
     }
     Condition = PointMatrixPick.CreatePickFromIC(
         _conditions[_conditionNames[ComboBoxCopy.SelectedIndex].Item2](
             _conditionNames[ComboBoxCopy.SelectedIndex].Item2.Item2, Size, (int)MatrixCount, false), Condition);
 }
Ejemplo n.º 11
0
        public static InitialConditions CreateICFromPick(PointMatrixPick Condition)
        {
            var IC = new InitialConditions();
            var IG = new InitialConditionsGrid();

            IG.CellGrid = new InitialConditionCell[Condition.Indices.GetLength(0), Condition.Indices.GetLength(1)];
            for (int i = 0; i < Condition.Indices.GetLength(0); i++)
            {
                for (int j = 0; j < Condition.Indices.GetLength(1); j++)
                {
                    IG.CellGrid[i, j] = new InitialConditionCell(i, j, Condition.Indices[i, j], Condition.Indices[i, j]);
                }
            }
            IC.Grid = IG;
            return(IC);
        }
Ejemplo n.º 12
0
        private void Canvas_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (Operation.None == _selectedOperation)
            {
                return;
            }

            if (_comboBox.SelectedIndex < 0)
            {
                return;
            }
            var p  = e.GetPosition(Canvas);
            var ic = PointMatrixPick.CreateICFromPick(Condition);
            var x  = _x + _scale * p.X / (Canvas.Width / (ic.Grid.CellGrid.GetLength(0)));

            if (x >= ic.Grid.CellGrid.GetLength(0))
            {
                return;
            }
            var y = _y + _scale * p.Y / (Canvas.Height / (ic.Grid.CellGrid.GetLength(1)));

            if (y >= ic.Grid.CellGrid.GetLength(1))
            {
                return;
            }
            if (Operation.Check == _selectedOperation)
            {
                ic.Grid.CellGrid[(int)x, (int)y].Value = InitialConditions.GetTransformation((int)MatrixCount)(_comboBox.SelectedIndex);
            }
            else
            {
                var k = ic.Grid.CellGrid[(int)x, (int)y].Value;
                for (var i = 0; i < ic.Grid.CellGrid.GetLength(0); i++)
                {
                    for (var j = 0; j < ic.Grid.CellGrid.GetLength(1); j++)
                    {
                        if (ic.Grid.CellGrid[i, j].Value == k)
                        {
                            ic.Grid.CellGrid[i, j].Value = InitialConditions.GetTransformation((int)MatrixCount)(_comboBox.SelectedIndex);
                        }
                    }
                }
            }
            Condition = PointMatrixPick.CreatePickFromIC(ic, Condition);
        }
Ejemplo n.º 13
0
        public static PointMatrixPick SingularMatrixCondition(PointMatrix M, int size1, int size2)
        {
            PointMatrixPick p = new PointMatrixPick
            {
                Indices  = new int[size1, size2],
                Matrices = new List <PointMatrix> {
                    M
                }
            };

            for (int i = 0; i < size1; i++)
            {
                for (int j = 0; j < size2; j++)
                {
                    p.Indices[i, j] = 0;
                }
            }
            return(p);
        }
Ejemplo n.º 14
0
        private void ButtonBase_OnClick1(object sender, RoutedEventArgs e)
        {
            var bf  = new BinaryFormatter();
            var ofd = new OpenFileDialog
            {
                Filter      = "Initial Condition File (*.cic)|*.cic",
                Multiselect = false
            };

            var result = ofd.ShowDialog();

            if (result.HasValue && result.Value)
            {
                var fs  = new FileStream(ofd.FileName, FileMode.Open);
                var obj = bf.Deserialize(fs);
                Condition = PointMatrixPick.CreatePickFromIC(obj as InitialConditions, Condition);
                fs.Close();
            }
        }
Ejemplo n.º 15
0
        private void AdvancedMatrix_OnClick(object sender, RoutedEventArgs e)
        {
            AdvancedPointMatrix = true;
            var d = Validate();

            if (d == null)
            {
                MessageBox.Show("Podstawowa Macierz musi być poprawna przed przejściem do zaawansowanych ustawień");
            }
            var D = d.Select(x => (float)x).ToArray();
            PointMatrixPicker picker = new PointMatrixPicker(new PointMatrix(D[3], D[2], D[1], D[0]), _ic.Grid.CellGrid.GetLength(0), _ic.Grid.CellGrid.GetLength(1), pointMatrix);
            var b = picker.ShowDialog();

            if (b.HasValue && b.Value)
            {
                AdvancedMatrixAccepted = true;
                pointMatrix            = picker.Condition;
            }
        }
Ejemplo n.º 16
0
        internal PointMatrixPicker(PointMatrix matrix, int Size1, int Size2, PointMatrixPick condition = null)
        {
            _condition      = condition ?? PointMatrixPick.SingularMatrixCondition(matrix, Size1, Size2);
            BrushRectangles = SPDAssets.GetBrushRectangles((int)MatrixCount);
            if (Condition.Size != Size)
            {
                _condition = Condition.Resize(Size);
            }
            this.Size1 = Size1;
            this.Size2 = Size2;
            Size       = Math.Max(Size1, Size2);
            InitializeComponent();


            _selectedOperation = Operation.None;

            _comboBox.ItemsSource   = BrushRectangles;
            _comboBox.SelectedIndex = 0;

            DataContext     = this;
            _conditionNames = new List <Tuple <string, Tuple <string, bool> > >();
            _conditions     = new Dictionary <Tuple <string, bool>, Func <bool, int, int, bool, InitialConditions> >();
            foreach (var T in new[] { false, true })
            {
                _conditions.Add(new Tuple <string, bool>("Donut", T), InitialConditions.DonutFactory);
                _conditions.Add(new Tuple <string, bool>("Circle", T), InitialConditions.CircleFactory);
                _conditions.Add(new Tuple <string, bool>("Diagonal", T), InitialConditions.DiagonalFactory);
            }
            _conditionNames.AddRange(
                _conditions.Select(
                    k =>
                    new Tuple <string, Tuple <string, bool> >(k.Value(k.Key.Item2, 1, 10, false).Name,
                                                              new Tuple <string, bool>(k.Key.Item1, k.Key.Item2))));
            ComboBoxCopy.ItemsSource = _conditionNames.Select(s => s.Item1);
            _canvalidate             = true;
            Condition = Condition;
        }
Ejemplo n.º 17
0
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     ResetScale();
     Condition = PointMatrixPick.CreatePickFromIC(InitialConditions.GenerateRandom(Size, (int)MatrixCount), Condition);
     ComboBoxCopy.SelectedIndex = -1;
 }
Ejemplo n.º 18
0
        public PointMatrixPick Resize(int Size)
        {
            PointMatrixPick result = new PointMatrixPick
            {
                Matrices = Matrices.ToList(),
                Indices = new int[Size, Size]
            };

            double d = (double)Size / this.Size;
            for (int i = 0; i < Size; i++)
            {
                double index = i / d;
                int k = (int)Math.Floor(index);
                int l = k + 1;
                double prob = 1 - (index - k);
                int I = random.NextDouble() < prob ? k : l;
                for (int j = 0; j < Size; j++)
                {
                    index = i / d;
                    k = (int)Math.Floor(index);
                    l = k + 1;
                    prob = 1 - (index - k);
                    int J = random.NextDouble() < prob ? k : l;
                    result.Indices[i, j] = Indices[I, J];
                }
            }
            return result;
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Konstruktor okna realizującego symulacje
        /// </summary>
        /// <param name="PayValues">Tablica zawierająca informacje wyekstrahowane z macierzy wypłat </param>
        /// <param name="strategies">Tablica zawierająca początkowe strategie w automacie</param>
        /// <param name="torus">Zmienna informująca czy obliczenia automatu realizowane są na torusie</param>
        /// <param name="vonneumann">Zmienna informująca czy obliczenia automatu realizowane są z sąsiedztwem Von Neumanna</param>
        public SPDView(PointMatrixPick Matrix, int[,] strategies, int _neighboursCount, INeighbourhood _neighbourhood)
        {
            _tooltip            = -1;
            _strategyCount      = 2 + _neighboursCount;
            _sumPointsHistory   = new List <double[]>();
            _sumPoints          = new double[_strategyCount];
            DataContext         = this;
            pointMatrixPick     = Matrix;
            float[,] fakePoints = new float[strategies.GetLength(0), strategies.GetLength(1)];

            AddHistory(strategies, fakePoints);

            for (int i = 0; i < _sumPoints.Length; i++)
            {
                _sumPoints[i] = 0;
            }

            _strategies   = strategies;
            neighbourhood = _neighbourhood;
#if DEBUG
            var threadNum = 1; //debugging purposes DON'T remove
#else
            var threadNum = 16;
#endif
            _strategyDictionary = GenerateIntegerStrategies(_strategyCount);

            _spd =

                new SPD.Engine.SPD(
                    Matrix.Function,
                    neighbourhood, strategies,
                    _strategyDictionary, 10, threadNum, Matrix.ModifiedPointCounting ? OptimizationKind.Relative : OptimizationKind.Absolute);

            Speed             = 1;
            PointsModel       = new PlotModel();
            CountModel        = new PlotModel();
            ChangeModel       = new PlotModel();
            SumModel          = new PlotModel();
            PointsModel.Title = "Średnie wartości punktowe";
            CountModel.Title  = "Liczebność strategii";
            ChangeModel.Title = "Niestabilność układu";
            SumModel.Title    = "Punkty dla strategii zagregowane";
            _iterations       = new List <Tuple <int, string> >();

            PointsModel.Axes.Add(new CategoryAxis {
                ItemsSource = _iterations, LabelField = "Item2"
            });

            CountModel.Axes.Add(new CategoryAxis {
                ItemsSource = _iterations, LabelField = "Item2"
            });
            PointsModel.Axes.Add(new LinearAxis {
                MinimumPadding = 0, AbsoluteMinimum = 0
            });
            CountModel.Axes.Add(new LinearAxis {
                MinimumPadding = 0, AbsoluteMinimum = 0
            });

            PointsModel.Series.Add(new ColumnSeries {
                ColumnWidth = 10, IsStacked = true
            });
            CountModel.Series.Add(new ColumnSeries {
                ColumnWidth = 10, IsStacked = true
            });

            ChangeModel.Axes.Add(new CategoryAxis {
                ItemsSource = _iterations, LabelField = "Item2"
            });
            ChangeModel.Axes.Add(new LinearAxis {
                MinimumPadding = 0, AbsoluteMinimum = 0
            });
            ChangeModel.Series.Add(new ColumnSeries {
                ColumnWidth = 10, IsStacked = true
            });
            SumModel.Axes.Add(new CategoryAxis {
                ItemsSource = _iterations, LabelField = "Item2"
            });
            SumModel.Axes.Add(new LinearAxis {
                MinimumPadding = 0, AbsoluteMinimum = 0
            });
            SumModel.Series.Add(new ColumnSeries {
                ColumnWidth = 10, IsStacked = true
            });


            UpdateModels();

            InitializeComponent();
            Iteration = 0;

            var D = SPDAssets.GenerateLegend(Legenda.Height, _strategyCount);


            _width  = _strategies.GetLength(0);
            _height = _strategies.GetLength(1);
            var image2 = new Image
            {
                Source = GenerateImage(_spd, 0, 0, _strategies.GetLength(0), _strategies.GetLength(1))
            };


            Canvas.SetTop(D, 0);
            Canvas.SetLeft(D, 0);
            Legenda.Children.Add(D);
            Canvas.Children.Add(image2);
        }
Ejemplo n.º 20
0
        private DrawingImage GenerateImage(int x, int y, int width, int height)
        {
            var ic = PointMatrixPick.CreateICFromPick(Condition);

            return(ic.Grid.GenerateImage(x, y, width, height, Canvas.Width, Canvas.Height));
        }
Ejemplo n.º 21
0
 private void AdvancedMatrix_OnClick(object sender, RoutedEventArgs e)
 {
     AdvancedPointMatrix = true;
     var d = Validate();
     if (d == null)
     {
         MessageBox.Show("Podstawowa Macierz musi być poprawna przed przejściem do zaawansowanych ustawień");
     }
     var D = d.Select(x=>(float)x).ToArray();
     PointMatrixPicker picker = new PointMatrixPicker(new PointMatrix(D[3], D[2], D[1], D[0]), _ic.Grid.CellGrid.GetLength(0), _ic.Grid.CellGrid.GetLength(1), pointMatrix);
     var b = picker.ShowDialog();
     if (b.HasValue && b.Value)
     {
         AdvancedMatrixAccepted = true;
         pointMatrix = picker.Condition;
     }
 }
Ejemplo n.º 22
0
        /// <summary>
        /// Konstruktor okna realizującego symulacje
        /// </summary>
        /// <param name="PayValues">Tablica zawierająca informacje wyekstrahowane z macierzy wypłat </param>
        /// <param name="strategies">Tablica zawierająca początkowe strategie w automacie</param>
        /// <param name="torus">Zmienna informująca czy obliczenia automatu realizowane są na torusie</param>
        /// <param name="vonneumann">Zmienna informująca czy obliczenia automatu realizowane są z sąsiedztwem Von Neumanna</param>
        public SPDView(PointMatrixPick Matrix, int[,] strategies, int _neighboursCount, INeighbourhood _neighbourhood)
        {
            _tooltip = -1;
            _strategyCount = 2 + _neighboursCount;
            _sumPointsHistory = new List<double[]>();
            _sumPoints = new double[_strategyCount];
            DataContext = this;
            pointMatrixPick = Matrix;
            float[,] fakePoints = new float[strategies.GetLength(0), strategies.GetLength(1)];

            AddHistory(strategies,fakePoints);
            
            for (int i = 0; i < _sumPoints.Length; i++) _sumPoints[i] = 0;
            
            _strategies = strategies;
            neighbourhood = _neighbourhood;
#if DEBUG
            var threadNum = 1; //debugging purposes DON'T remove
#else
            var threadNum = 16;
#endif
            _strategyDictionary = GenerateIntegerStrategies(_strategyCount);
            
            _spd =

                new SPD.Engine.SPD(
                    Matrix.Function,
                    neighbourhood, strategies,
                  _strategyDictionary, 10, threadNum, Matrix.ModifiedPointCounting ? OptimizationKind.Relative : OptimizationKind.Absolute);

            Speed = 1;
            PointsModel = new PlotModel();
            CountModel = new PlotModel();
            ChangeModel = new PlotModel();
            SumModel = new PlotModel();
            PointsModel.Title = "Średnie wartości punktowe";
            CountModel.Title = "Liczebność strategii";
            ChangeModel.Title = "Niestabilność układu";
            SumModel.Title = "Punkty dla strategii zagregowane";
            _iterations = new List<Tuple<int, string>>();

            PointsModel.Axes.Add(new CategoryAxis { ItemsSource = _iterations, LabelField = "Item2" });

            CountModel.Axes.Add(new CategoryAxis { ItemsSource = _iterations, LabelField = "Item2" });
            PointsModel.Axes.Add(new LinearAxis { MinimumPadding = 0, AbsoluteMinimum = 0 });
            CountModel.Axes.Add(new LinearAxis { MinimumPadding = 0, AbsoluteMinimum = 0 });

            PointsModel.Series.Add(new ColumnSeries { ColumnWidth = 10, IsStacked = true });
            CountModel.Series.Add(new ColumnSeries { ColumnWidth = 10, IsStacked = true });

            ChangeModel.Axes.Add(new CategoryAxis { ItemsSource = _iterations, LabelField = "Item2" });
            ChangeModel.Axes.Add(new LinearAxis { MinimumPadding = 0, AbsoluteMinimum = 0 });
            ChangeModel.Series.Add(new ColumnSeries {ColumnWidth = 10, IsStacked = true});
                 SumModel.Axes.Add(new CategoryAxis { ItemsSource = _iterations, LabelField = "Item2" });
            SumModel.Axes.Add(new LinearAxis { MinimumPadding = 0, AbsoluteMinimum = 0 });
            SumModel.Series.Add(new ColumnSeries { ColumnWidth = 10, IsStacked = true });
       

            UpdateModels();

            InitializeComponent();
            Iteration = 0;

            var D =  SPDAssets.GenerateLegend(Legenda.Height, _strategyCount);

           
            _width = _strategies.GetLength(0);
            _height = _strategies.GetLength(1);
            var image2 = new Image
            {
                Source = GenerateImage(_spd, 0, 0, _strategies.GetLength(0), _strategies.GetLength(1))

            };


            Canvas.SetTop(D, 0);
            Canvas.SetLeft(D, 0);
            Legenda.Children.Add(D);
            Canvas.Children.Add(image2);
        }