5

I am trying to assign material property using multiple variables. For example; density and conductivity are two decision variables for material_1, material_2 and material_3.

I have to input the following information:

density of material_1 = 1000
density of material_2 = 2000
density of material_3 = 1500

conductivity of material_1 = 250
conductivity of material_2 = 400
conductivity of material_3 = 100

The standard format for defining variables in Pyomo is given below:

model.variable_1 = Var(bounds=(800,2000))

The above code means variable_1 is a variable with lower bound = 800, and upper bound = 2000.

But how can we define a variable with a specific set of values instead of a bound?

The idea is to input data values into the optimizer such that when it chooses a density value, it should also choose the conductivity value from the same material.

How can we impose such a condition into pyomo framework? Can someone please help me with this?

Rua Goa
  • 127
  • 9
  • 3
    Are you making **quantity** selections of one or multiple materials (such as make the item from 10.5 pounds of steel and 4.2 pounds of aluminum) or is it just a use/don't use (selection) decision for across a range of materials? This will help shape the suggestion. What you are asking above is completely doable. – AirSquid Dec 07 '20 at 18:25
  • @AirSquid It is just a use/don't use (selection) decision for across a range of materials. – Rua Goa Dec 07 '20 at 19:23

1 Answers1

2

So if you are just selecting one of many options, you can set this up as an Integer Linear Program. The basic gist is that we let a binary variable x in the example below represent the act of selecting material i, where i is a member of the set of materials.

In your question above, you seem to be struggling with the concept of separating the parameters in the model (price, density, conductivity, etc.) which are fixed in value from the variables which are the decisions you want to model.

A slightly more advanced model than below might be a mixing model, where you can take proportions of various materials within some constraints, etc. which would require changing the domain of x to be non-negative real numbers. This one just models the binary action of selection. Of course in a model as trivial as this, you could just solve it with list/dictionary comprehensions or a filter, so using algebraic modeling is really overkill, but it is an example to differentiate the concepts you asked about.

# material selection model

import pyomo.environ as pyo

# data
materials = ['steel', 'alum', 'carbon', 'cheese']

density =   {   'steel' : 1.2,
                'alum'  : 0.8,
                'carbon': 1.8,
                'cheese': 0.7}

conductivity = {'steel' : 6.4,
                'alum'  : 3.1,
                'carbon': 4.4,
                'cheese': 0.3}

price =     {   'steel' : 2.3,
                'alum'  : 3.5,
                'carbon': 5.8,
                'cheese': 6.0}

m = pyo.ConcreteModel('material selector')

# SETS (used to index the decision variable and the parameters)
m.matl = pyo.Set(initialize=materials)

# VARIABLES
m.x = pyo.Var(m.matl, domain=pyo.Binary)   # a binary decision variable representing the selection of matl

# PARAMETERS
m.density = pyo.Param(m.matl, initialize=density)
m.conductivity = pyo.Param(m.matl, initialize=conductivity)
m.price = pyo.Param(m.matl, initialize=price)


# OBJ (minimize price)
m.obj = pyo.Objective(expr=sum(m.x[i] * m.price[i] for i in m.matl))

# Constraints
m.c1 = pyo.Constraint(expr=(sum(m.x[i] * m.density[i] for i in m.matl) >= 1.0))     # min density
m.c2 = pyo.Constraint(expr=(sum(m.x[i] * m.conductivity[i] for i in m.matl) <= 5.0)) # max cond.

# solve it
solver = pyo.SolverFactory('glpk')
result = solver.solve(m)
m.display()

Yields:

Model material selector

  Variables:
    x : Size=4, Index=matl
        Key    : Lower : Value : Upper : Fixed : Stale : Domain
          alum :     0 :   0.0 :     1 : False : False : Binary
        carbon :     0 :   1.0 :     1 : False : False : Binary
        cheese :     0 :   0.0 :     1 : False : False : Binary
         steel :     0 :   0.0 :     1 : False : False : Binary

  Objectives:
    obj : Size=1, Index=None, Active=True
        Key  : Active : Value
        None :   True :   5.8

  Constraints:
    c1 : Size=1
        Key  : Lower : Body : Upper
        None :   1.0 :  1.8 :  None
    c2 : Size=1
        Key  : Lower : Body : Upper
        None :  None :  4.4 :   5.0
AirSquid
  • 10,214
  • 2
  • 7
  • 31
  • 2
    Thank you for your effort and support. I got the knowledge of how to use the Set and Parameters from your example. But for the objective function, I have some doubts because my problem formulation is a bit different. Imagine you have 2 rectangular plates: Objective function = Area_1 * thickness_1 * density_1 + Area_2 * thickness_2 * density_2; Area and thickness are fixed for each plates. But the material is chosen by the solver to get the best results. In this case, how do you modify your code? Thank you once again for your support. – Rua Goa Dec 07 '20 at 22:58
  • 3
    So, in the extension you mention above, what are the possible outcomes for the selection of material? Is there a constraint to only select 1 material, or is it possible to select 2 different materials for the differing plates, and if so, is it *required* that the materials be different or just an option? – AirSquid Dec 07 '20 at 23:02
  • There are constraints which contains terms of conductivity. The plates can take any material. They can be same material. The idea is to let the solver decide the material for each plates. It depends on the constraints. For example the minimum required heat transfer for a plate could be a constraint. The total heat transfer through both the plates could be another constraint. Thank you very much. – Rua Goa Dec 07 '20 at 23:25
  • 1
    So that will be a more complex model, but totally doable. You can use a foundation like above, and the best thing to do would be to introduce a new set that contains an index to the plates and then double-index your decision variable to both the material and the plate, which can capture the assignment from each material to each plate and will be set up for the model. Do some research & give it a try! – AirSquid Dec 07 '20 at 23:47
  • Dear @AirSquid, I find it difficult to formulate this problem using 2 sets. I am posting this problem in more detail with your idea of solving. I would appreciate it if you could please hep me with this. Thank you very much. https://stackoverflow.com/questions/65201147/pyomo-defining-dataset-using-sets-and-parameters-for-solving-an-optimization-pr – Rua Goa Dec 08 '20 at 14:52