Created
April 21, 2020 04:42
-
-
Save hfziu/a28af9d1c4b45b77462a7e3e452693ad to your computer and use it in GitHub Desktop.
math-code-snippets
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
import numpy as np | |
import cvxpy as cp | |
# Consider the Prisoner's Dilemma https://en.wikipedia.org/wiki/Prisoner%27s_dilemma | |
# Variable v is prisoner A's payoff in the worst case (minimum) | |
v = cp.Variable() # expected utility of prisoner A | |
c = cp.Variable(nonneg=True) # probability that A cooperates | |
d = cp.Variable(nonneg=True) # probability that A defects | |
obj = cp.Maximize(v) | |
constraints = [ | |
c + d == 1, | |
v <= -c, | |
v <= -3*c + -2 * d | |
] | |
prob = cp.Problem(obj, constraints) | |
prob.solve() | |
if prob.status == 'optimal': | |
print(f'optimal value: {prob.value}') | |
print(f'optimal solution: p(cooperate) = {c.value}, p(defect) = {d.value}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment