Gekko Optimization to get ratings to different questions

42 Views Asked by At

I have a sample of 1000 customers who have a fixed score(R) All the customers must answer 137 questions. For each question answered we have to give it a score(T) and the score should be between 0 and 24 inclusive. and so we have to optimize 137 scores so that we get maximum F1 Measure. i will have to load my data to get F1 measure for 1000 customers.

sumS = sum(T)*4
if T>100 than H=1 else 0
SR = fixed value( either 0 or 1 for each customer)

Recall and precision will be calculated for all the 1000 customers

Recall = number of customers having R>0 and SR>0/no. of customers having R>0
Precision = number of customers having R>0 and SR>0/no. of customers having SR>0
Objective = (2*Recall*Precision/Recall+Precision)

I would highly appreciate your input. i tried to do apply my logic in the following code but its not solvable.

from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
nq = 137 # number of questions

# select 24 questions out of 137

S = m.Array(m.Var,(nq) ,integer=True)

for i in range(nq):
    S[i].value = 2
    S[i].lower = 0
    S[i].upper = 24

sumS = m.sum(S)*4.17

HR= m.if3(sumS-101 , 0 ,1)
SR = m.FV(value=0, lb=0,ub=1,integer=True)


#countifs = m.sum([m.if3(H[i]*S[i]-0.1,0,1) for i in range(nq)])
countifs = m.if3(HR*SR-0.1, 0,1) 

#countSR = m.sum(SR for i in range (nq))
#countHR = m.sum(HR for i in range (nq))

Recall    = m.Intermediate(countifs/SR)
Precision = m.Intermediate(countifs/HR)
m.Maximize(2*Recall*Precision/(Recall+Precision))

# solve
m.solve(disp=False)

print('T: ', T.value)
print('HR: ', HR.value)
print('SR:' , SR.value)
print('Precision', Precision.value)
print('Recall', Recall.value)
print('S: ', [S[i].value[0] for i in range(nq)])
1

There are 1 best solutions below

1
On

Here is a sample program that may help you get started.

from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
nq = 137 # number of questions

# select 24 questions out of 137
S = m.Array(m.Var,nq,value=0,integer=True,lb=0,ub=1)
sumS = m.Var(lb=1,ub=24)
m.Equation(sumS==m.sum(S))

# rating (0-200) for 137 questions
T = np.random.rand(nq)*200
H = [1 if T[i]>100 else 0 for i in range(nq)]

# if H>=0 and S>=0 as H*S>0.1 (or some tolerance above 0)
countifs = m.sum([m.if3(H[i]*S[i]-0.1,0,1) for i in range(nq)])

Recall    = m.Intermediate(countifs/sumS)
Precision = m.Intermediate(countifs/np.sum(H))
m.Maximize(2*Recall*Precision/(Recall+Precision+1e-5))

# solve
m.solve()

print('T: ', T)
print('H: ', H)
print('S: ', [S[i].value[0] for i in range(nq)])

Here is a sample solution with nq=7 so that the solution isn't too long.

 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.0636 sec
 Objective      :  -0.9999950000249999
 Successful solution
 ---------------------------------------------------
 

T:  [111.19615552  81.07719896 112.01092282  69.69595595  84.29274544
  32.18879746 107.07809521]
H:  [1, 0, 1, 0, 0, 0, 1]
S:  [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]