// Apply the changes and quit
        private void btnOk_Click(object sender, EventArgs e)
        {
            // Store the contents of the textboxes as integers
            int t = Convert.ToInt32(rtbTemptation.Text);
            int r = Convert.ToInt32(rtbReward.Text);
            int p = Convert.ToInt32(rtbPunishment.Text);
            int s = Convert.ToInt32(rtbSucker.Text);

            // Check for matrix validity
            // T < R < P < S
            if (!(PayoffMatrix.isValid(t, r, p, s)))
            {
                // If it is not valid we abort and tell the user
                MessageBox.Show(
                    "The selected matrix is not valid."
                    + Environment.NewLine
                    + "Rules :"
                    + Environment.NewLine
                    + "[Temptation < Reward < Punishment < Sucker]"
                    + Environment.NewLine
                    + "[2 * Reward < Temptation + Sucker]"
                    );
            }
            else
            {
                // Else, we apply the changes
                currentMatrix.Reward     = r;
                currentMatrix.Sucker     = s;
                currentMatrix.Temptation = t;
                currentMatrix.Punishment = p;

                // Close the form
                this.Close();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Designated constructor
        /// </summary>
        /// <param name="x">X coordinate of the cell on the grid</param>
        /// <param name="y">Y coordinate of the cell on the grid</param>
        /// <param name="strategy">Current strategy of the cell</param>
        /// <param name="matrix">Payoff matrix used to determine the score of each cell</param>
        public Cell(int x, int y, Strategy strategy, PayoffMatrix matrix)
        {
            this.X            = x;
            this.Y            = y;
            this.Strategy     = strategy;
            this.PayoffMatrix = matrix;
            this.Score        = 0;

            this.Neighbors = new List <Cell>();
            this.History   = new Stack <Move>();

            // Get the color of the cell from the current strategy
            this.setColorFromStrategy();

            // Starts with a move relevent to the strategy
            this.chooseNextMove();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Plays a game of the prisoners dilemma with the cell's neighbors using the cell's current strategy
        /// </summary>
        public void step()
        {
            // Go and play with each of our neighbors
            List <int> scores = new List <int>();

            foreach (Cell neighbor in this.Neighbors)
            {
                // Play a game and store the result
                scores.Add(PayoffMatrix.returnPayoff(this.Choice, neighbor.Choice));
            }

            // We get the best score of the cell
            this.Score = scores.Min();

            // Update the color of the cell
            this.setColorFromMove();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Designated constructor
        /// </summary>
        /// <param name="width">The width of the grid in pixels</param>
        /// <param name="height">The height of the grid in pixels</param>
        /// <param name="nbCols">The number of columns of the grid</param>
        /// <param name="nbLines">The number of lines of the grid</param>
        public Grid(int width, int height, int nbLines, int nbCols, PayoffMatrix matrix, WrapMode wrapmode, Strategy strategy)
        {
            this.Width        = width;
            this.Height       = height;
            this.NbLines      = nbLines;
            this.NbCols       = nbCols;
            this.PayoffMatrix = matrix;
            this.ColorMode    = ColorMode.Strategy;
            this.WrapMode     = wrapmode;

            // Initialize our list of cells
            this.Cells = new Cell[nbLines, nbCols];

            // Calculate the width and the height of a cell
            int cellWidth  = this.Width / nbCols;
            int cellHeight = this.Height / nbLines;

            // Go through each possible slot in the grid
            for (int y = 0; y < this.NbLines; y++)
            {
                for (int x = 0; x < this.NbCols; x++)
                {
                    // Create a temporary cell with the default strategy
                    Cell tmpCell = new Cell(x, y, strategy, this.PayoffMatrix);

                    // Set the cell's height according to the grid's need
                    tmpCell.Width  = cellWidth;
                    tmpCell.Height = cellHeight;

                    // Add the cell to the list
                    this.Cells[y, x] = tmpCell;
                }
            }

            foreach (Cell cell in this.Cells)
            {
                // Make each cell aware of its neighbors
                cell.Neighbors = findCellNeighbors(cell);
            }
        }
Exemplo n.º 5
0
 /// Overloaded function that allows isValid to be used on the current object
 /// <summary>
 /// Checks the validity of the matrix according to the rules :
 /// T better than R better than P better than S
 /// </summary>
 /// <returns>True if the matrix is valid, false if it is not</returns>
 public bool isValid()
 {
     return(PayoffMatrix.isValid(this.Temptation, this.Reward, this.Punishment, this.Sucker));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Conveniance constructor
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 public Cell(int x, int y, PayoffMatrix matrix)
     : this(x, y, DEFAULT_STRATEGY, matrix)
 {
     // No code
 }
Exemplo n.º 7
0
        /// <summary>
        /// Set up the form after it is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainView_Load(object sender, EventArgs e)
        {
            // Make a list of all our available strategies
            availableStrategies = new List <Strategy>();

            // To add more strategies, add them to the list
            availableStrategies.Add(new StratRandom());
            availableStrategies.Add(new StratTitForTat());
            availableStrategies.Add(new StratBlinker());
            availableStrategies.Add(new StratAlwaysCooperate());
            availableStrategies.Add(new StratAlwaysDefect());
            availableStrategies.Add(new StratTitForTwoTats());
            availableStrategies.Add(new StratGrimTrigger());
            availableStrategies.Add(new StratFortress());
            availableStrategies.Add(new StratAdaptativePavlov());
            availableStrategies.Add(new StratSuspiciousTitForTat());

            // Sort the list
            availableStrategies.Sort();

            // Initialize the payoff matrix with default values
            payoffMatrix = new PayoffMatrix();

            // Initialize our grid of cells
            mainGrid = new Grid(pbGrid.Width, pbGrid.Height, tbLines.Value, tbColumns.Value, payoffMatrix, gridWrappingMode);

            // Initialise the combobox with strategies and colors
            cbStrategies.AddStrategies(availableStrategies);

            // Select the first element by default
            cbStrategies.SelectedIndex = 0;

            // Set the user help text
            lblUserHelp.Text = "Click on a cell to change its strategy."
                               + Environment.NewLine + "The default strategy is "
                               + Cell.DEFAULT_STRATEGY.ToString();

            // Update the other labels
            updateLabels();

            // CHARTS
            // Pie chart
            pieStrategy.InnerRadius       = 50;
            pieStrategy.LegendLocation    = LegendLocation.Right;
            pieStrategy.DisableAnimations = true;
            pieStrategy.Series            = new SeriesCollection();

            foreach (Strategy strategy in availableStrategies)
            {
                // Get the color from the strategy
                System.Windows.Media.BrushConverter converter = new System.Windows.Media.BrushConverter();
                System.Windows.Media.Brush          brush     = (System.Windows.Media.Brush)converter.ConvertFromString(strategy.getColor().ToHex());

                // Create an object for storing values on the pie chart
                PieSeries stratToAdd = new PieSeries
                {
                    Title  = strategy.ToString(),
                    Values = new ChartValues <double>
                    {
                        mainGrid.findCountOfStrategy(strategy)
                    },
                    DataLabels = true,
                    Fill       = brush
                };

                stratToAdd.Visibility = System.Windows.Visibility.Hidden;


                // Add the values to the pie chart
                pieStrategy.Series.Add(stratToAdd);
            }

            // Cartesian
            cartesianStrategy.LegendLocation = LegendLocation.Right;
            cartesianStrategy.AxisX.Add(new Axis
            {
                Title          = "Current Generation",
                LabelFormatter = value => value.ToString()
            });

            cartesianStrategy.AxisY.Add(new Axis
            {
                Title          = "Number of Days in Prison",
                LabelFormatter = value => value.ToString(),
            });

            // Initialize the cartesian chart
            initializeChart();
            updateDonutChart();
        }
Exemplo n.º 8
0
 /// <summary>
 /// Conveniance constructor 2
 /// </summary>
 public Grid(int width, int height, int nbLines, int nbCols, PayoffMatrix matrix)
     : this(width, height, nbLines, nbCols, matrix, DEFAULT_WRAP_MODE, Cell.DEFAULT_STRATEGY)
 {
     // No code
 }
Exemplo n.º 9
0
 /// <summary>
 /// Conveniance constructor
 /// </summary>
 public Grid(int width, int height, int nbLines, int nbCols, PayoffMatrix matrix, WrapMode wrapmode)
     : this(width, height, nbLines, nbCols, matrix, wrapmode, Cell.DEFAULT_STRATEGY)
 {
     // No code
 }