# model for three criteria
from scipy.optimize import minimize
a,b,c =[1/3,1/3,1/3]
v= [1,2,2]
def constraint1(x):
sum_eq = 1
for i in range(3):
sum_eq = sum_eq - x[i]
return sum_eq
con1 = {'type': 'eq', 'fun': constraint1}
def exp(p):
a,b,c= p
return (abs(a/b-v[0]) + abs(b/c-v[1]) +abs(a/c-v[2]))
msol = minimize(exp,[a,b,c],bounds=((0.0001,1),(0.0001,1),(0.0001,1)), constraints=con1)
a,b,c = msol.x
print(msol.x)
print(abs(a/b-v[0]),abs(b/c-v[1]),abs(a/c-v[2]))
[0.40000004 0.39999987 0.20000009] 4.080028628283827e-07 1.4950442814853915e-06 6.790391657851558e-07
# model for five criteria
from scipy.optimize import minimize
a,b,c,d,e =[0.2,0.2,0.2,0.2,0.2]
v= [1,3,1.33333333333333,1,3,4,1.33333333333333]
def constraint1(x):
sum_eq = 1
for i in range(5):
sum_eq = sum_eq - x[i]
return sum_eq
con1 = {'type': 'eq', 'fun': constraint1}
def exp(p):
a,b,c,d,e= p
return (abs(a/b-v[0]) + abs(b/c-v[1]) + abs(c/d-v[2]) + abs(d/e-v[3])+abs(a/c-v[4]) + abs(b/d-v[5])+abs(c/e-v[6]))
msol = minimize(exp,[a,b,c,d,e],bounds=((0.0001,1),(0.0001,1),(0.0001,1),(0.0001,1),(0.0001,1)), constraints=con1)
a,b,c,d,e = msol.x
print(msol.x)
print(abs(a/b-v[0]),abs(b/c-v[1]),abs(c/d-v[2]),abs(d/e-v[3]),abs(a/c-v[4]),abs(b/d-v[5]),abs(c/e-v[6]))
[0.35294111 0.35294649 0.11764713 0.08823251 0.08823275] 1.5237688374414304e-05 4.3234850662710045e-05 4.2953966792769194e-05 2.7842978901349014e-06 2.478873259992298e-06 0.00018651022502602643 3.924145000921975e-05