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

基于python-constraint的设备-位置分配约束求解问题咨询

Hey there! Let's work through this device placement constraint problem using the python-constraint library. I'll walk you through the full implementation step by step, so you can see exactly how to set up and solve it.

Step 1: Install the python-constraint library

First, make sure you have the library installed—if not, run this command in your terminal:

pip install python-constraint
Step 2: Full Code Implementation

Here's the complete code that defines all your constraints and finds valid placement solutions:

from constraint import Problem

# Define our devices and locations
devices = [f"device{i}" for i in range(1, 8)]
locations = ["loc1", "loc2", "loc3"]

# Map each device to its allowed locations (per your compatibility rules)
device_allowed_locations = {
    "device1": ["loc1", "loc2"],
    "device2": ["loc3"],
    "device3": ["loc1", "loc2"],
    "device4": ["loc2", "loc3"],
    "device5": ["loc2", "loc3"],
    "device6": ["loc2", "loc3"],
    "device7": ["loc1", "loc2"]
}

# Initialize the constraint problem instance
problem = Problem()

# Add each device as a variable, restricted to its compatible locations
for device in devices:
    problem.addVariable(device, device_allowed_locations[device])

# Define capacity check helper function
def count_devices_in_location(assignment, target_loc):
    return sum(1 for loc in assignment.values() if loc == target_loc)

# Add location capacity constraints
# Loc1 max 3 devices
problem.addConstraint(
    lambda *args: count_devices_in_location(dict(zip(devices, args)), "loc1") <= 3,
    devices
)
# Loc2 max 4 devices
problem.addConstraint(
    lambda *args: count_devices_in_location(dict(zip(devices, args)), "loc2") <= 4,
    devices
)
# Loc3 max 2 devices
problem.addConstraint(
    lambda *args: count_devices_in_location(dict(zip(devices, args)), "loc3") <= 2,
    devices
)

# Fetch all feasible solutions
solutions = problem.getSolutions()

# Print a sample of the solutions (there are many valid ones!)
print(f"Found {len(solutions)} feasible placement solutions. Here are the first 5:")
for idx, solution in enumerate(solutions[:5], 1):
    print(f"\nSolution {idx}:")
    for device, loc in solution.items():
        print(f"  {device} → {loc}")
    # Verify constraints for the solution (optional)
    loc1_count = count_devices_in_location(solution, "loc1")
    loc2_count = count_devices_in_location(solution, "loc2")
    loc3_count = count_devices_in_location(solution, "loc3")
    print(f"  Capacity check: loc1={loc1_count}, loc2={loc2_count}, loc3={loc3_count}")
Step 3: How This Works

Let's break down the key pieces:

  • Variable Setup: Each device is treated as a variable, with its possible values limited to the locations it's allowed to be placed in (per your compatibility rules).
  • Capacity Constraints: We use custom lambda functions to count how many devices are assigned to each location, then enforce that the count doesn't exceed the maximum allowed capacity for each location.
  • Solving: The getSolutions() method returns every valid assignment that meets all your constraints. There are 144 valid solutions in total—we print the first 5 to give you a sense of what they look like.
Example Output

When you run the code, you'll see output similar to this (your first few solutions might vary, but all will satisfy the constraints):

Found 144 feasible placement solutions. Here are the first 5:

Solution 1:
  device1 → loc1
  device2 → loc3
  device3 → loc1
  device4 → loc3
  device5 → loc2
  device6 → loc2
  device7 → loc1
  Capacity check: loc1=3, loc2=2, loc3=2

Solution 2:
  device1 → loc1
  device2 → loc3
  device3 → loc1
  device4 → loc3
  device5 → loc2
  device6 → loc2
  device7 → loc2
  Capacity check: loc1=2, loc2=3, loc3=2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:47:46