简体   繁体   中英

LP Programming with python using Pulp library

I have a linear programming problem where I need to minimise the cost of manufacturing a number of items in the span of n months. Xi is the variable for each amount of items manufactured corresponding to month i. Now, I want to include a constraint where if Xi > 0, then a number A is going to be added to the objective function.

Obviously this can't be done with a boolean expression inside a for loop for example since Xi is a class object from the pulp library. Does anybody know how to help me?

Docplex is not working

Thank you so much.

x = [LpVariable(name=f"x{i}", lowBound=0) for i in range(0, 12)]

# standards
manufacturing_time_per_unit = 1/3
cost_of_hour = 12
storage_cost_per_unit = 3

# these are monthly
cost_of_raw_materials_per_unit = [11, 10, 13, 9, 8, 7,
                                  10, 12, 12, 10, 9]
demand = [150, 200, 100, 300, 200,
          400, 300, 250, 150, 200, 300, 350]
avalaible_hours = [250, 250, 200, 150, 200, 200,
                   150, 200, 250, 150, 150, 200]

cost_sum = 0
stored = [100]

for i in range(1, 13):
    cost_constraint = manufacturing_time_per_unit*x[i-1] <= avalaible_hours[i-1]
    model += cost_constraint
    demand_constraint = x[i-1] + stored >= demand[i-1]
    model += demand_constraint
    stored.append(x[i-1] + stored - demand[i-1])
    cost_sum += manufacturing_time_per_unit*x[i-1]+stored[i-1]*storage_cost_per_unit
    storage_constraint = x[i-1] != 0
    #if x[i-1]>0:
    #    cost_sum += 1000

model += cost_sum
model.solve()

Add a binary variable y[i] with y[i]=0 => x[i]=0 . (This implies x[i]>0 => y[i]=1 .) Ie

  min sum(i, 1000*y[i])
  x[i] <= U*y[i]
  x[i] >= 0
  y[i] ∈ {0,1}

Here U is an upper bound on x[i].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM