python - Gekko 优化以获得不同问题的评分

标签 python optimization 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)])

最佳答案

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

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

这是一个示例解决方案,其中 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, 0, 1, 0, 0, 0, 1]
S:  [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]

关于python - Gekko 优化以获得不同问题的评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64396053/

相关文章:

python - 如何用狮身人面像制作两列

python - 单元测试问题——接收 str 对象没有属性 'get'

algorithm - 查询 3d 点

python - 如何从gekko中获取 'infeasibilities.txt'

gekko - 当我使用较少的 nlp 迭代时,APOPT 正在为 MINLP 问题找到更好的局部最小值。我期待相反的结果,我错过了什么?

python - 通过 pip : Microsoft Visual C++ 14. 安装 lxml 时出错 0 是必需的

python - 将 pcollection 的每一行拆分为多个 pcollection?

c - 在 C 中将 float 转换为处理器内部的 int

c# - 快速查找可枚举对象中的结果

python - GEKKO'对象没有属性 'Maximize'