【问题标题】:Pyomo: variables with domain set of multiple individual valuesPyomo:具有多个单独值的域集的变量
【发布时间】:2021-09-09 10:57:38
【问题描述】:

我有一个非常复杂的问题,我必须使用求解器来解决。

我有一些变量,其中可能的值存储在一个数组中,例如 x1 不能在 0 和 1 之间的任何值,除了 possible_values 中指定的值。

以下示例是我模型的一小部分,因此不能使用几个循环来找到解决方案。

问题是我不能在 Var 函数中使用 possible_values 数组作为域。

你有什么想法和想法吗?

model = ConcreteModel()

possible_values = [0.1,0.25,0.2,0.05,0.4,0.15,0.75]  # add up to 1

model.x1 = Var(domain=possible_values)
model.x2 = Var(domain=possible_values)
model.x3 = Var(domain=possible_values)
model.x4 = Var(domain=possible_values)
model.x5 = Var(domain=possible_values)
model.x6 = Var(domain=possible_values)

obj_expr = 3*model.x2 + 1*model.x3 + 2*model.x4 + 7*model.x5 + 9*model.x6 + 10*model.x7 

model.minimizer = Objective(expr=obj_expr, sense=maximize)

model.const1 = Constraint(expr=model.x1 + model.x2 <= 0.3)
model.const2 = Constraint(expr=model.x4 + model.x3 >= 0.25)

solver.solve(model);

#values xN should all be different
print(value(model.x1)) #should be one of possible_values
print(value(model.x2)) #should be one of possible_values
print(value(model.x3)) #should be one of possible_values
print(value(model.x4)) #should be one of possible_values
print(value(model.x5)) #should be one of possible_values
print(value(model.x6)) #should be one of possible_values
> [0.1, 0.25, 0.2, 0.05, 0.4, 0.15, 0.75] is not a valid domain.
> Variable domains must be an instance of a Pyomo Set.  Examples:
> NonNegativeReals, Integers, Binary

【问题讨论】:

    标签: python python-3.x solver pyomo


    【解决方案1】:

    因此,您需要以不同的方式处理域片段……显然。 :)

    这是一个应该可行的想法。该示例显示了一个 x 变量,其基数为 2,可以采用 3 个值之一。还强制执行所有x_i 必须不同的概念。

    # weird domain
    
    import pyomo.environ as pyo
    
    ### DATA
    
    domain = {  0 : 0.3,
                1 : 0.5,
                2 : 0.7}
    
    constants = {   0: 5,
                    1: 2}
    
    #### MODEL
    model = pyo.ConcreteModel('oddball domain')
    
    ### SETS
    model.I = pyo.Set(initialize=range(2))   # x index
    model.DI = pyo.Set(initialize=domain.keys())   # domain index
    
    ### PARAMETERS
    # note:  These aren't totally necessary, you could just work with python dictionaries
    #        but it adds clarity to the model
    model.vals = pyo.Param(model.DI, initialize=domain)
    model.c = pyo.Param(model.I, initialize=constants)
    
    ### VARIABLES
    model.x = pyo.Var(model.I, model.DI, domain=pyo.Binary)
    
    ### OBJ
    model.obj = pyo.Objective(expr=sum(model.x[i,di]*model.vals[di]*model.c[i] 
                                for i in model.I
                                for di in model.DI), sense=pyo.maximize)
    
    ### CONSTRAINTS
    # Only one.  For each x-index, we need to sum accross the domain and limit 
    # the selection to 1 or less
    def only_one(m, i):
        return sum(m.x[i,di] for di in m.DI) <= 1
    model.C1 = pyo.Constraint(model.I, rule=only_one)
    
    # All different.  Each x_i must be unique
    def unique(m, di):
        return sum(m.x[i,di] for i in m.I) <= 1
    model.C2 = pyo.Constraint(model.DI, rule=unique)
    
    #model.pprint()
    
    
    solver = pyo.SolverFactory('glpk')
    results = solver.solve(model)
    print(results)
    
    for i in model.I:
        for di in model.DI:
            if model.x[i,di].value:
                print(f'x[{i}] = {model.vals[di]}')
    

    产量:

    Problem: 
    - Name: unknown
      Lower bound: 4.5
      Upper bound: 4.5
      Number of objectives: 1
      Number of constraints: 6
      Number of variables: 7
      Number of nonzeros: 13
      Sense: maximize
    Solver: 
    - Status: ok
      Termination condition: optimal
      Statistics: 
        Branch and bound: 
          Number of bounded subproblems: 1
          Number of created subproblems: 1
      Error rc: 0
      Time: 0.010771036148071289
    Solution: 
    - number of solutions: 0
      number of solutions displayed: 0
    
    x[0] = 0.7
    x[1] = 0.5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 2017-11-22
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      相关资源
      最近更新 更多