-
Notifications
You must be signed in to change notification settings - Fork 0
/
acsg.test.js
138 lines (129 loc) · 3.89 KB
/
acsg.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-env jest */
const rewire = require('rewire')
const acsg = require('./acsg')
// Access private functions for unit tests
const acsgRewire = rewire('./acsg')
var outputData = ''
var storeLog = inputs => (outputData += inputs)
// eslint-disable-next-line no-console
console['log'] = jest.fn(storeLog)
// Random seed
beforeEach(() => {
outputData = ''
})
// Generate basic world
function init_game(config) {
var game_config = {SEED: 2, INCLUDE_HUMAN: true, IS_CLI: true}
Object.assign(game_config, config)
var game = acsg.Game({config: game_config})
return game
}
test('sum function', () => {
var sum = acsgRewire.__get__('sum')
expect(sum([1, 2, 3])).toEqual(6)
expect(sum([8, 9, 10])).toEqual(27)
expect(sum([])).toEqual(0)
})
test('softmax function', () => {
const softmax = acsgRewire.__get__('softmax')
expect(softmax([1, 2, 3])).toEqual([1/6, 1/3, 1/2])
expect(softmax([0, 0, 0])).toEqual([3, 3, 3])
expect(softmax([1, 2, 3], 2)).toEqual([1/14, 2/7, 9/14])
})
test('player move changes position', () => {
var game = init_game()
// Create a player
var player = game.world.players[0]
// initial position is random, but deterministic based on seed
expect(player.position).toEqual([9, 15])
// move() returns direction
expect(player.move('up')).toEqual('up')
expect(player.position).toEqual([8, 15])
player.move('down')
expect(player.position).toEqual([9, 15])
player.move('left')
expect(player.position).toEqual([9, 14])
player.move('right')
expect(player.position).toEqual([9, 15])
// Nonsense directions are logged, and do nothing
player.move('nonsense')
expect(outputData).toEqual('Direction not recognized.')
expect(player.position).toEqual([9, 15])
})
test('calculate straight payoff', () => {
var game = init_game({
NUM_PLAYERS: 2,
DOLLARS_PER_POINT: 0.05
})
var player = game.world.players[0]
// Payoff is a simple multiple of our score
player.score = 10
expect(player.payoff).toEqual(0)
game.computePayoffs()
expect(player.payoff).toEqual(0.5)
player.score = 20
game.computePayoffs()
expect(player.payoff).toEqual(1)
player.score = 0
game.computePayoffs()
expect(player.payoff).toEqual(0)
// Other player scores don't matter
player.score = 20
game.world.players[1].score = 50
game.computePayoffs()
expect(player.payoff).toEqual(1)
})
test('calculate intragroup payoff', () => {
var game = init_game({
NUM_PLAYERS: 2,
DOLLARS_PER_POINT: 0.05,
INTRAGROUP_COMPETITION: 2
})
var player = game.world.players[0]
var bot1 = game.world.players[1]
expect(player.teamIdx).toEqual(0)
expect(bot1.teamIdx).toEqual(0)
// We get all the payoff if we get all the points
player.score = 20
bot1.score = 0
game.computePayoffs()
expect(player.payoff).toEqual(1)
// We get a payoff based on our portion of the in-group total
bot1.score = 10
game.computePayoffs()
expect(player.payoff).toEqual(30 * 4/5 * 0.05)
})
test('calculate intergroup payoff', () => {
var game = init_game({
NUM_PLAYERS: 4,
DOLLARS_PER_POINT: 0.1,
INTERGROUP_COMPETITION: 2
})
var player = game.world.players[0]
var bot1 = game.world.players[1]
var bot2 = game.world.players[2]
var bot3 = game.world.players[3]
expect(player.teamIdx).toEqual(0)
expect(bot1.teamIdx).toEqual(0)
expect(bot2.teamIdx).toEqual(1)
expect(bot3.teamIdx).toEqual(1)
// Point distribution between groups determines payoff
player.score = 20
bot1.score = 0
bot2.score = 20
bot3.score = 40
game.computePayoffs()
expect(player.payoff).toEqual(80 * 1/10 * 0.1)
// Point distribution within our group changes the payoff
player.score = 10
bot1.score = 10
game.computePayoffs()
expect(player.payoff).toEqual(80 * 1/10 * 0.1 * 0.5)
// Point distribution in the out-group has no impact
player.score = 20
bot1.score = 0
bot2.score = 10
bot3.score = 50
game.computePayoffs()
expect(player.payoff).toEqual(80 * 1/10 * 0.1)
})