微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Gekko Optimization获得对不同问题的评分

如何解决Gekko Optimization获得对不同问题的评分

我有1000个具有固定分数的客户的样本 所有客户必须回答137个问题。 对于每个回答的问题,我们都必须给它一个分数(T),分数应在0到24之间(含0和24)。因此我们必须优化137分数,以便获得最大的F1测度。 我必须加载我的数据才能获得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,1)
SR = m.FV(value=0,lb=0,ub=1,integer=True)


#countifs = m.sum([m.if3(H[i]*S[i]-0.1,1) for i in range(nq)])
countifs = m.if3(HR*SR-0.1,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)])

解决方法

这是一个示例程序,可以帮助您入门。

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,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)])

这里是带有nq=7的示例解决方案,因此解决方案不会太长。

 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,1,1]
S:  [1.0,0.0,1.0,1.0]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。