我为RandomGame类编写了一个构造函数,该类继承自另一个类SGGame。但是,构造函数无法正常运行。
假设创建一个随机游戏,它具有随机的收益和给定玩家数量,动作和状态的整数输入的过渡概率。现在,无论输入什么,它总是在创建一个有两个玩家,一个状态和一个动作的游戏。
这是构造函数:
class RandomGame : public SGGame
{
private:
int numactions;
public:
RandomGame(int numPlayers,
int numStates,
int numactions)
{
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::uniform_real_distribution<double> distribution(1e-6,1-(1e-6));
double delta = distribution(generator);
vector<bool> unconstrained(numPlayers,false);
vector< vector< int > > numActions(numStates,vector<int>(numPlayers,numactions));
vector<int> numActions_total(numStates,pow(numactions, numPlayers));
// Payoffs
vector< vector< vector<double> > >
payoffs(numStates,vector< vector<double> >(pow(numactions, numPla
yers),vector<double>(numPlayers,0.0)));
unsigned int i,j,k,l;
for(i=0;i<numStates;i++)
{
for(j=0;j<numActions_total[i];j++)
{
for(k=0;k<numPlayers;k++)
{
std::uniform_real_distribution<double> distribution(0,10);
payoffs[i][j][k] = distribution(generator);
}
}
}
// Transition probabilities
vector < vector< vector<double> > >
probabilities(numStates,vector< vector<double> >(pow(numactions,
numPlayers), vector<double>(numStates,1.0))); // 1.0 as initial value, but then ran
domize
// randomize transition probabilities
double prob_sum;
for(i=0;i<numStates;i++)
{
for(j=0;j<numActions_total[i];j++)
{
prob_sum = 0.0;
for(k=0;k<numStates;k++)
{
std::uniform_real_distribution<double> distribution(0,1.0);
probabilities[i][j][k] = distribution(generator);
// normalize probabilities
prob_sum += probabilities[i][j][k];
}
for(k=0;k<numStates;k++)
{
probabilities[i][j][k] = probabilities[i][j][k]/prob_sum;
}
}
}
SGGame game(delta,
numStates,
numActions,
payoffs,
probabilities,
unconstrained);
}
}; ```
// decleration of the class SGGame: the constructor I call at the bottom of that code uses an SGGame constructor which creates an SGGame with the given inputs.
``` ```
class SGGame
{
private:
double delta; /*!< The discount factor. */
int numPlayers; /*!< The number of players. */
int numStates; /*!< The number of states, must be at least 1. */
vector< vector<int> > numActions; /*!< Gives the number of each
player's actions in each
state. In particular, player i
has numActions[s][i] actions in
state s. Should note that a
pair (a1,a2) is mapped into an
action profile using the
formula
a=a1+a2*numActions[s][a1], and
generalized to n>2. */
vector<int> numActions_total; /*!< Total number of action profiles
for each state. */
vector< vector<SGPoint> > payoffs; /*!< Gives the payoffs of the
players as a function of the
action profile. In particular,
payoffs[s][a][i] are player
i's payoffs in state s when
action profile a is played. */
vector< vector< vector<double> > > probabilities; /*!< State
transition
probabilities:
probabilities[s][a][s']
is the
probability of
transitioning
to state s'
when action
profile a is
played in state
s. */
vector< vector<bool> > eqActions; /*!< Indicates which action profiles
are allowed to be played on path in
each state. By default, initialized
to true for all action
profiles. Allows one to, for
example, look at strongly symmetric
equilibria (by first excluding
asymmetric action profiles from the
lists). Players can always deviate
to action profiles which are not
allowed on path. */
vector<bool> unconstrained; /*!< If unconstrained[i]=true, the
algorithm will not impose incentive
compatibility as a constraint for
player i. */ ```
// call to the constructor which is not working properly:
``` ```
RandomGame game(2,3,4); ```
答案 0 :(得分:1)
在构造函数的末尾,您创建了SGGame
对象,但实际上并未对其执行任何操作。它只是被丢弃。
由于SGGame
是RandomGame
的基类,因此应将构造函数内部的逻辑移入其自己的函数中,然后可以将其传递给SGGame
的构造函数。
我们将此函数称为generateRandomGame
:
class RandomGame : public SGGame {
private:
int numactions;
public:
RandomGame(int numPlayers, int numStates, int numactions)
: SGGame(generateRandomGame(numPlayers, numStates, numactions))
, numactions(numactions) {}
// Other stuff...
};
根据原始构造函数中的代码,generateRandomGame
应该看起来像这样:
SGGame generateRandomGame(int numPlayers, int numStates, int numactions) {
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_real_distribution<double> distribution(1e-6, 1 - (1e-6));
double delta = distribution(generator);
vector<bool> unconstrained(numPlayers, false);
vector<vector<int>> numActions(numStates,
vector<int>(numPlayers, numactions));
vector<int> numActions_total(numStates, pow(numactions, numPlayers));
// Payoffs
vector<vector<vector<double>>> payoffs(
numStates, vector<vector<double>>(pow(numactions, numPlayers),
vector<double>(numPlayers, 0.0)));
unsigned int i, j, k, l;
for (i = 0; i < numStates; i++) {
for (j = 0; j < numActions_total[i]; j++) {
for (k = 0; k < numPlayers; k++) {
std::uniform_real_distribution<double> distribution(0, 10);
payoffs[i][j][k] = distribution(generator);
}
}
}
// Transition probabilities
vector<vector<vector<double>>> probabilities(
numStates,
vector<vector<double>>(
pow(numactions, numPlayers),
vector<double>(numStates,
1.0))); // 1.0 as initial value, but then randomize
// randomize transition probabilities
double prob_sum;
for (i = 0; i < numStates; i++) {
for (j = 0; j < numActions_total[i]; j++) {
prob_sum = 0.0;
for (k = 0; k < numStates; k++) {
std::uniform_real_distribution<double> distribution(0, 1.0);
probabilities[i][j][k] = distribution(generator);
// normalize probabilities
prob_sum += probabilities[i][j][k];
}
for (k = 0; k < numStates; k++) {
probabilities[i][j][k] = probabilities[i][j][k] / prob_sum;
}
}
}
return SGGame(delta, numStates, numActions, payoffs, probabilities,
unconstrained);
}