Feasibility: Constraint Satisfaction
Basic
Feasibility Mode
No Objective
Boolean Variables
PER
THE PROBLEM
A hospital needs to cover three daily shifts (Morning, Afternoon, Night) using a pool of four nurses. The rules are simple: every shift must have at least one nurse, and no nurse can work more than one shift per day. There is no score to maximize or cost to minimize — the goal is just to find any valid schedule that satisfies all constraints. This is a feasibility problem: omitting the objective clause tells DeciDB to return the first satisfying assignment it finds.
SAMPLE DATA
CREATE TABLE assignments (
nurse_id INTEGER, nurse_name VARCHAR,
shift_id INTEGER, shift_name VARCHAR
);
INSERT INTO assignments VALUES
(1, 'Alice', 1, 'Morning'),
(1, 'Alice', 2, 'Afternoon'),
(1, 'Alice', 3, 'Night'),
(2, 'Bob', 1, 'Morning'),
(2, 'Bob', 2, 'Afternoon'),
(2, 'Bob', 3, 'Night'),
(3, 'Carol', 1, 'Morning'),
(3, 'Carol', 2, 'Afternoon'),
(4, 'Dan', 2, 'Afternoon'),
(4, 'Dan', 3, 'Night');
THE QUERY
SELECT nurse_name, shift_name, assigned
FROM assignments
DECIDE assigned IS BOOLEAN
SUCH THAT
SUM(assigned) >= 1 PER shift_id AND
SUM(assigned) <= 1 PER nurse_id;
QUERY BREAKDOWN
1
DECIDE assigned IS BOOLEAN — One binary variable per row. Each row represents one nurse–shift pair; assigned=1 means "this nurse works this shift".
2
SUM(assigned) >= 1 PER shift_id — For each distinct shift, at least one nurse must be assigned. The PER keyword generates one copy of this constraint per distinct value of shift_id — no hardcoded shift IDs needed.
3
SUM(assigned) <= 1 PER nurse_id — Each nurse works at most one shift. Again, one constraint per distinct nurse ID, derived from the data.
4
No
MAXIMIZE or MINIMIZE — This is a feasibility query. DeciDB finds any valid assignment and returns it. If no assignment satisfies all constraints, the query reports infeasibility.RESULT
| nurse_name | shift_name | assigned |
|---|---|---|
| Alice | Morning | 1 |
| Alice | Afternoon | 0 |
| Alice | Night | 0 |
| Bob | Morning | 0 |
| Bob | Afternoon | 0 |
| Bob | Night | 1 |
| Carol | Morning | 0 |
| Carol | Afternoon | 1 |
| Dan | Afternoon | 0 |
| Dan | Night | 0 |
INTERPRETATION
DeciDB found a valid schedule: Alice covers Morning, Carol covers Afternoon, and Bob covers Night. Dan is not assigned. Every shift has exactly one nurse (≥1 PER shift), and no nurse works more than one shift (≤1 PER nurse). Because there is no objective, DeciDB returns the first feasible assignment it finds — not the "best" one. To optimize among valid schedules (e.g., maximize total seniority), simply add a
MAXIMIZE clause.