Ejemplo n.º 1
0
Resource_available = {'fertilizer':1260, 'pesticide':780}       # amount of fertilizer and pesticide available
Resource_required = {('Barley', 'fertilizer'):4,
                     ('Wheat', 'fertilizer'):12,
                    ('Rice', 'fertilizer'):18,
                    ('Maize', 'fertilizer'):19,
                    ('Barley', 'pesticide'):5,
                    ('Wheat', 'pesticide'):4,
                    ('Rice', 'pesticide'):3,
                    ('Maize', 'pesticide'):6}

                
# Concrete Model instantiates the data of the problem
model = ConcreteModel()

# Decision variables - varying the combination of plant area
model.SeasonProd = Var(Plants, within=NonNegativeReals)

# Objective - to maximize profit
# meaning: for each product, calculate the profit rate * production, sum all these at the end
model.obj = Objective(expr = sum(SellingPrice[i] * model.SeasonProd[i] for i in Plants), 
                      sense=maximize)

# Constraint - uses a Capacity Rule and Land Area function
# meaning: for each plant, the resource used should not exceed the total resource available
def CapacityRule(model, p):
    """User defined capacity rule - 
    Accepts a pyomo ConcreteModel as the first positional argument,
    and a plant index as a second positional argument"""
    return sum(Resource_required[i,p] * model.SeasonProd[i] for i in Plants) <= Resource_available[p]

# meaning: sum of all planting area should not exceed the total area owned by farmer