Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

""" 

Co-operative Games With Finite Players 

 

This module implements a class for a characteristic function cooperative 

game. Methods to calculate the Shapley value (a fair way of sharing 

common resources: see [CEW2011]_) as well as test properties of the game 

(monotonicity, superadditivity) are also included. 

 

AUTHORS: 

 

- James Campbell and Vince Knight (06-2014): Original version 

""" 

 

#***************************************************************************** 

# Copyright (C) 2014 James Campbell james.campbell@tanti.org.uk 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from itertools import permutations, combinations 

from sage.misc.misc import powerset 

from sage.rings.integer import Integer 

from sage.structure.sage_object import SageObject 

 

 

class CooperativeGame(SageObject): 

r""" 

An object representing a co-operative game. Primarily used to compute the 

Shapley value, but can also provide other information. 

 

INPUT: 

 

- ``characteristic_function`` -- a dictionary containing all possible 

sets of players: 

 

* key - each set must be entered as a tuple. 

* value - a real number representing each set of players contribution 

 

EXAMPLES: 

 

The type of game that is currently implemented is referred to as a 

Characteristic function game. This is a game on a set of players 

`\Omega` that is defined by a value function `v : C \to \RR` where 

`C = 2^{\Omega}` is the set of all coalitions of players. 

Let `N := |\Omega|`. 

An example of such a game is shown below: 

 

.. MATH:: 

 

v(c) = \begin{cases} 

0 &\text{if } c = \emptyset, \\ 

6 &\text{if } c = \{1\}, \\ 

12 &\text{if } c = \{2\}, \\ 

42 &\text{if } c = \{3\}, \\ 

12 &\text{if } c = \{1,2\}, \\ 

42 &\text{if } c = \{1,3\}, \\ 

42 &\text{if } c = \{2,3\}, \\ 

42 &\text{if } c = \{1,2,3\}. \\ 

\end{cases} 

 

The function `v` can be thought of as a record of contribution of 

individuals and coalitions of individuals. Of interest, becomes how to 

fairly share the value of the grand coalition (`\Omega`)? This class 

allows for such an answer to be formulated by calculating the Shapley 

value of the game. 

 

Basic examples of how to implement a co-operative game. These functions 

will be used repeatedly in other examples. :: 

 

sage: integer_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: integer_game = CooperativeGame(integer_function) 

 

We can also use strings instead of numbers. :: 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

 

Please note that keys should be tuples. ``'1, 2, 3'`` is not a valid key, 

neither is ``123``. The correct input would be ``(1, 2, 3)``. Similarly, 

for coalitions containing a single element the bracket notation (which 

tells Sage that it is a tuple) must be used. So ``(1)``, ``(1,)`` are 

correct however simply inputting `1` is not. 

 

Characteristic function games can be of various types. 

 

A characteristic function game `G = (N, v)` is monotone if it satisfies 

`v(C_2) \geq v(C_1)` for all `C_1 \subseteq C_2`. A characteristic 

function game `G = (N, v)` is superadditive if it satisfies 

`v(C_1 \cup C_2) \geq v(C_1) + v(C_2)` for all `C_1, C_2 \subseteq 2^{\Omega}` such 

that `C_1 \cap C_2 = \emptyset`. 

 

We can test if a game is monotonic or superadditive. :: 

 

sage: letter_game.is_monotone() 

True 

sage: letter_game.is_superadditive() 

False 

 

Instances have a basic representation that will display basic information 

about the game:: 

 

sage: letter_game 

A 3 player co-operative game 

 

It can be shown that the "fair" payoff vector, referred to as the 

Shapley value is given by the following formula: 

 

.. MATH:: 

 

\phi_i(G) = \frac{1}{N!} \sum_{\pi\in\Pi_n} \Delta_{\pi}^G(i), 

 

where the summation is over the permutations of the players and the 

marginal contributions of a player for a given permutation is given as: 

 

.. MATH:: 

 

\Delta_{\pi}^G(i) = v\bigl( S_{\pi}(i) \cup \{i\} \bigr) 

- v\bigl( S_{\pi}(i) \bigr) 

 

where `S_{\pi}(i)` is the set of predecessors of `i` in `\pi`, i.e. 

`S_{\pi}(i) = \{ j \mid \pi(i) > \pi(j) \}` (or the number of inversions 

of the form `(i, j)`). 

 

This payoff vector is "fair" in that it has a collection of properties 

referred to as: efficiency, symmetry, additivity and Null player. 

Some of these properties are considered in this documentation (and tests 

are implemented in the class) but for a good overview see [CEW2011]_. 

 

Note ([MSZ2013]_) that an equivalent formula for the Shapley value is given by: 

 

.. MATH:: 

 

\phi_i(G) = \sum_{S \subseteq \Omega} \sum_{p \in S} 

\frac{(|S|-1)!(N-|S|)!}{N!} \bigl( v(S) - v(S \setminus \{p\}) \bigr) 

= \sum_{S \subseteq \Omega} \sum_{p \in S} 

\frac{1}{|S|\binom{N}{|S|}} \bigl( v(S) - v(S \setminus \{p\}) \bigr). 

 

This later formulation is implemented in Sage and 

requires `2^N-1` calculations instead of `N!`. 

 

To compute the Shapley value in Sage is simple:: 

 

sage: letter_game.shapley_value() 

{'A': 2, 'B': 5, 'C': 35} 

 

The following example implements a (trivial) 10 player characteristic 

function game with `v(c) = |c|` for all `c \in 2^{\Omega}`. 

 

:: 

 

sage: def simple_characteristic_function(N): 

....: return {tuple(coalition) : len(coalition) 

....: for coalition in subsets(range(N))} 

sage: g = CooperativeGame(simple_characteristic_function(10)) 

sage: g.shapley_value() 

{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1} 

 

For very large games it might be worth taking advantage of the particular 

problem structure to calculate the Shapley value and there are also 

various approximation approaches to obtaining the Shapley value of a game 

(see [SWJ2008]_ for one such example). Implementing these would be a 

worthwhile development For more information about the computational 

complexity of calculating the Shapley value see [XP1994]_. 

 

We can test 3 basic properties of any payoff vector `\lambda`. 

The Shapley value (described above) is known to be the unique 

payoff vector that satisfies these and 1 other property 

not implemented here (additivity). They are: 

 

* Efficiency - `\sum_{i=1}^N \lambda_i = v(\Omega)` 

In other words, no value of the total coalition is lost. 

 

* The nullplayer property - If there exists an `i` such that 

`v(C \cup i) = v(C)` for all `C \in 2^{\Omega}` then, `\lambda_i = 0`. 

In other words: if a player does not contribute to any coalition then 

that player should receive no payoff. 

 

* Symmetry property - If `v(C \cup i) = v(C \cup j)` for all 

`C \in 2^{\Omega} \setminus \{i,j\}`, then `x_i = x_j`. 

If players contribute symmetrically then they should get the same 

payoff:: 

 

sage: payoff_vector = letter_game.shapley_value() 

sage: letter_game.is_efficient(payoff_vector) 

True 

sage: letter_game.nullplayer(payoff_vector) 

True 

sage: letter_game.is_symmetric(payoff_vector) 

True 

 

Any payoff vector can be passed to the game and these properties 

can once again be tested:: 

 

sage: payoff_vector = {'A': 0, 'C': 35, 'B': 3} 

sage: letter_game.is_efficient(payoff_vector) 

False 

sage: letter_game.nullplayer(payoff_vector) 

True 

sage: letter_game.is_symmetric(payoff_vector) 

True 

 

TESTS: 

 

Check that the order within a key does not affect other functions:: 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('C', 'A',): 42, 

....: ('B', 'C',): 42, 

....: ('B', 'A', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: letter_game.shapley_value() 

{'A': 2, 'B': 5, 'C': 35} 

sage: letter_game.is_monotone() 

True 

sage: letter_game.is_superadditive() 

False 

sage: letter_game.is_efficient({'A': 2, 'C': 35, 'B': 5}) 

True 

sage: letter_game.nullplayer({'A': 2, 'C': 35, 'B': 5}) 

True 

sage: letter_game.is_symmetric({'A': 2, 'C': 35, 'B': 5}) 

True 

 

Any payoff vector can be passed to the game and these properties can once 

again be tested. :: 

 

sage: letter_game.is_efficient({'A': 0, 'C': 35, 'B': 3}) 

False 

sage: letter_game.nullplayer({'A': 0, 'C': 35, 'B': 3}) 

True 

sage: letter_game.is_symmetric({'A': 0, 'C': 35, 'B': 3}) 

True 

""" 

def __init__(self, characteristic_function): 

r""" 

Initializes a co-operative game and checks the inputs. 

 

TESTS: 

 

An attempt to construct a game from an integer:: 

 

sage: int_game = CooperativeGame(4) 

Traceback (most recent call last): 

... 

TypeError: characteristic function must be a dictionary 

 

This test checks that an incorrectly entered singularly tuple will be 

changed into a tuple. In this case ``(1)`` becomes ``(1,)``:: 

 

sage: tuple_function = {(): 0, 

....: (1): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: tuple_game = CooperativeGame(tuple_function) 

 

This test checks that if a key is not a tuple an error is raised:: 

 

sage: error_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: 12: 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: error_game = CooperativeGame(error_function) 

Traceback (most recent call last): 

... 

TypeError: key must be a tuple 

 

A test to ensure that the characteristic function is the power 

set of the grand coalition (ie all possible sub-coalitions):: 

 

sage: incorrect_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2, 3,): 42} 

sage: incorrect_game = CooperativeGame(incorrect_function) 

Traceback (most recent call last): 

... 

ValueError: characteristic function must be the power set 

""" 

if type(characteristic_function) is not dict: 

raise TypeError("characteristic function must be a dictionary") 

 

self.ch_f = characteristic_function 

for key in self.ch_f: 

if len(str(key)) == 1 and type(key) is not tuple: 

self.ch_f[(key,)] = self.ch_f.pop(key) 

elif type(key) is not tuple: 

raise TypeError("key must be a tuple") 

for key in self.ch_f: 

sortedkey = tuple(sorted(list(key))) 

self.ch_f[sortedkey] = self.ch_f.pop(key) 

 

self.player_list = max(characteristic_function.keys(), key=lambda key: len(key)) 

for coalition in powerset(self.player_list): 

if tuple(sorted(list(coalition))) not in sorted(self.ch_f.keys()): 

raise ValueError("characteristic function must be the power set") 

 

self.number_players = len(self.player_list) 

 

def shapley_value(self): 

r""" 

Return the Shapley value for ``self``. 

 

The Shapley value is the "fair" payoff vector and 

is computed by the following formula: 

 

.. MATH:: 

 

\phi_i(G) = \sum_{S \subseteq \Omega} \sum_{p \in S} 

\frac{1}{|S|\binom{N}{|S|}} 

\bigl( v(S) - v(S \setminus \{p\}) \bigr). 

 

EXAMPLES: 

 

A typical example of computing the Shapley value:: 

 

sage: integer_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: integer_game = CooperativeGame(integer_function) 

sage: integer_game.player_list 

(1, 2, 3) 

sage: integer_game.shapley_value() 

{1: 2, 2: 5, 3: 35} 

 

A longer example of the Shapley value:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 0, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 65} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.shapley_value() 

{1: 70/3, 2: 10, 3: 25/3, 4: 70/3} 

""" 

payoff_vector = {} 

n = Integer(len(self.player_list)) 

for player in self.player_list: 

weighted_contribution = 0 

for coalition in powerset(self.player_list): 

if coalition: # If non-empty 

k = Integer(len(coalition)) 

weight = 1 / (n.binomial(k) * k) 

t = tuple(p for p in coalition if p != player) 

weighted_contribution += weight * (self.ch_f[tuple(coalition)] 

- self.ch_f[t]) 

payoff_vector[player] = weighted_contribution 

 

return payoff_vector 

 

def is_monotone(self): 

r""" 

Return ``True`` if ``self`` is monotonic. 

 

A game `G = (N, v)` is monotonic if it satisfies 

`v(C_2) \geq v(C_1)` for all `C_1 \subseteq C_2`. 

 

EXAMPLES: 

 

A simple game that is monotone:: 

 

sage: integer_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: integer_game = CooperativeGame(integer_function) 

sage: integer_game.is_monotone() 

True 

 

An example when the game is not monotone:: 

 

sage: integer_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 10, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: integer_game = CooperativeGame(integer_function) 

sage: integer_game.is_monotone() 

False 

 

An example on a longer game:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 0, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 65} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.is_monotone() 

True 

""" 

return not any([set(p1) <= set(p2) and self.ch_f[p1] > self.ch_f[p2] 

for p1, p2 in permutations(self.ch_f.keys(), 2)]) 

 

def is_superadditive(self): 

r""" 

Return ``True`` if ``self`` is superadditive. 

 

A characteristic function game `G = (N, v)` is superadditive 

if it satisfies `v(C_1 \cup C_2) \geq v(C_1) + v(C_2)` for 

all `C_1, C_2 \subseteq 2^{\Omega}` such that `C_1 \cap C_2 

= \emptyset`. 

 

EXAMPLES: 

 

An example that is not superadditive:: 

 

sage: integer_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: integer_game = CooperativeGame(integer_function) 

sage: integer_game.is_superadditive() 

False 

 

An example that is superadditive:: 

 

sage: A_function = {(): 0, 

....: (1,): 6, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 18, 

....: (1, 3,): 48, 

....: (2, 3,): 55, 

....: (1, 2, 3,): 80} 

sage: A_game = CooperativeGame(A_function) 

sage: A_game.is_superadditive() 

True 

 

An example with a longer game that is superadditive:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 0, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 65} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.is_superadditive() 

True 

 

An example with a longer game that is not:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 55, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 85} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.is_superadditive() 

False 

""" 

sets = self.ch_f.keys() 

for p1, p2 in combinations(sets, 2): 

if not (set(p1) & set(p2)): 

union = tuple(sorted(set(p1) | set(p2))) 

if self.ch_f[union] < self.ch_f[p1] + self.ch_f[p2]: 

return False 

return True 

 

def _repr_(self): 

r""" 

Return a concise description of ``self``. 

 

EXAMPLES:: 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: letter_game 

A 3 player co-operative game 

""" 

return "A {} player co-operative game".format(self.number_players) 

 

def _latex_(self): 

r""" 

Return the LaTeX code representing the characteristic function. 

 

EXAMPLES:: 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: latex(letter_game) 

v(c) = \begin{cases} 

0, & \text{if } c = \emptyset \\ 

6, & \text{if } c = \{A\} \\ 

42, & \text{if } c = \{C\} \\ 

12, & \text{if } c = \{B\} \\ 

42, & \text{if } c = \{B, C\} \\ 

12, & \text{if } c = \{A, B\} \\ 

42, & \text{if } c = \{A, C\} \\ 

42, & \text{if } c = \{A, B, C\} \\ 

\end{cases} 

""" 

cf = self.ch_f 

output = "v(c) = \\begin{cases}\n" 

for key in sorted(cf.keys(), key=lambda key: len(key)): 

if not key: # == () 

coalition = "\\emptyset" 

else: 

coalition = "\\{" + ", ".join(str(player) for player in key) + "\\}" 

output += "{}, & \\text{{if }} c = {} \\\\\n".format(cf[key], coalition) 

output += "\\end{cases}" 

return output 

 

def is_efficient(self, payoff_vector): 

r""" 

Return ``True`` if ``payoff_vector`` is efficient. 

 

A payoff vector `v` is efficient if 

`\sum_{i=1}^N \lambda_i = v(\Omega)`; 

in other words, no value of the total coalition is lost. 

 

INPUT: 

 

- ``payoff_vector`` -- a dictionary where the key is the player 

and the value is their payoff 

 

EXAMPLES: 

 

An efficient payoff vector:: 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: letter_game.is_efficient({'A': 14, 'B': 14, 'C': 14}) 

True 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: letter_game.is_efficient({'A': 10, 'B': 14, 'C': 14}) 

False 

 

A longer example:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 0, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 65} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.is_efficient({1: 20, 2: 20, 3: 5, 4: 20}) 

True 

""" 

pl = tuple(sorted(list(self.player_list))) 

return sum(payoff_vector.values()) == self.ch_f[pl] 

 

def nullplayer(self, payoff_vector): 

r""" 

Return ``True`` if ``payoff_vector`` possesses the nullplayer 

property. 

 

A payoff vector `v` has the nullplayer property if there exists 

an `i` such that `v(C \cup i) = v(C)` for all `C \in 2^{\Omega}` 

then, `\lambda_i = 0`. In other words: if a player does not 

contribute to any coalition then that player should receive no payoff. 

 

INPUT: 

 

- ``payoff_vector`` -- a dictionary where the key is the player 

and the value is their payoff 

 

EXAMPLES: 

 

A payoff vector that returns ``True``:: 

 

sage: letter_function = {(): 0, 

....: ('A',): 0, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: letter_game.nullplayer({'A': 0, 'B': 14, 'C': 14}) 

True 

 

A payoff vector that returns ``False``:: 

 

sage: A_function = {(): 0, 

....: (1,): 0, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 55, 

....: (1, 2, 3,): 55} 

sage: A_game = CooperativeGame(A_function) 

sage: A_game.nullplayer({1: 10, 2: 10, 3: 25}) 

False 

 

A longer example for nullplayer:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 0, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 65} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.nullplayer({1: 20, 2: 20, 3: 5, 4: 20}) 

True 

 

TESTS: 

 

Checks that the function is going through all players:: 

 

sage: A_function = {(): 0, 

....: (1,): 42, 

....: (2,): 12, 

....: (3,): 0, 

....: (1, 2,): 55, 

....: (1, 3,): 42, 

....: (2, 3,): 12, 

....: (1, 2, 3,): 55} 

sage: A_game = CooperativeGame(A_function) 

sage: A_game.nullplayer({1: 10, 2: 10, 3: 25}) 

False 

""" 

for player in self.player_list: 

results = [] 

for coalit in self.ch_f: 

if player in coalit: 

t = tuple(sorted(set(coalit) - {player})) 

results.append(self.ch_f[coalit] == self.ch_f[t]) 

if all(results) and payoff_vector[player] != 0: 

return False 

return True 

 

def is_symmetric(self, payoff_vector): 

r""" 

Return ``True`` if ``payoff_vector`` possesses the symmetry property. 

 

A payoff vector possesses the symmetry property if 

`v(C \cup i) = v(C \cup j)` for all 

`C \in 2^{\Omega} \setminus \{i,j\}`, then `x_i = x_j`. 

 

INPUT: 

 

- ``payoff_vector`` -- a dictionary where the key is the player 

and the value is their payoff 

 

EXAMPLES: 

 

A payoff vector that has the symmetry property:: 

 

sage: letter_function = {(): 0, 

....: ('A',): 6, 

....: ('B',): 12, 

....: ('C',): 42, 

....: ('A', 'B',): 12, 

....: ('A', 'C',): 42, 

....: ('B', 'C',): 42, 

....: ('A', 'B', 'C',): 42} 

sage: letter_game = CooperativeGame(letter_function) 

sage: letter_game.is_symmetric({'A': 5, 'B': 14, 'C': 20}) 

True 

 

A payoff vector that returns ``False``:: 

 

sage: integer_function = {(): 0, 

....: (1,): 12, 

....: (2,): 12, 

....: (3,): 42, 

....: (1, 2,): 12, 

....: (1, 3,): 42, 

....: (2, 3,): 42, 

....: (1, 2, 3,): 42} 

sage: integer_game = CooperativeGame(integer_function) 

sage: integer_game.is_symmetric({1: 2, 2: 5, 3: 35}) 

False 

 

A longer example for symmetry:: 

 

sage: long_function = {(): 0, 

....: (1,): 0, 

....: (2,): 0, 

....: (3,): 0, 

....: (4,): 0, 

....: (1, 2): 0, 

....: (1, 3): 0, 

....: (1, 4): 0, 

....: (2, 3): 0, 

....: (2, 4): 0, 

....: (3, 4): 0, 

....: (1, 2, 3): 0, 

....: (1, 2, 4): 45, 

....: (1, 3, 4): 40, 

....: (2, 3, 4): 0, 

....: (1, 2, 3, 4): 65} 

sage: long_game = CooperativeGame(long_function) 

sage: long_game.is_symmetric({1: 20, 2: 20, 3: 5, 4: 20}) 

True 

""" 

sets = self.ch_f.keys() 

element = [i for i in sets if len(i) == 1] 

for c1, c2 in combinations(element, 2): 

results = [] 

for m in sets: 

junion = tuple(sorted(set(c1) | set(m))) 

kunion = tuple(sorted(set(c2) | set(m))) 

results.append(self.ch_f[junion] == self.ch_f[kunion]) 

if all(results) and payoff_vector[c1[0]] != payoff_vector[c2[0]]: 

return False 

return True