Last active
August 29, 2015 14:09
-
-
Save niklasf/595bea6349f166d1a8d1 to your computer and use it in GitHub Desktop.
Compute a deterministic strategy to sustain the highest feasible discounted payoff in the battle of the sexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <gmp.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) { | |
if (argc <= 1) { | |
printf("Give number of iterations as arguments!\n"); | |
} | |
mpq_t husband, wife, value, delta; | |
mpq_init(husband); | |
mpq_init(wife); | |
mpq_init(value); | |
mpq_init(delta); | |
mpq_set_ui(delta, 9, 10); | |
for (int j = 1; j < argc; j++) { | |
if (j != 1) { | |
printf("\n"); | |
} | |
int limit = atoi(argv[j]); | |
mpq_set_ui(husband, 0, 1); | |
mpq_set_ui(wife, 0, 1); | |
mpq_set_ui(value, 1, 10); | |
for (int i = 0; i < limit; i++) { | |
if (mpq_cmp(husband, wife) <= 0) { | |
printf("■"); | |
mpq_add(husband, husband, value); | |
} else { | |
printf("□"); | |
mpq_add(wife, wife, value); | |
} | |
mpq_mul(value, value, delta); | |
} | |
printf("\n"); | |
gmp_printf("Husband: %Qu\n", husband); | |
gmp_printf(" Wife: %Qu\n", wife); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Slightly more flexible (but less precise for large numbers) | |
# Python implementation. | |
# Allows experimenting with periodic strategies. | |
from __future__ import print_function | |
d = 9.0 / 10.0 | |
def partial_sum(d, n): | |
return sum(d**k for k in range(0, n + 1)) | |
def infinite_sum(d): | |
return 1 / (1 - d) | |
def value(d, start, factor, offset): | |
return infinite_sum(d**factor) * d**(start + offset) | |
def payoffs(d, start, pattern): | |
payoff_1 = 0 | |
payoff_2 = 0 | |
for i, letter in enumerate(start): | |
if letter == "1": | |
payoff_1 += 2 * d**i | |
payoff_2 += 1 * d**i | |
elif letter == "2": | |
payoff_1 += 1 * d**i | |
payoff_2 += 2 * d**i | |
for i, letter in enumerate(pattern): | |
if letter == "1": | |
payoff_1 += 2 * value(d, len(start), len(pattern), i) | |
payoff_2 += 1 * value(d, len(start), len(pattern), i) | |
elif letter == "2": | |
payoff_1 += 1 * value(d, len(start), len(pattern), i) | |
payoff_2 += 2 * value(d, len(start), len(pattern), i) | |
return (1 - d) * payoff_1, (1 - d) * payoff_2 | |
s = "" | |
husband, wife = 0.0, 0.0 | |
for i in range(0, 100): | |
print(s.replace("1", "_").replace("2", "X"), (husband, wife)) | |
husband, wife = payoffs(d, s, "") | |
if husband <= wife: | |
s += "1" | |
else: | |
s += "2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
■□□■□■■□□■
Husband: 3238351489/10000000000
Wife: 327486411/1000000000
■□□■□■■□□■■□■□□■□■■□□■■□■□□■■□□■□■■□■□□■□■■□■□□■□■
Husband: 49739528258046340800842059722946801782876073783789/100000000000000000000000000000000000000000000000000
Wife: 4974509422122164786812147914728757694442181869421/10000000000000000000000000000000000000000000000000
■□□■□■■□□■■□■□□■□■■□□■■□■□□■■□□■□■■□■□□■□■■□■□□■□■■□■□□■□■■□■□□■□■■□■□□■□■■□□■■□■□□■□■■□■□□■■□□■□■■□
Husband: 499986726565125620045726400063973312900805405218007804755875999039157324275124003971303229076981011/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Wife: 4999867120359867924773397218038231091365116714367268557945265434646687664757858658103973324531145889/10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
■□□■□■■□□■■□■□□■□■■□□■■□■□□■■□□■□■■□■□□■□■■□■□□■□■■□■□□■□■■□■□□■□■■□■□□■□■■□□■■□■□□■□■■□■□□■■□□■□■■□□■■□■□□■■□□■□■■□□■■□■□□■□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□■□□■□■■□□■■□■□□■□■■□■□□■□■■□■□□■■□□■□■■□□■■□■□□■■□□■□■■□□■■□■□□■■□□■□■■□■□□■□■■□■□□■■□□■□■■□□■■□■□□■□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□□■■□■□□■■□□■□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□□■■□■□□■□■■□■□□■□■■□■□□■□■■□□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□■□□■■□□■□■■□■□□■□■■□□■■□■□□■■□□■□■■□□■■□■□□■□■■□■□□■□■■□□■■□
Husband: 4999999999999999999999933742136868229403459326989431080103889925348554177623688440037587758425104273994244690259045809553870722379066801984174059430640922079574664279659657851168570226305217997938923283515081979443302963469496914369138408064731770019521633554561709687387774978630358910368095577041144455982458437236469589542352441097389915806097771721649974374100923343753339973135822887840990434781458918964392904197845933799446182507774226635536492426248817205263788483132615147241422306836978011/10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Wife: 49999999999999999999999340507811836899328516274845937054595135324481706075595450679255895587151610555158012319095691296399328866431635107575903454739008678585341491860678163534640270116722621699806889150074951240725695974904913267690574790404866676710345602888209849039447759707518285415974638682534158401279598162267049188440254758757537063356732054367101948371093848006062515369704161748347923805825472114839305939080952552945112085250818869541820725351863080781530104554307716354483008028774999889/100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000