栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

最大化消耗能源

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

最大化消耗能源

这是使用线性编程通过纸浆(https://pypi.org/project/PuLP)的解决方案,为我提供了最佳解决方案

Maximum energy level: 758.0Mapping of stores per foodtype: {1: [9, 2, 4], 0: [3, 8, 0, 6, 7], 2: [1, 5]}

我认为该性能应优于手动编码的穷举求解器。

from collections import defaultdictimport pulp# datanStores = 10a, b, c = max_stores = 5, 3, 2matrix = [    [56, 44, 41],    [56, 84, 45],    [40, 98, 49],    [91, 59, 73],    [69, 94, 42],    [81, 64, 80],    [55, 76, 26],    [63, 24, 22],    [81, 60, 44],    [52, 95, 11]]# create an LP problemlp = pulp.LpProblem("maximize energy", sense=pulp.LpMaximize)# create the list of indices for the variables# the variables are binary variables for each combination of store and food_type# the variable alpha[(store, food_typeà] = 1 if the food_type is taken from the storeindex = {(store, food_type) for store in range(nStores) for food_type in range(3)}alpha = pulp.LpVariable.dicts("alpha", index, lowBound=0, cat="Binary")# add the constrain on max storesfor food_type, n_store_food_type in enumerate(max_stores):    lp += sum(alpha[(store, food_type)] for store in range(nStores)) <= n_store_food_type# only one food type can be taken per storefor store in range(nStores):    lp += sum(alpha[(store, food_type)] for food_type in range(3)) <= 1# add the objective to maximiselp += sum(alpha[(store, food_type)] * matrix[store][food_type] for store, food_type in index)# solve the problemlp.solve()# collect the resultsstores_for_foodtype = defaultdict(list)for (store, food_type) in index:    # check if the variable is active    if alpha[(store, food_type)].varValue:        stores_for_foodtype[food_type].append(store)print(f"Maximum energy level: {lp.objective.value()}")print(f"Mapping of stores per foodtype: {dict(stores_for_foodtype)}")


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/650170.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号