147 lines
4.6 KiB
Python
147 lines
4.6 KiB
Python
"""
|
|
Simple test to verify CBC solver functionality
|
|
"""
|
|
|
|
import pulp
|
|
import sys
|
|
import subprocess
|
|
import os
|
|
|
|
print("=== PuLP and CBC Solver Test ===")
|
|
print(f"Python version: {sys.version}")
|
|
print(f"PuLP version: {pulp.__version__}")
|
|
|
|
# Test 1: Check PuLP installation
|
|
print("\n1. Checking PuLP installation...")
|
|
try:
|
|
from pulp import LpProblem, LpVariable, LpMinimize, LpMaximize, lpSum, value
|
|
|
|
print("[OK] PuLP imported successfully")
|
|
except ImportError as e:
|
|
print(f"[FAIL] PuLP import failed: {e}")
|
|
sys.exit(1)
|
|
|
|
# Test 2: Check CBC solver file existence
|
|
print("\n2. Checking CBC solver file...")
|
|
solver_dir = os.path.join(
|
|
os.path.dirname(pulp.__file__), "apis", "..", "solverdir", "cbc", "win", "i64"
|
|
)
|
|
solver_path = os.path.join(solver_dir, "cbc.exe")
|
|
|
|
print(f"Looking for CBC at: {solver_path}")
|
|
if os.path.exists(solver_path):
|
|
print(f"[OK] CBC solver file found")
|
|
file_size = os.path.getsize(solver_path)
|
|
print(f" File size: {file_size:,} bytes ({file_size / 1024 / 1024:.2f} MB)")
|
|
else:
|
|
print(f"[FAIL] CBC solver file not found")
|
|
print(f" Checking directory contents:")
|
|
try:
|
|
parent_dir = os.path.dirname(solver_path)
|
|
if os.path.exists(parent_dir):
|
|
for item in os.listdir(parent_dir):
|
|
print(f" - {item}")
|
|
else:
|
|
print(f" Directory does not exist: {parent_dir}")
|
|
except Exception as e:
|
|
print(f" Error listing directory: {e}")
|
|
|
|
# Test 3: Try to run CBC solver directly
|
|
print("\n3. Testing CBC solver execution...")
|
|
if os.path.exists(solver_path):
|
|
try:
|
|
result = subprocess.run(
|
|
[solver_path, "-version"],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=10,
|
|
check=True,
|
|
)
|
|
print("[OK] CBC solver executed successfully")
|
|
print(f" Output: {result.stdout[:200]}")
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"[FAIL] CBC solver execution failed (exit code {e.returncode})")
|
|
print(f" stdout: {e.stdout[:200]}")
|
|
print(f" stderr: {e.stderr[:200]}")
|
|
except subprocess.TimeoutExpired:
|
|
print("[FAIL] CBC solver execution timed out")
|
|
except Exception as e:
|
|
print(f"[FAIL] CBC solver execution error: {e}")
|
|
else:
|
|
print("[FAIL] Cannot test CBC execution - file not found")
|
|
|
|
# Test 4: Solve a simple linear programming problem
|
|
print("\n4. Testing simple LP problem...")
|
|
try:
|
|
# Simple problem: minimize x + y subject to x + y >= 5, x >= 0, y >= 0
|
|
prob = LpProblem("Simple_LP_Test", LpMinimize)
|
|
|
|
x = LpVariable("x", lowBound=0, cat="Continuous")
|
|
y = LpVariable("y", lowBound=0, cat="Continuous")
|
|
|
|
prob += x + y # Objective: minimize x + y
|
|
prob += x + y >= 5 # Constraint
|
|
|
|
print(" Created simple LP problem: minimize x + y subject to x + y >= 5")
|
|
|
|
# Try to solve with CBC
|
|
solver = pulp.PULP_CBC_CMD(msg=False, timeLimit=10)
|
|
print(" Attempting to solve with CBC...")
|
|
|
|
status = prob.solve(solver)
|
|
|
|
print(f"[OK] LP problem solved")
|
|
print(f" Status: {pulp.LpStatus[prob.status]}")
|
|
print(f" Objective value: {value(prob.objective)}")
|
|
print(f" x = {value(x)}, y = {value(y)}")
|
|
|
|
if abs(value(prob.objective) - 5.0) < 0.01:
|
|
print(" [OK] Correct solution found!")
|
|
else:
|
|
print(f" [FAIL] Unexpected solution (expected 5.0)")
|
|
|
|
except Exception as e:
|
|
print(f"[FAIL] LP problem solving failed: {e}")
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
# Test 5: Solve a simple mixed integer programming problem
|
|
print("\n5. Testing simple MIP problem...")
|
|
try:
|
|
# Simple MIP: minimize x + y subject to x + y >= 5, x, y integers >= 0
|
|
prob = LpProblem("Simple_MIP_Test", LpMinimize)
|
|
|
|
x = LpVariable("x", lowBound=0, cat="Integer")
|
|
y = LpVariable("y", lowBound=0, cat="Integer")
|
|
|
|
prob += x + y # Objective
|
|
prob += x + y >= 5 # Constraint
|
|
|
|
print(
|
|
" Created simple MIP problem: minimize x + y subject to x + y >= 5, x,y integers"
|
|
)
|
|
|
|
solver = pulp.PULP_CBC_CMD(msg=False, timeLimit=10)
|
|
print(" Attempting to solve with CBC...")
|
|
|
|
status = prob.solve(solver)
|
|
|
|
print(f"[OK] MIP problem solved")
|
|
print(f" Status: {pulp.LpStatus[prob.status]}")
|
|
print(f" Objective value: {value(prob.objective)}")
|
|
print(f" x = {value(x)}, y = {value(y)}")
|
|
|
|
if abs(value(prob.objective) - 5.0) < 0.01:
|
|
print(" [OK] Correct solution found!")
|
|
else:
|
|
print(f" [FAIL] Unexpected solution (expected 5.0)")
|
|
|
|
except Exception as e:
|
|
print(f"[FAIL] MIP problem solving failed: {e}")
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
print("\n=== Test Complete ===")
|