OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

lpSum value dependent multiplier in Pulp

  • Thread starter Thread starter Emma Shibata
  • Start date Start date
E

Emma Shibata

Guest
I am struggling to select the correct cost_coefficient that is dependent on lpSum of the decision variables. I need to compare the lpSum(binary_option[option] for option in options) against the elements in threshold. The first element which is greater than lpSum(binary_option[option] for option in options), its corresponding cost coefficient (with the same index) should be used as the multiplier for the objective function. The following is what i have so far.

Please let me know how i can correctly formulate this.

Code:
``from pulp import LpProblem, LpVariable, lpSum, LpMinimize

# Define the options 
options = ['A', 'B', 'C', 'D', 'E', 'F']

# Define the thresholdsand their associated cost coefficients
cost_coefficient = [2, 6, 8, 11, 16, 20, 34]
threshold = [1, 2, 3, 4, 5, 6, 7]

# Create a PuLP ILP problem
model = LpProblem("Minimize_Cost", LpMinimize)

# Decision Variables: binary variables for each option
binary_option = {option: LpVariable(f"{option}", cat='Binary') for option in options}

# Decision Variables: binary variables for each cost coefficient
cost_coefficient_selection = []
for i in range(len(threshold)):
    var_name = f"cost_sel_{i}"
    cost_coefficient_selection.append(LpVariable(var_name, cat='Binary'))

# Total requirement is the sum of selected options
requirement = lpSum(binary_option[option] for option in options)


# Constraints: coefficient[i] ==1  for first threshold[i] that is greater than requirement
for i, t in enumerate(threshold):
    model += requirement >= t, f"Threshold_{i}"

# Objective: Minimize the total cost
model += lpSum(requierment * cost_coefficient_selection[i] *cost_coefficient[i]  )for i in range(len(threshold))

# Solve the ILP problem
model.solve()`

enter image description here
<p>I am struggling to select the correct cost_coefficient that is dependent on lpSum of the decision variables. I need to compare the lpSum(binary_option[option] for option in options) against the elements in threshold. The first element which is greater than lpSum(binary_option[option] for option in options), its corresponding cost coefficient (with the same index) should be used as the multiplier for the objective function. The following is what i have so far.</p>
<p>Please let me know how i can correctly formulate this.</p>
<pre><code>``from pulp import LpProblem, LpVariable, lpSum, LpMinimize

# Define the options
options = ['A', 'B', 'C', 'D', 'E', 'F']

# Define the thresholdsand their associated cost coefficients
cost_coefficient = [2, 6, 8, 11, 16, 20, 34]
threshold = [1, 2, 3, 4, 5, 6, 7]

# Create a PuLP ILP problem
model = LpProblem("Minimize_Cost", LpMinimize)

# Decision Variables: binary variables for each option
binary_option = {option: LpVariable(f"{option}", cat='Binary') for option in options}

# Decision Variables: binary variables for each cost coefficient
cost_coefficient_selection = []
for i in range(len(threshold)):
var_name = f"cost_sel_{i}"
cost_coefficient_selection.append(LpVariable(var_name, cat='Binary'))

# Total requirement is the sum of selected options
requirement = lpSum(binary_option[option] for option in options)


# Constraints: coefficient ==1 for first threshold that is greater than requirement
for i, t in enumerate(threshold):
model += requirement >= t, f"Threshold_{i}"

# Objective: Minimize the total cost
model += lpSum(requierment * cost_coefficient_selection *cost_coefficient )for i in range(len(threshold))

# Solve the ILP problem
model.solve()`
</code></pre>
<p><a href="https://i.sstatic.net/g2nlGGIz.png" rel="nofollow noreferrer">enter image description here</a></p>
 

Latest posts

Top