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

CPLEX护士排班模型参数定义求助:新手优化建模问题

Hey there! Let's work through your CPLEX parameter setup issue—since you're a new programmer building a nurse scheduling model, getting these foundational parameters right is crucial for locking in that initial feasible solution.

First off, I noticed a key mistake in your original code: you used dvar (decision variables) for parameters that should be fixed input data. In CPLEX OPL, parameters are defined with param, not dvar—dvar is for variables you want the solver to optimize. That's probably why your initial setup wasn't working!

Let's break down two common ways to define these parameters with dummy data: directly in your model file, or using a separate data file (great for scaling later).


1. Define Dummy Data Directly in the Model File (.mod)

This is perfect for quick testing since everything is in one place. Here's how to structure it correctly:

// First, define your sets with dummy values
int n = 5; // Number of nurses (dummy count)
range nurses = 1..n;
{string} roles = {"Surgeon", "Anesthesiologist", "Scrub Nurse"}; // Dummy role set
int c = 4; // Number of surgery cases (dummy count)
range cases = 1..c;
int cc = 3; // Number of overlapping cases (dummy count)
range overlapcases = 1..cc;

// P10: Nurse i can handle role k for case c (1=yes, 0=no)
// Option 1: Manual dummy assignments
param P10{nurses, roles, cases} binary :=
    [1, "Surgeon", 1] 1    [1, "Surgeon", 2] 1    [1, "Surgeon", 3] 0    [1, "Surgeon", 4] 1
    [1, "Anesthesiologist", 1] 1    [1, "Anesthesiologist", 2] 0    [1, "Anesthesiologist", 3] 1    [1, "Anesthesiologist", 4] 1
    [1, "Scrub Nurse", 1] 1    [1, "Scrub Nurse", 2] 1    [1, "Scrub Nurse", 3] 1    [1, "Scrub Nurse", 4] 1
    // Add rows for nurses 2-5 as needed
;

// Option 2: Generate random dummy data (faster for large sets)
// param P10{nurses, roles, cases} binary := rand(2); // Spits out 0 or 1 randomly

// ST & ET: Start/end times for each case (dummy hourly values)
param ST{cases} integer := 8 + (ord(cases)-1)*1; // Cases start at 8,9,10,11 AM
param ET{cases} integer := ST[c] + 3; // Each case lasts 3 hours

// P11: Case c overlaps with overlapping case cc (1=yes, 0=no)
// Assuming overlapcases maps to cases (e.g., overlapcase 1 = case 1, etc.)
param P11{cases, overlapcases} binary :=
    if (ST[c] < ET[cc] && ST[cc] < ET[c]) then 1 else 0;

2. Use a Separate Data File (.dat)

If you plan to swap in real data later, splitting model structure and data is cleaner. Here's how to set it up:

Model File (.mod)

// Define sets (no dummy values here—we'll assign them in the data file)
int n;
range nurses = 1..n;
{string} roles;
int c;
range cases = 1..c;
int cc;
range overlapcases = 1..cc;

// Declare parameter structures (no assignments yet)
param P10{nurses, roles, cases} binary;
param ST{cases} integer;
param ET{cases} integer;
param P11{cases, overlapcases} binary;

Data File (.dat)

// Assign dummy values to sets
n = 5;
roles = {"Surgeon", "Anesthesiologist", "Scrub Nurse"};
c = 4;
cc = 3;

// Assign dummy values to parameters
P10 :=
    1 "Surgeon" 1 1
    1 "Surgeon" 2 1
    1 "Surgeon" 3 0
    1 "Surgeon" 4 1
    1 "Anesthesiologist" 1 1
    1 "Anesthesiologist" 2 0
    // Add remaining rows for all nurse-role-case combinations
;

ST := 1 8, 2 9, 3 10, 4 11;
ET := 1 11, 2 12, 3 13, 4 14;

P11 :=
    1 1 0    1 2 1    1 3 0
    2 1 1    2 2 0    2 3 1
    3 1 0    3 2 1    3 3 0
    4 1 0    4 2 0    4 3 0
;

Key Notes to Remember

  • param vs dvar: Always use param for input data (like your P10, ST, etc.)—dvar is only for variables the solver will adjust to optimize your objective.
  • Binary Parameters: Using binary instead of boolean is standard in OPL for 0/1 parameters, but both work.
  • Overlap Logic: For P11, if overlapcases isn't a direct subset of cases, you'll need an extra parameter to map each overlapcase to its corresponding case (e.g., param overlap_to_case{overlapcases} in cases;), then adjust the overlap check to use overlap_to_case[cc].

内容的提问来源于stack exchange,提问作者L. Meisner

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:22:59