def policy_diff(r = 10, R_max = 100, a = 1000, b1 = 1, b2 = 1, q = -0.5, c = 0.05,
                d = 100, k = 2, p = 3, h = 0.06, g = 0.01, m = 0.3,
                W_min = 0, dt = 0.08):
  '''
  Loop through several different caps to see if policies have any effect. Takes in
  usual parameters, returns whether or not any of the policies led to a different
  outcome
  '''
  num_points = 50
  np.random.seed(0)
  initial_points = doe.lhs(3, samples = num_points)
  # Scale points ([R, U, W])
  R_0 = initial_points[:,0] * 100
  U_0 = initial_points[:,1] * 45
  W_0 = initial_points[:,2] * 20

  amount = 500
  caps = np.linspace(1.5, 31.5, 11)
  caps = np.append(caps, 100)
  res = np.zeros(len(caps))

  for i, cap in enumerate(caps):
    for j in range(num_points):
      ts = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m,
                              W_min, dt, R_0[j], W_0[j], U_0[j], cap, amount)
      R, E, U, S, W, P, L, convergence = ts.run()

      if R[-1] < 90 and U[-1] > 1:
        res[i] += 1

  return res/num_points
def resource_from_extraction(R_0, trajectory):
    R = np.zeros(len(trajectory) + 1)
    R[0] = R_0
    # set resource parameters
    r = 10
    R_max = 100  # aquifer capacity

    # Set industrial payoff parameters
    a = 1000  # profit cap
    b1 = 1
    b2 = 1
    q = -0.5
    c = 0.05  # extraction cost parameter
    d = 100

    # Set domestic user parameters
    k = 2  # water access parameter (how steeply benefit of water rises)
    p = 3  # out-of-system wellbeing/payoff
    h = 0.06  # rate at which wage declines with excess labor
    g = 0.01  # rate at which wage increases with marginal benefit of more labor
    m = 0.8  # responsiveness of population to wellbeing (h*p > 1 with dt=1)
    W_min = 0
    dt = 0.08
    W_0 = 0
    U_0 = 0
    for i, E in enumerate(trajectory):
        ts = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m, W_min,
                          dt, R_0, W_0, U_0)
        R[i + 1] = ts.resource(R[i], E)
    return R
def bifurcation(r=10,
                R_max=100,
                a=1000,
                b1=1,
                b2=1,
                q=-0.5,
                c=0.05,
                d=100,
                k=2,
                p=3,
                h=0.06,
                g=0.01,
                m=0.3,
                W_min=0,
                dt=0.08):
    '''
  Loop through several different caps to see if policies have any effect. Takes in
  usual parameters, returns whether or not any of the policies led to a different
  outcome
  '''
    num_points = 10
    np.random.seed(0)
    initial_points = doe.lhs(3, samples=num_points)
    # Scale points ([R, U, W])
    R_0 = initial_points[:, 0] * 100
    U_0 = initial_points[:, 1] * 45
    W_0 = initial_points[:, 2] * 20

    eq_R = np.zeros([num_points])
    eq_U = np.zeros([num_points])
    eq_W = np.zeros([num_points])

    for i in range(num_points):
        ts = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m, W_min,
                          dt, R_0[i], U_0[i], W_0[i])
        R, E, U, S, W, P, L, convergence = ts.run()

        eq_R[i] = R[-1]
        eq_U[i] = U[-1]
        eq_W[i] = W[-1]

    return eq_R, eq_U, eq_W
def wellbeing(resource_data, wage_data, pop_data, labor_data, num_points):
  """
  calculates wellbeing trajectories for each policy
  """
    # set resource parameters
  r = 10
  R_max = 100  # aquifer capacity

  # Set industrial payoff parameters
  a = 1000  # profit cap
  b1 = 1
  b2 = 1
  q = -0.5
  c = 0.05  # extraction cost parameter
  d = 100

  # Set domestic user parameters
  k = 2 # water access parameter (how steeply benefit of water rises)
  p = 3 # out-of-system wellbeing/payoff
  h = 0.06 # rate at which wage declines with excess labor
  g = 0.01 # rate at which wage increases with marginal benefit of more labor
  m = 0.3 # responsiveness of population to wellbeing (h*p > 1 with dt=1)
  W_min = 0
  dt = 0.08
  W_0 = 0
  U_0 = 0
  R_0 = 0

  wellbeing_trajectories = [0]*num_points
  ts = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m,
                        W_min, dt, R_0, W_0, U_0)

  for i in range(num_points):
    water_access_data = np.array(ts.water_access(np.array(resource_data[i])))[1:]
    wage_data[i] = np.array(wage_data[i][1:])
    wellbeing_trajectories[i] = water_access_data*wage_data[i]*labor_data[i]
  return wellbeing_trajectories
Example #5
0
W_min = 0

dt = 1

N = 10

U_arr = np.linspace(1, 55, N)
W_arr = np.linspace(1, 30, N)

dU = np.zeros((N, N))
dW = np.zeros((N, N))
dR = np.zeros((N, N))

for j, U_0 in enumerate(U_arr):
    for l, W_0 in enumerate(W_arr):
        pp = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m, W_min,
                          dt, 100, W_0, U_0)
        R, E, U, S, W, P, L, convergence = pp.run()
        dU[j, l] = U[-1] - U_0
        if U[-1] < 0.1:
            dW[j, l] = 0 - W_0
        else:
            dW[j, l] = (L[-1] / U[-1]) * W[-1] - W_0

U, W = np.meshgrid(U_arr, W_arr)

plt.figure()
ax = plt.axes()
ax.quiver(U, W, dU, dW)

ax.set_xlabel('Population (U)')
ax.set_ylabel('Wage (W)')
Example #6
0
p = 3  # out-of-system wellbeing/payoff
h = 0.06  # rate at which wage declines with excess labor
g = 0.01  # rate at which wage increases with marginal benefit of more labor
m = 0.8  # responsiveness of population to wellbeing (h*p > 1 with dt=1)
W_min = 0

# Step size
dt = 0.08

R_0 = 100
U_0 = 10
W_0 = 4

W_min = 0

ts = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m, W_min, dt, R_0,
                  W_0, U_0)
R, E, U, S, W, P, L, convergence = ts.run()
R = np.array(R)

n = len(R)
T = n * dt
fig, axarr = plt.subplots(3)
axarr[0].plot(np.arange(n) * dt, R, color='k')
axarr[0].set_ylim([0, R_max])
axarr[0].set_title('Resource')
axarr[1].plot(np.arange(n) / (n / T), U, color='k')
axarr[1].set_title('Population')
axarr[1].set_ylim([0, 30])
eff_W = W[1:] * (np.array(L) / np.array(U[1:]))
axarr[2].plot(np.arange(n - 1) / (n / T), eff_W, color='k')
axarr[2].set_title('Effective Wage')
Example #7
0
def make_policy_cm(fines, fine_caps, fees, fee_caps, r, R_max, a, b1, b2, q, c,
                   d, k, p, h, g, m, W_min, dt, processor_num,
                   cells_per_processor):
    """
  Produces a colormap for various combinations of fines and thresholds.
  For each policy, runs simulation over 100 different trajectories and averages
  the payoff and population and keeps track of the proportion sustainable equilibrium.
  """
    # check which policy is an array
    if hasattr(fines, "__len__"):
        amounts = fines
        caps = fine_caps
        policy = 'fine'
    else:
        amounts = fees
        caps = fee_caps
        policy = 'fee'

    # set initial conditions to loop over
    np.random.seed(1)
    num_points = 80
    initial_points = doe.lhs(3, samples=num_points)
    # Scale points ([R, U, W])
    initial_points[:, 0] = initial_points[:, 0] * 100
    initial_points[:, 1] = initial_points[:, 1] * 45
    initial_points[:, 2] = initial_points[:, 2] * 20

    print('running')
    #
    #  Es = create_2d_list(len(amounts), len(caps))
    #  Us = create_2d_list(len(amounts), len(caps))
    #  Ps = create_2d_list(len(amounts), len(caps))
    #  Ws = create_2d_list(len(amounts), len(caps))

    for i, cap in enumerate(caps):
        for j, amount in enumerate(amounts):
            R_trajectories = [0] * num_points
            U_trajectories = [0] * num_points
            P_trajectories = [0] * num_points
            W_trajectories = [0] * num_points
            L_trajectories = [0] * num_points

            for n, point in enumerate(initial_points):
                R_0 = point[0]
                U_0 = point[1]
                W_0 = point[2]
                if policy == 'fee':
                    pp = simulate_SES(r,
                                      R_max,
                                      a,
                                      b1,
                                      b2,
                                      q,
                                      c,
                                      d,
                                      k,
                                      p,
                                      h,
                                      g,
                                      m,
                                      W_min,
                                      dt,
                                      R_0,
                                      W_0,
                                      U_0,
                                      fee_cap=cap,
                                      fee=amount)
                else:
                    pp = simulate_SES(r,
                                      R_max,
                                      a,
                                      b1,
                                      b2,
                                      q,
                                      c,
                                      d,
                                      k,
                                      p,
                                      h,
                                      g,
                                      m,
                                      W_min,
                                      dt,
                                      R_0,
                                      W_0,
                                      U_0,
                                      fine_cap=cap,
                                      fine=amount)
                R, E, U, S, W, P, L, converged = pp.run()

                # save trajectories
                R_trajectories[n] = R
                U_trajectories[n] = U
                P_trajectories[n] = P
                W_trajectories[n] = W
                L_trajectories[n] = L

            # Save  compressed colormap data
            p_row = processor_num // 8
            p_column = processor_num % 8
            piece_number = 200 * p_row + 5 * p_column + 40 * j + i  #cells_per_processor*processor_num + i # goes from 0 to 1599
            with bz2.BZ2File("labor_%s.p" % (piece_number), 'w') as f:
                pickle.dump(L_trajectories, f)

            with bz2.BZ2File("pop_%s.p" % (piece_number), 'w') as f:
                pickle.dump(U_trajectories, f)

            with bz2.BZ2File("payoff_%s.p" % (piece_number), 'w') as f:
                pickle.dump(P_trajectories, f)

            with bz2.BZ2File("wage_%s.p" % (piece_number), 'w') as f:
                pickle.dump(W_trajectories, f)
def plot_equilibrium_basins(r, R_max, a, b1, b2, q, c, d, fine_cap, fine, fee_cap,
                            fee, k, p, h, g, m, W_min, dt, policy, num_points):

  """
  Produce scatterplot of randomly sampled initial conditions color coded by the
  equilibrium that they end up at. Saves a file with the equilibrium (whether it
  is a sustainable or collapse outcome) for each point.

  """

  # generate initial points using latin hypercube sampling

  np.random.seed(0)
  initial_points = doe.lhs(3, samples = num_points)
  # Scale points ([R, U, W])
  initial_points[:,0] = initial_points[:,0] * 100
  initial_points[:,1] = initial_points[:,1] * 45
  initial_points[:,2] = initial_points[:,2] * 20


  sust_eq = 0
  # initialize matrix recording whether initial point leads to good or bad eq
  eq_condition = np.zeros(len(initial_points))
  # matrix for recording sustainable eq
  eq = []
  ax = plt.axes(projection='3d')

  for i, point in enumerate(initial_points):
    R_0 = point[0]
    U_0 = point[1]
    W_0 = point[2]
    pp = simulate_SES(r, R_max, a, b1, b2, q, c, d, k, p, h, g, m,
                      W_min, dt, R_0, W_0, U_0, fine_cap, fine, fee_cap, fee)
    R, E, U, S, W, P, L, convergence = pp.run()

    if R[-1] > 90 and U[-1] < 1:
      eq_condition[i] = 0
      ax.scatter(R[0], U[0], W[0], s = (30,), marker = 'o', color = 'red', alpha = 0.3)
    else:
      ax.scatter(R[0], U[0], W[0], s = (30,), marker = 'o', color = 'blue', alpha = 0.3)
      ax.scatter(R[-1], U[-1], W[-1], s = (60,), marker = '*', color = 'b')
      eq_condition[i] = 1
      sust_eq += 1
      eq.append((R[-1],U[-1],W[-1]))


  ax.scatter(100, 0, 0, s = (60,), marker = 'X', color = 'red')
  ax.set_xlabel('Resource (R)')
  ax.set_ylabel('Population (U)')
  ax.set_zlabel('Wage (W)')
  if policy == 'fine':
    filename = 'scatterplot_data\\fine_eq_basins_c%s_%s_%s.csv'%(c, fine_cap, int(fine))
  else:
    filename = 'scatterplot_data\\fee_eq_basins_c2_%s_%s.csv'%(fee_cap, int(fee))
  with open(filename, 'w+') as f:
    csvwriter = csv.writer(f)
    csvwriter.writerows(np.transpose([initial_points[:,0], initial_points[:,1], initial_points[:,2], eq_condition]))
    csvwriter.writerows(eq)
  sust_eq /= len(initial_points)

  return sust_eq, eq_condition, eq