Unable to optimize the values

16 Views Asked by At

The loss function code is:

def LossFun2(w0_new):
    predicted_w1bar = np.round(np.dot(w0_new[:4] , weights), 2)
    predicted_w2bar = np.round(np.dot(w0_new[4:8] ,weights), 2)
    predicted_w3bar = np.round(np.dot(w0_new[8:], weights), 2)
    diff_Values = np.square(predicted_w1bar - res1[0]) + np.square(predicted_w2bar - 
                             res1[1]) + np.square(predicted_w3bar - res1[2])
    return diff_Values
w0_new = np.ones(12) * (1/3)      
weights = np.array([0.2, 0.4, 0.3, 0.1])  
res1 = np.array([0.3, 0.5, 0.2]) 
LossFun2(w0_new)

The constraints for this are are:

w0_new11 + w0_new21 + · · · + w0_newp1 = 1
w0_new12 + w0_new22 + · · · + w0_newp2 = 1
w0_new13 + w0_new23 + · · · + w0_newp3 = 1
w0_new14 + w0_new24 + · · · + w0_newp4 = 1 

for w0_new values

The optimization code:

num1 = len(w0_new)
A_Values = np.ones((1, num1))
A_eq_additional = np.eye(num1)
b_eq_additional = np.ones(num1)
A_eq = np.vstack((A_Values, A_eq_additional))
b_eq = np.hstack((1, b_eq_additional))
linear_constraintValues = LinearConstraint(A_eq, lb=b_eq, ub=b_eq)
individual_boundsValues = [(0, `enter code here`1)] * num1

Perform optimization

res2 = minimize(LossFun2, w0_new, constraints=[linear_constraintValues], 
bounds=individual_boundsValues)
print("\nOptimized weights:", res2.x)

output:

Optimized weights: [0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333],

and the output values didn't get changed which are exactly same as w0_new values. Where do I need to change for the values to get optimized or to get optimized values.

0

There are 0 best solutions below