Skip to content

Instantly share code, notes, and snippets.

@hfziu
Created April 21, 2020 04:42
Show Gist options
  • Save hfziu/a28af9d1c4b45b77462a7e3e452693ad to your computer and use it in GitHub Desktop.
Save hfziu/a28af9d1c4b45b77462a7e3e452693ad to your computer and use it in GitHub Desktop.
math-code-snippets
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