提问人:Amal 提问时间:11/8/2023 最后编辑:Amal 更新时间:11/10/2023 访问量:105
使用 Xgboost 模型作为优化器的目标函数
Using Xgboost Model as an Objective Function for an Optimizer
问:
我正在尝试解决预算分配问题,其中需要在 20 种不同的营销产品中分配预算。我有一个 xgboost 模型,它采用预算(以及一些预先计算的其他变量)来预测最终的销售额。我需要从 20 种产品中选择 n (n<=20) 种产品,以最大限度地提高整体销售额。
我正在尝试将预算的 10% 分配给产品 1,5% 分配给产品 2 等比例。有一个函数可以计算为每个产品分配的最大预算比例。
我尝试使用 Gekko,但 xgboost 不支持 Gekko 数据类型,因此会抛出错误。由于时间和内存的限制,我无法真正创建所有可能的分配的组合并在该空间内进行搜索。
我尝试使用 scipy,但它不尊重边界甚至约束。有没有其他方法可以解决这个问题?
添加我在下面使用的代码
from scipy.optimize import minimize
def objective_function(allocations, budget=budget):
individual_predictions = [predict_results(allocations[i], i)
for i in range(num_products)]
total_prediction = -sum(individual_predictions)
if np.sum(allocations)!=1:
loss=1e+9
elif np.sum(allocations>0)!=7:
loss=1e+9
elif check_bounds(allocations, bounds)==False:
loss=1e+9
else:
loss=total_prediction/budget
return loss
num_products=len(preferred_list)
def total_allocations_constraint(allocations):
return np.sum(allocations) - 1
def total_selections_constraint(allocations):
return np.sum(allocations>0) - 7
def nonnegativity_constraint(allocations):
return np.sum(allocations<0)
constraints = (
{'type': 'eq', 'fun': total_selections_constraint},
{'type': 'eq', 'fun': total_allocations_constraint},
{'type': 'eq', 'fun': nonnegativity_constraint},
)
initial_allocations = [0 for i in preferred_list]
bounds = [(0, get_upper_bound(index_lookup.get(i))) for i in range(num_products)]
from datetime import datetime
t0=datetime.now()
print("Started at", t0)
result = minimize(objective_function,
initial_allocations,
method='trust-constr',
bounds=bounds,
constraints=constraints)
optimized_allocations = result.x
print("Completed at", datetime.now())
print("Optimized Allocations:", optimized_allocations)
答:
2赞
LaGrande Gunnell
11/9/2023
#1
不幸的是,Gekko 目前不支持 xgboost 或其他基于树的方法。由于后端求解器使用基于梯度下降的方法,因此模型的决策函数需要可区分且可使用 Gekko 变量重写。到目前为止,高斯过程回归、支持向量回归、线性回归和神经网络已集成到 Gekko: https://gekko.readthedocs.io/en/latest/ml.html 中。如果可能的话,我建议你用其中一个来重新建模你的问题,或者使用其他基于启发式的求解器来解决你的优化问题。
评论
method='SLSQP'