Skip to content

Commit

Permalink
implement extended payoff matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cardinot committed Mar 20, 2023
1 parent b0e3563 commit 43b1833
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 - Marcos Cardinot <[email protected]>
Copyright (c) 2023 - Marcos Cardinot <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Binary file removed example.jpg
Binary file not shown.
13 changes: 8 additions & 5 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"type": "model",
"uid": "prisonersDilemma",
"uid": "prisonersDilemmaExtended",
"version": 1,
"title": "Prisoner's Dilemma Game",
"title": "Extended Prisoner's Dilemma Game",
"author": "Marcos Cardinot",
"description": "It implements the experiment proposed by Nowak, M. A., & May, R. M. (1992). Evolutionary games and spatial chaos. Nature, 359(6398), 826. DOI: http://dx.doi.org/10.1038/359826a0",
"description": "It implements a generalized version of the experiment proposed by Nowak, M. A., & May, R. M. (1992). Evolutionary games and spatial chaos. Nature, 359(6398), 826. DOI: http://dx.doi.org/10.1038/359826a0",
"supportedGraphs": ["squareGrid","edgesFromFile"],
"pluginAttributesScope": [
{"temptation": "double[1,2]"}
{"cc": "double[-1000,1000]"},
{"cd": "double[-1000,1000]"},
{"dc": "double[-1000,1000]"},
{"dd": "double[-1000,1000]"}
],
"nodeAttributesScope": [
{"strategy": "int{0,1,2,3}"},
{"score": "double[0,16]"}
{"score": "double[-100000,100000]"}
]
}
22 changes: 15 additions & 7 deletions plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 - Marcos Cardinot <[email protected]>
* Copyright (c) 2023 - Marcos Cardinot <[email protected]>
*
* This source code is licensed under the MIT license found in
* the LICENSE file in the root directory of this source tree.
Expand All @@ -11,8 +11,16 @@ namespace evoplex {

bool PDGame::init()
{
m_temptation = attr("temptation", -1.0).toDouble();
return m_temptation >=1.0 && m_temptation <= 2.0;
const auto read = [this](auto& var, const auto& strategy)
{
var = attr(strategy, -999999.).toDouble();
if (var < -1000. || var > 1000.) {
qDebug() << "Invalid payoff! " << strategy << var;
return false;
}
return true;
};
return read(m_cc, "cc") && read(m_cd, "cd") && read(m_dc, "dc") && read(m_dd, "dd");
}

bool PDGame::algorithmStep()
Expand Down Expand Up @@ -62,10 +70,10 @@ bool PDGame::algorithmStep()
double PDGame::playGame(const int sX, const int sY) const
{
switch (binarize(sX) * 2 + binarize(sY)) {
case 0: return 1.0; // CC : Reward for mutual cooperation
case 1: return 0.0; // CD : Sucker's payoff
case 2: return m_temptation; // DC : Temptation to defect
case 3: return 0.0; // DD : Punishment for mutual defection
case 0: return m_cc; // CC : Reward for mutual cooperation
case 1: return m_cd; // CD : Sucker's payoff
case 2: return m_dc; // DC : Temptation to defect
case 3: return m_dd; // DD : Punishment for mutual defection
default: qFatal("Error! strategy should be 0 or 1!");
}
}
Expand Down
7 changes: 5 additions & 2 deletions plugin.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 - Marcos Cardinot <[email protected]>
* Copyright (c) 2023 - Marcos Cardinot <[email protected]>
*
* This source code is licensed under the MIT license found in
* the LICENSE file in the root directory of this source tree.
Expand All @@ -20,7 +20,10 @@ class PDGame: public AbstractModel
private:
enum NodeAttr { STRATEGY, SCORE };

double m_temptation;
double m_cc;
double m_cd;
double m_dc;
double m_dd;

double playGame(const int sX, const int sY) const;
int binarize(const int strategy) const;
Expand Down

0 comments on commit 43b1833

Please sign in to comment.