scipy 使用Xgboost模型作为优化器的目标函数

uurv41yg  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(48)

我试图解决一个预算分配问题,其中预算需要在20种不同的营销产品中分配。我有一个xgboost模型,该模型采用预算(和其他一些预先计算的变量)来预测最终的销售额。我需要从20种产品中选择n(n<=20)种产品,以使整体销售额最大化。
我正试图分配预算的比例,如10%的预算,以product1,5%的product2等,有一个函数,计算应该是什么最大的预算比例分配给这些产品。
我尝试使用Gekko,但Gekko数据类型不受xgboost支持,因此抛出错误。由于时间和内存限制,我无法在该空间内创建所有可能的分配和搜索组合。
我试过使用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)

字符串

rseugnpd

rseugnpd1#

遗憾的是,Gekko目前不支持xgboost或其他基于树的方法。由于后端求解器使用基于梯度下降的方法,因此模型的决策函数需要是可区分的,并且需要与Gekko变量进行交互。到目前为止,Gaussian process regression,support vector regression,linear regression和neural networks已经集成到Gekko中:https://gekko.readthedocs.io/en/latest/ml.html .如果可能的话,我建议你用其中一个来改造你的问题,或者使用其他基于数学的求解器来解决你的优化问题。

相关问题