0

I have a problem, where I have to find the optimal cost of 3 given motor.

Motor 1 has a range of 100 - 300 Motor 2 has a range of 400 - 1000 Motor 3 has a range of 50 - 250

They have a target value of 600

Motor 1 price is 5000 Motor 2 price is 5500 Motor 3 price is 5250

The equation looks like this:

Cost = Motor1 * 5000 + Motor2 * 5500 + Motor3 * 5250.

And a very important part, NOT every motor needs to run.

I have a python code, that can calculate it, but I can give it to it that not every motors needs to be inclued. Here is the code:

from pulp import LpProblem, LpVariable, LpMinimize

def find_lowest_cost():
    # Define the problem
    problem = LpProblem("Motor Optimization", LpMinimize)

    # Define the decision variables
    x = LpVariable("Motor1", lowBound=100, cat='Integer')  # Power of motor 1
    y = LpVariable("Motor2", lowBound=0, cat='Integer')  # Power of motor 2
    z = LpVariable("Motor3", lowBound=50, cat='Integer')  # Power of motor 3

    # Define the objective function (cost)
    problem += x * 5000 + y * 5500 + z * 5250

    # Define the constraints
    problem += x >= 100  # Motor 1 lower bound
    problem += x <= 300   # Motor 1 upper bound
    problem += y >= 350  # Motor 2 lower bound
    problem += y <= 1000  # Motor 2 upper bound
    problem += z >= 50  # Motor 3 lower bound
    problem += z <= 250  # Motor 3 upper bound
    problem += x + y + z == 500  # Total power constraint

    # Solve the problem
    problem.solve()

    # Retrieve the optimal solution
    lowest_cost = problem.objective.value()
    best_combination = (x.value(), y.value(), z.value())
    return lowest_cost, best_combination

cost, combination = find_lowest_cost()
print("Lowest cost:", cost)
print("Motor combination:", combination)

I tried to add 'or' to the "Define the Constraints' part, but it did not help

    problem += x >= 100 or x ==0 # Motor 1 lower bound
    problem += x <= 300  # Motor 1 upper bound
    problem += y >= 350 or y == 0 # Motor 2 lower bound
    problem += y <= 1000  # Motor 2 upper bound
    problem += z >= 50 or z == 0 # Motor 3 lower bound
    problem += z <= 250  # Motor 3 upper bound
    problem += x + y + z == 500  # Total power constraint

So my Questions is, how to implement that 'OR' into my code.

Thank you in advance

Miguff
  • 45
  • 5

1 Answers1

0

I make some assumptions:

  • Continuous power of motors, not integral
  • Observe the minima in your variable bounds, not the redundant and inconsistent constraints added later
  • Use 500 as a target, not 600

You need binary selection variables, like this:

from pulp import LpProblem, LpVariable, LpMinimize, LpContinuous, lpDot, LpBinary, lpSum

powers = (
    LpVariable('Motor1', cat=LpContinuous, upBound=300),
    LpVariable('Motor2', cat=LpContinuous, upBound=1000),
    LpVariable('Motor3', cat=LpContinuous, upBound=250),
)
used = LpVariable.matrix(name='MotorUsed', cat=LpBinary, indices=range(len(powers)))

problem = LpProblem(name='Motor_Optimization', sense=LpMinimize)
problem.objective = lpDot(powers, (5000, 5500, 5250))

problem.addConstraint(name='target', constraint=lpSum(powers) == 500)

for power, power_min, use in zip(
    powers,
    (100, 0, 50),
    used,
):
    problem.addConstraint(power >= power_min*used)
    problem.addConstraint(power <= 1000*used)

problem.solve()
combination = [p.value() for p in powers]
print('Lowest cost:', problem.objective.value())
print('Motor combination:', combination)
Result - Optimal solution found

Objective value:                2550000.00000000
Enumerated nodes:               0
Total iterations:               0
Time (CPU seconds):             0.01
Time (Wallclock seconds):       0.01

Option for printingOptions changed from normal to all
Total time (CPU seconds):       0.01   (Wallclock seconds):       0.01

Lowest cost: 2550000.0
Motor combination: [300.0, 0.0, 200.0]
Reinderien
  • 11,755
  • 5
  • 49
  • 77