-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoFirms_twoLocations.py
84 lines (64 loc) · 2.39 KB
/
twoFirms_twoLocations.py
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
import numpy as np
import matplotlib.pyplot as plt
from sympy import *
from mpl_toolkits.mplot3d import axes3d
import scipy.optimize as opt
# price function
def price_func(a, b, Ak, Ai):
return (a - b * Ak) * Ai
# rent cost function
def cost_func(r, Ak, Ai):
return (r * Ak) * Ai
# production cost function
def prod_cost_func(c, Ai):
return c * Ai ** 2
# utility function
# def fun1(x1, x2):
# f1 = price_func(10, 2, (x1+x2))*x1 - cost_func(1, (x1+x2))*x1 - prod_cost_func(3, x1)
# return f1
#
# def fun2(x1, x2):
# f2 = price_func(10, 2, (x1+x2))*x2 - cost_func(1, (x1+x2))*x2 - prod_cost_func(4, x2)
# return f2
# f = price_func(coefs[0], coefs[1], (x1 + x2), x1) - cost_func(coefs[2], (x1 + x2), x1) - prod_cost_func(coefs[-1], x1)
# list_of_func = []
def grad_func1_x(variables, coefs):
x1, x2, y1, y2 = variables
ax, bx, ay, by, rx, ry, c1, c2 = coefs
df = ax - 2 * (bx + rx + c1) * x1 - (bx + rx) * x2 - 2*c1*y1
# list_of_func.append(grad_func1_x(variables, coefs))
return df
def grad_func1_y(variables, coefs):
x1, x2, y1, y2 = variables
ax, bx, ay, by, rx, ry, c1, c2 = coefs
df = ay - 2 * (by + ry + c1) * y1 - (by + ry) * y2 - 2*c1*x1
# list_of_func.append(grad_func1_y(variables, coefs))
return df
def grad_func2_x(variables, coefs):
x1, x2, y1, y2 = variables
ax, bx, ay, by, rx, ry, c1, c2 = coefs
df = ax - 2 * (bx + rx + c2) * x2 - (bx + rx) * x1 - 2*c2*y2
# list_of_func.append(grad_func2_x(variables, coefs))
return df
def grad_func2_y(variables, coefs):
x1, x2, y1, y2 = variables
ax, bx, ay, by, rx, ry, c1, c2 = coefs
df = ay - 2 * (by + ry + c2) * y2 - (by + ry) * y1 - 2*c2*x2
# list_of_func.append(grad_func2_y(variables, coefs))
return df
def error(variables, coefs):
e = 0
# for i in list_of_func:
# e += i ** 2
e += grad_func1_x(variables, coefs) ** 2 + grad_func1_y(variables, coefs) ** 2 + grad_func2_x(variables, coefs) ** 2 + grad_func2_y(variables, coefs) ** 2
return e
# coefs = [a,b,r,c1, c2]
coefs = np.array([10, 2, 20, 1, 4, 5, 1, 2])
initial = np.array([0, 0, 0, 0])
result = opt.minimize(error, initial, args=coefs)
if result.success:
variables = result.x
print("Firm 1: x1 = ", variables[0], "\ty1 = ", variables[2], "\nFirm 2: x2 = ", variables[1], "\ty2 = ", variables[-1])
print(error(result.x, coefs))
else:
raise ValueError(result.message)