You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

机械工程0-1多目标优化问题:寻求合适的Python求解库

Your problem is a multi-objective binary integer linear programming problem—perfectly suited for several Python libraries. The right choice depends on whether you want heuristic Pareto front exploration or exact solutions:

Evolutionary Optimization Libraries

These use genetic algorithms and similar heuristics to quickly generate a set of trade-off solutions (Pareto-optimal points) between cost and lead time. Great for exploring multiple optimal scenarios:

Pymoo

Pymoo is a dedicated multi-objective optimization library with first-class support for binary variables and constraint handling. It includes variants of popular algorithms like NSGA-II adapted for discrete problems.

Here’s a quick implementation for your factory selection problem:

from pymoo.core.problem import Problem
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.operators.sampling.rnd import BinaryRandomSampling
from pymoo.operators.crossover.hux import HUX
from pymoo.operators.mutation.bitflip import BitflipMutation
from pymoo.optimize import minimize

# Your problem parameters
costs = [10, 15, 8, 12, 9, 11, 14, 7, 13, 10]
lead_times = [5, 3, 7, 4, 6, 2, 5, 8, 3, 4]
capacities = [20, 30, 15, 25, 22, 18, 35, 12, 28, 24]
required_capacity = 100

class FactorySelection(Problem):
    def __init__(self):
        super().__init__(n_var=10, n_obj=2, n_constr=1, xl=0, xu=1, type_var=int)
    
    def _evaluate(self, X, out, *args, **kwargs):
        # Minimize total cost and total lead time
        f1 = X @ costs
        f2 = X @ lead_times
        
        # Enforce minimum capacity constraint (negative value means satisfied)
        g1 = required_capacity - (X @ capacities)
        
        out["F"] = [f1, f2]
        out["G"] = [g1]

# Set up algorithm with binary-specific operators
algorithm = NSGA2(
    pop_size=40,
    sampling=BinaryRandomSampling(),
    crossover=HUX(prob=0.9),  # Uniform crossover for binary variables
    mutation=BitflipMutation(prob=1/10),
    eliminate_duplicates=True
)

# Run optimization
result = minimize(
    FactorySelection(),
    algorithm,
    ("n_gen", 100),
    verbose=True,
    seed=42
)

# Print Pareto solutions
print("Pareto-optimal factory selections (0=not selected, 1=selected):")
for sol in result.X:
    print(sol)
print("\nCorresponding cost and lead time pairs:")
for cost, lead in result.F:
    print(f"Cost: {cost:.0f}, Lead Time: {lead:.0f}")

DEAP

DEAP is a flexible framework for building custom evolutionary algorithms. You have full control over chromosome representation (binary, in your case), fitness functions, and selection mechanisms—ideal if you need to tweak the algorithm for your specific problem.

Mathematical Programming Libraries

These solvers find exact optimal solutions (or high-quality approximations) for linear problems. Perfect for your small problem size (10 variables) where exact results matter:

Pyomo + Solver (Gurobi/CPLEX/CBC)

Pyomo is a modeling language that lets you define your problem in plain Python, then interface with linear/integer solvers. For multi-objective problems:

  • Gurobi and CPLEX (commercial) support native multi-objective optimization and can generate Pareto fronts.
  • CBC (open-source) works well for single-objective problems; for multi-objective, you’ll need to use weighted sum approaches to explore trade-offs.

Example using weighted sum (adjust alpha to prioritize cost vs lead time):

import pyomo.environ as pyo

# Parameters
costs = [10,15,8,12,9,11,14,7,13,10]
lead_times = [5,3,7,4,6,2,5,8,3,4]
capacities = [20,30,15,25,22,18,35,12,28,24]
required_capacity = 100

# Model setup
model = pyo.ConcreteModel()
model.x = pyo.Var(range(10), domain=pyo.Binary)  # 0-1 variables for each factory

# Weighted objective: alpha*cost + (1-alpha)*lead_time (minimize)
alpha = 0.6  # Higher = prioritize cost; lower = prioritize lead time
model.obj = pyo.Objective(
    expr=alpha*sum(costs[i]*model.x[i] for i in range(10)) + 
         (1-alpha)*sum(lead_times[i]*model.x[i] for i in range(10)),
    sense=pyo.minimize
)

# Capacity constraint
model.capacity = pyo.Constraint(
    expr=sum(capacities[i]*model.x[i] for i in range(10)) >= required_capacity
)

# Solve (replace 'gurobi' with 'cbc' for open-source)
solver = pyo.SolverFactory('gurobi')
solver.solve(model)

# Output results
print("Selected factories:")
for i in range(10):
    if pyo.value(model.x[i]) == 1:
        print(f"Factory {i+1}")
print(f"\nTotal cost: {sum(costs[i]*pyo.value(model.x[i]) for i in range(10))}")
print(f"Total lead time: {sum(lead_times[i]*pyo.value(model.x[i]) for i in range(10))}")

Gurobi Python API

If you want direct access to the solver’s multi-objective features, the Gurobi Python API lets you define multiple objectives and generate Pareto-optimal solutions without a modeling layer.


内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:07:10