python - Gekko 优化以获得不同问题的评分
问题描述
我有一个包含 1000 个固定分数的客户的样本(R)所有客户必须回答 137 个问题。对于每个回答的问题,我们必须给它一个分数(T),分数应该在 0 到 24 之间(含)。因此我们必须优化 137 个分数,以便获得最大的 F1 Measure。我将不得不加载我的数据以获得 1000 名客户的 F1 测量。
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)
我非常感谢您的意见。我试图在下面的代码中应用我的逻辑,但它无法解决。
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)])
解决方案
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]