首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么IBM优化器studio OPL给出的结果与docplex不同?

为什么IBM优化器studio OPL给出的结果与docplex不同?
EN

Stack Overflow用户
提问于 2018-09-13 19:19:29
回答 1查看 167关注 0票数 0

这里是optimization-problem,我正在尝试解决(稍微有点扭曲,用我使用的opl代码)。

opl代码给出了两个解决方案,即:{Product12,Product31}

当我使用docplex使用以下代码将其转换为python语言时:

代码语言:javascript
复制
from docplex.mp.model import Model
from docplex.util.environment import get_environment

# ----------------------------------------------------------------------------
# Initialize the problem data
# ----------------------------------------------------------------------------

Categories_groups = {"Carbs": ["Meat","Milk"],"Protein":["Pasta","Bread"], "Fat": ["Oil","Butter"]}

Groups_Products = {"Meat":["Product11","Product12"], "Milk": ["Product21","Product22","Product23"], "Pasta": ["Product31","Product32"],
                   "Bread":["Product41","Product42"], "Oil":["Product51"],"Butter":["Product61","Product62"]}
Products_Prices ={"Product11":1,"Product12":1, "Product21":3,"Product22":3,"Product23":2,"Product31":1,"Product32":2,
                    "Product41":1,"Product42":3, "Product51": 1,"Product61":2,"Product62":1}

Uc={"Protein":1,"Carbs": 0, "Fat": 0}

Ug = {"Meat": 0.8, "Milk": 0.2, "Pasta": 0.1, "Bread": 1, "Oil": 0.01, "Butter": 0.6}


budget=2

def build_userbasket_model(**kwargs):


    allcategories = Categories_groups.keys()

    allgroups = Groups_Products.keys()

    allproducts = Products_Prices.keys()

    # Model
    mdl = Model(name='userbasket', **kwargs)
    z = mdl.binary_var_dict(allproducts, name='%s')

    xg = {g:  mdl.sum(z[p] for p in Groups_Products[g]) for g in allgroups}

    xc = {c: 1 <= mdl.sum(xg[g] for g in Categories_groups[c]) for c in allcategories}


    mdl.add_constraint(mdl.sum(Products_Prices[p] * z[p] for p in allproducts) <= budget)

    for g in allgroups:
        mdl.add_constraint(xg[g]==1 )


    for c in allcategories:
        mdl.add_constraint(Uc[c] == xc[c])



    mdl.maximize(mdl.sum(Uc[c] * xc[c] for c in allcategories) + mdl.sum(
        xg[g] * Uc[c] * Ug[g]  for c in allcategories for g in Categories_groups[c]  ))


    return mdl

if __name__ == '__main__':
    """DOcplexcloud credentials can be specified with url and api_key in the code block below.

    Alternatively, Context.make_default_context() searches the PYTHONPATH for
    the following files:

        * cplex_config.py
        * cplex_config_<hostname>.py
        * docloud_config.py (must only contain context.solver.docloud configuration)

    These files contain the credentials and other properties. For example,
    something similar to::

       context.solver.docloud.url = "https://docloud.service.com/job_manager/rest/v1"
       context.solver.docloud.key = "example api_key"
    """
    url = None
    key = None

    mdl = build_userbasket_model()

    # will use IBM Decision Optimization on cloud.
    if not mdl.solve(url=url, key=key):
        print("*** Problem has no solution")
    else:
        mdl.float_precision = 3
        print("* model solved as function:")

        mdl.print_solution()

        '''
        Solution displayed using the line of code above

        solution = mdl.solution



        for index, dvar in enumerate(solution.iter_variables()):
            print index, dvar.to_string(), solution[dvar], solution.get_var_value(dvar)



        # Save the CPLEX solution as "solution.json" program output
        with get_environment().get_output_stream("solution.json") as fp:
            mdl.solution.export(fp, "json")

我明白了:

*问题没有解决办法

我不明白为什么我有不同的结果,谁能帮我这个忙吗?

提前谢谢你。

问候

EN

回答 1

Stack Overflow用户

发布于 2018-09-14 14:17:00

如果一个问题提供了一个可行的解决方案,而另一个问题被认为是不可行的,那么很可能您并不是在解决相同的问题。有几件事你可以做:

  1. 再次检查您的Python代码是否正确。
  2. 将模型从OPL和Python代码导出为LP文件,并比较这两种模型。
  3. 使用冲突细化器来了解为什么Python模型是不可行的。这样,您就可以知道Python模型的问题所在。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52320428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档