assignment-user-matcher 1.8.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +240 -12
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -1
- package/dist/matcher.class.d.ts +44 -2
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +393 -4
- package/dist/queries/pagination.d.ts +5 -1
- package/dist/queries/pagination.d.ts.map +1 -1
- package/dist/queries/pagination.js +14 -3
- package/dist/schedule/policy.d.ts +35 -0
- package/dist/schedule/policy.d.ts.map +1 -0
- package/dist/schedule/policy.js +62 -0
- package/dist/scheduling/constraints/availability.d.ts +44 -0
- package/dist/scheduling/constraints/availability.d.ts.map +1 -0
- package/dist/scheduling/constraints/availability.js +142 -0
- package/dist/scheduling/constraints/consecutive.d.ts +25 -0
- package/dist/scheduling/constraints/consecutive.d.ts.map +1 -0
- package/dist/scheduling/constraints/consecutive.js +215 -0
- package/dist/scheduling/constraints/constraint.d.ts +35 -0
- package/dist/scheduling/constraints/constraint.d.ts.map +1 -0
- package/dist/scheduling/constraints/constraint.js +97 -0
- package/dist/scheduling/constraints/contract.d.ts +21 -0
- package/dist/scheduling/constraints/contract.d.ts.map +1 -0
- package/dist/scheduling/constraints/contract.js +106 -0
- package/dist/scheduling/constraints/cost.d.ts +18 -0
- package/dist/scheduling/constraints/cost.d.ts.map +1 -0
- package/dist/scheduling/constraints/cost.js +65 -0
- package/dist/scheduling/constraints/daily-rest.d.ts +22 -0
- package/dist/scheduling/constraints/daily-rest.d.ts.map +1 -0
- package/dist/scheduling/constraints/daily-rest.js +118 -0
- package/dist/scheduling/constraints/duty-quota.d.ts +20 -0
- package/dist/scheduling/constraints/duty-quota.d.ts.map +1 -0
- package/dist/scheduling/constraints/duty-quota.js +95 -0
- package/dist/scheduling/constraints/engagement-floor.d.ts +28 -0
- package/dist/scheduling/constraints/engagement-floor.d.ts.map +1 -0
- package/dist/scheduling/constraints/engagement-floor.js +79 -0
- package/dist/scheduling/constraints/fairness.d.ts +34 -0
- package/dist/scheduling/constraints/fairness.d.ts.map +1 -0
- package/dist/scheduling/constraints/fairness.js +149 -0
- package/dist/scheduling/constraints/group-composition.d.ts +19 -0
- package/dist/scheduling/constraints/group-composition.d.ts.map +1 -0
- package/dist/scheduling/constraints/group-composition.js +92 -0
- package/dist/scheduling/constraints/hour-budget.d.ts +13 -0
- package/dist/scheduling/constraints/hour-budget.d.ts.map +1 -0
- package/dist/scheduling/constraints/hour-budget.js +59 -0
- package/dist/scheduling/constraints/in-shift-breaks.d.ts +24 -0
- package/dist/scheduling/constraints/in-shift-breaks.d.ts.map +1 -0
- package/dist/scheduling/constraints/in-shift-breaks.js +97 -0
- package/dist/scheduling/constraints/intervals.d.ts +5 -0
- package/dist/scheduling/constraints/intervals.d.ts.map +1 -0
- package/dist/scheduling/constraints/intervals.js +17 -0
- package/dist/scheduling/constraints/max-shift-duration.d.ts +7 -0
- package/dist/scheduling/constraints/max-shift-duration.d.ts.map +1 -0
- package/dist/scheduling/constraints/max-shift-duration.js +38 -0
- package/dist/scheduling/constraints/min-rest.d.ts +22 -0
- package/dist/scheduling/constraints/min-rest.d.ts.map +1 -0
- package/dist/scheduling/constraints/min-rest.js +60 -0
- package/dist/scheduling/constraints/min-staffing.d.ts +14 -0
- package/dist/scheduling/constraints/min-staffing.d.ts.map +1 -0
- package/dist/scheduling/constraints/min-staffing.js +59 -0
- package/dist/scheduling/constraints/night-work.d.ts +24 -0
- package/dist/scheduling/constraints/night-work.d.ts.map +1 -0
- package/dist/scheduling/constraints/night-work.js +163 -0
- package/dist/scheduling/constraints/no-overlap.d.ts +7 -0
- package/dist/scheduling/constraints/no-overlap.d.ts.map +1 -0
- package/dist/scheduling/constraints/no-overlap.js +36 -0
- package/dist/scheduling/constraints/notice.d.ts +44 -0
- package/dist/scheduling/constraints/notice.d.ts.map +1 -0
- package/dist/scheduling/constraints/notice.js +140 -0
- package/dist/scheduling/constraints/one-shift-per-day.d.ts +7 -0
- package/dist/scheduling/constraints/one-shift-per-day.d.ts.map +1 -0
- package/dist/scheduling/constraints/one-shift-per-day.js +35 -0
- package/dist/scheduling/constraints/overtime.d.ts +47 -0
- package/dist/scheduling/constraints/overtime.d.ts.map +1 -0
- package/dist/scheduling/constraints/overtime.js +149 -0
- package/dist/scheduling/constraints/protections.d.ts +21 -0
- package/dist/scheduling/constraints/protections.d.ts.map +1 -0
- package/dist/scheduling/constraints/protections.js +93 -0
- package/dist/scheduling/constraints/qualification.d.ts +18 -0
- package/dist/scheduling/constraints/qualification.d.ts.map +1 -0
- package/dist/scheduling/constraints/qualification.js +78 -0
- package/dist/scheduling/constraints/rest-days.d.ts +30 -0
- package/dist/scheduling/constraints/rest-days.d.ts.map +1 -0
- package/dist/scheduling/constraints/rest-days.js +133 -0
- package/dist/scheduling/constraints/rolling-hours.d.ts +24 -0
- package/dist/scheduling/constraints/rolling-hours.d.ts.map +1 -0
- package/dist/scheduling/constraints/rolling-hours.js +147 -0
- package/dist/scheduling/constraints/start-interval.d.ts +20 -0
- package/dist/scheduling/constraints/start-interval.d.ts.map +1 -0
- package/dist/scheduling/constraints/start-interval.js +53 -0
- package/dist/scheduling/constraints/support.d.ts +48 -0
- package/dist/scheduling/constraints/support.d.ts.map +1 -0
- package/dist/scheduling/constraints/support.js +97 -0
- package/dist/scheduling/constraints/time-off.d.ts +7 -0
- package/dist/scheduling/constraints/time-off.d.ts.map +1 -0
- package/dist/scheduling/constraints/time-off.js +42 -0
- package/dist/scheduling/constraints/weekly-rest.d.ts +22 -0
- package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -0
- package/dist/scheduling/constraints/weekly-rest.js +92 -0
- package/dist/scheduling/cost.d.ts +56 -0
- package/dist/scheduling/cost.d.ts.map +1 -0
- package/dist/scheduling/cost.js +120 -0
- package/dist/scheduling/engine/construction.d.ts +22 -0
- package/dist/scheduling/engine/construction.d.ts.map +1 -0
- package/dist/scheduling/engine/construction.js +97 -0
- package/dist/scheduling/engine/explain.d.ts +12 -0
- package/dist/scheduling/engine/explain.d.ts.map +1 -0
- package/dist/scheduling/engine/explain.js +24 -0
- package/dist/scheduling/engine/objective.d.ts +73 -0
- package/dist/scheduling/engine/objective.d.ts.map +1 -0
- package/dist/scheduling/engine/objective.js +164 -0
- package/dist/scheduling/engine/prng.d.ts +5 -0
- package/dist/scheduling/engine/prng.d.ts.map +1 -0
- package/dist/scheduling/engine/prng.js +24 -0
- package/dist/scheduling/engine/propagation.d.ts +19 -0
- package/dist/scheduling/engine/propagation.d.ts.map +1 -0
- package/dist/scheduling/engine/propagation.js +40 -0
- package/dist/scheduling/engine/search.d.ts +50 -0
- package/dist/scheduling/engine/search.d.ts.map +1 -0
- package/dist/scheduling/engine/search.js +127 -0
- package/dist/scheduling/engine/state.d.ts +28 -0
- package/dist/scheduling/engine/state.d.ts.map +1 -0
- package/dist/scheduling/engine/state.js +96 -0
- package/dist/scheduling/engine/timeline.d.ts +179 -0
- package/dist/scheduling/engine/timeline.d.ts.map +1 -0
- package/dist/scheduling/engine/timeline.js +416 -0
- package/dist/scheduling/engine/verdicts.d.ts +18 -0
- package/dist/scheduling/engine/verdicts.d.ts.map +1 -0
- package/dist/scheduling/engine/verdicts.js +74 -0
- package/dist/scheduling/index.d.ts +10 -0
- package/dist/scheduling/index.d.ts.map +1 -0
- package/dist/scheduling/index.js +29 -0
- package/dist/scheduling/model.d.ts +53 -0
- package/dist/scheduling/model.d.ts.map +1 -0
- package/dist/scheduling/model.js +379 -0
- package/dist/scheduling/operations.d.ts +134 -0
- package/dist/scheduling/operations.d.ts.map +1 -0
- package/dist/scheduling/operations.js +376 -0
- package/dist/scheduling/provenance.d.ts +33 -0
- package/dist/scheduling/provenance.d.ts.map +1 -0
- package/dist/scheduling/provenance.js +70 -0
- package/dist/scheduling/rules.d.ts +23 -0
- package/dist/scheduling/rules.d.ts.map +1 -0
- package/dist/scheduling/rules.js +37 -0
- package/dist/scheduling/scheduler.class.d.ts +24 -0
- package/dist/scheduling/scheduler.class.d.ts.map +1 -0
- package/dist/scheduling/scheduler.class.js +150 -0
- package/dist/scheduling/time.d.ts +132 -0
- package/dist/scheduling/time.d.ts.map +1 -0
- package/dist/scheduling/time.js +256 -0
- package/dist/scheduling/types.d.ts +932 -0
- package/dist/scheduling/types.d.ts.map +1 -0
- package/dist/scheduling/types.js +19 -0
- package/dist/scoring/match-score.d.ts.map +1 -1
- package/dist/scoring/match-score.js +1 -3
- package/dist/types/matcher.d.ts +195 -1
- package/dist/types/matcher.d.ts.map +1 -1
- package/dist/utils/keys.d.ts +3 -0
- package/dist/utils/keys.d.ts.map +1 -1
- package/dist/utils/keys.js +15 -0
- package/dist/validation/assignment-lint.d.ts +24 -0
- package/dist/validation/assignment-lint.d.ts.map +1 -0
- package/dist/validation/assignment-lint.js +132 -0
- package/package.json +4 -2
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Labour-cost arithmetic — pure logic, shared by the solve objective, the
|
|
4
|
+
* repair candidate ranking and the result's cost summary.
|
|
5
|
+
*
|
|
6
|
+
* One implementation on purpose: an objective that minimises a different cost
|
|
7
|
+
* than the ranking reports would steer the roster one way and justify it
|
|
8
|
+
* another. All money is minor units (cents), integral until the final rounding.
|
|
9
|
+
*
|
|
10
|
+
* What is priced, and what deliberately is not:
|
|
11
|
+
*
|
|
12
|
+
* - The *working* portion of a span is paid at the hourly rate, uplifted by
|
|
13
|
+
* whichever premium bands apply (night / Sunday / holiday), stacked
|
|
14
|
+
* additively or by maximum as the cost model says.
|
|
15
|
+
* - The non-working remainder of a *duty-classified* span — stand-by hours
|
|
16
|
+
* that occupy the clock without counting as work — is paid flat at
|
|
17
|
+
* `standbyRateFraction` of the rate (Estonia's *valveaeg* owes ≥ 1/10 of
|
|
18
|
+
* the agreed wage). No premiums apply to it; unset means unpaid.
|
|
19
|
+
* - Overtime is a per-employee surcharge on the minutes worked past
|
|
20
|
+
* `overtimeAfterMinutes`, at `(overtimeMultiplier − 1)` on top of the base
|
|
21
|
+
* already priced per shift. Kept separate from the per-shift price because
|
|
22
|
+
* which minutes are "the overtime ones" is an accounting fiction — the
|
|
23
|
+
* surcharge belongs to the person's total, not to any one shift.
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.rosterCost = exports.marginalCostCents = exports.overtimeSurchargeCents = exports.shiftCostCents = void 0;
|
|
27
|
+
const engagement_floor_1 = require("./constraints/engagement-floor");
|
|
28
|
+
/**
|
|
29
|
+
* Price of one shift for one cost model, excluding any overtime surcharge.
|
|
30
|
+
*
|
|
31
|
+
* Premium predicates are judged on the instance: `night` applies when any of
|
|
32
|
+
* the shift falls in the configured night band, `sunday`/`holiday` on the start
|
|
33
|
+
* day. Romania stacks premiums additively (overtime 75% + night 25% + holiday
|
|
34
|
+
* 100%); most jurisdictions apply only the largest, so `'max'` is the default —
|
|
35
|
+
* under-stating the Romanian bill rather than over-stating everyone else's.
|
|
36
|
+
*/
|
|
37
|
+
function shiftCostCents(inst, cost, engagement) {
|
|
38
|
+
if (!cost?.hourlyRateCents)
|
|
39
|
+
return 0;
|
|
40
|
+
const paid = (0, engagement_floor_1.paidMinutesFor)(inst, engagement);
|
|
41
|
+
const base = (paid / 60) * cost.hourlyRateCents;
|
|
42
|
+
const multipliers = [];
|
|
43
|
+
for (const premium of cost.premiums ?? []) {
|
|
44
|
+
const applies = (premium.predicate === 'night' && inst.nightMinutes > 0) ||
|
|
45
|
+
(premium.predicate === 'sunday' && inst.isSunday) ||
|
|
46
|
+
(premium.predicate === 'holiday' && inst.isPublicHoliday);
|
|
47
|
+
if (applies)
|
|
48
|
+
multipliers.push(premium.multiplier);
|
|
49
|
+
}
|
|
50
|
+
const uplift = multipliers.length === 0
|
|
51
|
+
? 1
|
|
52
|
+
: cost.stacking === 'add'
|
|
53
|
+
? 1 + multipliers.reduce((sum, m) => sum + (m - 1), 0)
|
|
54
|
+
: Math.max(...multipliers);
|
|
55
|
+
return Math.round(base * uplift + standbyAllowanceCents(inst, cost));
|
|
56
|
+
}
|
|
57
|
+
exports.shiftCostCents = shiftCostCents;
|
|
58
|
+
/**
|
|
59
|
+
* The stand-by allowance for the non-working remainder of a duty-classified
|
|
60
|
+
* span. Only duty-classified instances have one — for a plain shift the
|
|
61
|
+
* remainder is the unpaid break, which is unpaid by definition.
|
|
62
|
+
*/
|
|
63
|
+
function standbyAllowanceCents(inst, cost) {
|
|
64
|
+
if (!cost.standbyRateFraction || !inst.duty || inst.duty.countsAsWorkingTime === 'full')
|
|
65
|
+
return 0;
|
|
66
|
+
const standbyMinutes = Math.max(0, inst.durationMinutes - inst.workingMinutes);
|
|
67
|
+
return (standbyMinutes / 60) * cost.hourlyRateCents * cost.standbyRateFraction;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Surcharge owed on `workedMinutes` of total working time — the extra above
|
|
71
|
+
* base rate for the minutes past the overtime threshold. Base pay for those
|
|
72
|
+
* minutes is already in the per-shift price, so only the uplift is added here.
|
|
73
|
+
*/
|
|
74
|
+
function overtimeSurchargeCents(workedMinutes, cost) {
|
|
75
|
+
if (!cost?.hourlyRateCents || cost.overtimeAfterMinutes === undefined || !cost.overtimeMultiplier)
|
|
76
|
+
return 0;
|
|
77
|
+
const overtime = Math.max(0, workedMinutes - cost.overtimeAfterMinutes);
|
|
78
|
+
return Math.round((overtime / 60) * cost.hourlyRateCents * (cost.overtimeMultiplier - 1));
|
|
79
|
+
}
|
|
80
|
+
exports.overtimeSurchargeCents = overtimeSurchargeCents;
|
|
81
|
+
/**
|
|
82
|
+
* Marginal cost of adding one shift on top of `workedMinutes` already assigned:
|
|
83
|
+
* the shift's own price plus the overtime surcharge it newly triggers. This is
|
|
84
|
+
* the number a call-out decision needs — the same shift is dearer in the hands
|
|
85
|
+
* of someone already at their threshold.
|
|
86
|
+
*/
|
|
87
|
+
function marginalCostCents(inst, employee, workedMinutes, engagement) {
|
|
88
|
+
const cost = employee.cost;
|
|
89
|
+
if (!cost?.hourlyRateCents)
|
|
90
|
+
return 0;
|
|
91
|
+
return (shiftCostCents(inst, cost, engagement) +
|
|
92
|
+
overtimeSurchargeCents(workedMinutes + inst.workingMinutes, cost) -
|
|
93
|
+
overtimeSurchargeCents(workedMinutes, cost));
|
|
94
|
+
}
|
|
95
|
+
exports.marginalCostCents = marginalCostCents;
|
|
96
|
+
/**
|
|
97
|
+
* Whole-roster cost, per employee and total, or `undefined` when nobody has a
|
|
98
|
+
* cost model — so rosters without one keep a byte-identical result shape.
|
|
99
|
+
*/
|
|
100
|
+
function rosterCost(state) {
|
|
101
|
+
const ctx = state.ctx;
|
|
102
|
+
if (!ctx.employees.some((e) => e.cost?.hourlyRateCents))
|
|
103
|
+
return undefined;
|
|
104
|
+
const byEmployee = {};
|
|
105
|
+
let totalCents = 0;
|
|
106
|
+
for (const employee of ctx.employees) {
|
|
107
|
+
let cents = 0;
|
|
108
|
+
for (const instanceId of state.byEmployee.get(employee.id) ?? []) {
|
|
109
|
+
const inst = ctx.instanceById.get(instanceId);
|
|
110
|
+
if (inst)
|
|
111
|
+
cents += shiftCostCents(inst, employee.cost, ctx.rules.engagement);
|
|
112
|
+
}
|
|
113
|
+
cents += overtimeSurchargeCents(state.minutesByEmployee.get(employee.id) ?? 0, employee.cost);
|
|
114
|
+
if (cents > 0)
|
|
115
|
+
byEmployee[employee.id] = cents;
|
|
116
|
+
totalCents += cents;
|
|
117
|
+
}
|
|
118
|
+
return { totalCents, byEmployee };
|
|
119
|
+
}
|
|
120
|
+
exports.rosterCost = rosterCost;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Greedy construction — pure logic.
|
|
3
|
+
*
|
|
4
|
+
* Fills most-constrained shift instances first (fewest eligible employees),
|
|
5
|
+
* picking the best employee by objective delta with seeded tie-breaking. A
|
|
6
|
+
* pair is only ever assigned when every hard constraint's `delta` is 0, so a
|
|
7
|
+
* constructed state is always hard-compliant; shortfalls surface as unfilled
|
|
8
|
+
* slots, not breaches.
|
|
9
|
+
*/
|
|
10
|
+
import type { ModelContext } from '../types';
|
|
11
|
+
import type { PropagationResult } from './propagation';
|
|
12
|
+
import { type InternalState } from './state';
|
|
13
|
+
/** Whether assigning the pair keeps every hard constraint satisfied. */
|
|
14
|
+
export declare function hardCompliant(ctx: ModelContext, state: InternalState, employeeId: string, instanceId: string): boolean;
|
|
15
|
+
/** Instances ordered most-constrained-first: fewest eligible employees, then earliest start. */
|
|
16
|
+
export declare function constructionOrder(ctx: ModelContext, propagation: PropagationResult): string[];
|
|
17
|
+
/**
|
|
18
|
+
* Greedily staff `instanceIds` (default: everything, most-constrained first).
|
|
19
|
+
* Shared by initial construction and LNS repair.
|
|
20
|
+
*/
|
|
21
|
+
export declare function greedyFill(ctx: ModelContext, state: InternalState, propagation: PropagationResult, objective: 'standard' | 'balanced', rand: () => number, instanceIds?: string[]): void;
|
|
22
|
+
//# sourceMappingURL=construction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"construction.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/construction.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAU,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAErD,wEAAwE;AACxE,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAKtH;AA8CD,gGAAgG;AAChG,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,YAAY,EAAE,WAAW,EAAE,iBAAiB,GAAG,MAAM,EAAE,CAS7F;AAED;;;GAGG;AACH,wBAAgB,UAAU,CACtB,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,aAAa,EACpB,WAAW,EAAE,iBAAiB,EAC9B,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,IAAI,EAAE,MAAM,MAAM,EAClB,WAAW,CAAC,EAAE,MAAM,EAAE,GACvB,IAAI,CAsBN"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Greedy construction — pure logic.
|
|
4
|
+
*
|
|
5
|
+
* Fills most-constrained shift instances first (fewest eligible employees),
|
|
6
|
+
* picking the best employee by objective delta with seeded tie-breaking. A
|
|
7
|
+
* pair is only ever assigned when every hard constraint's `delta` is 0, so a
|
|
8
|
+
* constructed state is always hard-compliant; shortfalls surface as unfilled
|
|
9
|
+
* slots, not breaches.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.greedyFill = exports.constructionOrder = exports.hardCompliant = void 0;
|
|
13
|
+
const state_1 = require("./state");
|
|
14
|
+
/** Whether assigning the pair keeps every hard constraint satisfied. */
|
|
15
|
+
function hardCompliant(ctx, state, employeeId, instanceId) {
|
|
16
|
+
for (const c of ctx.constraints) {
|
|
17
|
+
if (c.hardness === 'hard' && c.delta(state, { employeeId, shiftInstanceId: instanceId }) > 0)
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
exports.hardCompliant = hardCompliant;
|
|
23
|
+
function rankCandidate(ctx, state, instanceId, employeeId, objective, rand) {
|
|
24
|
+
const reasons = [];
|
|
25
|
+
let rank = 0;
|
|
26
|
+
// Prefer candidates that close an unmet tag requirement.
|
|
27
|
+
const inst = ctx.instanceById.get(instanceId);
|
|
28
|
+
if (inst) {
|
|
29
|
+
const assigned = state.assignments.get(instanceId);
|
|
30
|
+
for (const [tag, needed] of Object.entries(inst.tagRequirements)) {
|
|
31
|
+
let have = 0;
|
|
32
|
+
if (assigned)
|
|
33
|
+
for (const e of assigned)
|
|
34
|
+
if (ctx.employeeTags.get(e)?.has(tag))
|
|
35
|
+
have++;
|
|
36
|
+
if (have < needed && ctx.employeeTags.get(employeeId)?.has(tag)) {
|
|
37
|
+
rank -= 1000;
|
|
38
|
+
reasons.push(`fills tag requirement "${tag}"`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (objective === 'balanced') {
|
|
43
|
+
// Less-loaded employees first so hours equalize.
|
|
44
|
+
rank += (state.minutesByEmployee.get(employeeId) ?? 0) / 60;
|
|
45
|
+
reasons.push('selected to balance hours');
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
reasons.push('eligible under all constraints');
|
|
49
|
+
}
|
|
50
|
+
// Seeded jitter for deterministic tie-breaking.
|
|
51
|
+
rank += rand();
|
|
52
|
+
return { employeeId, rank, reasons };
|
|
53
|
+
}
|
|
54
|
+
/** Instances ordered most-constrained-first: fewest eligible employees, then earliest start. */
|
|
55
|
+
function constructionOrder(ctx, propagation) {
|
|
56
|
+
return ctx.instances
|
|
57
|
+
.map((i) => i.id)
|
|
58
|
+
.sort((a, b) => {
|
|
59
|
+
const ea = propagation.eligibleByInstance.get(a)?.length ?? 0;
|
|
60
|
+
const eb = propagation.eligibleByInstance.get(b)?.length ?? 0;
|
|
61
|
+
if (ea !== eb)
|
|
62
|
+
return ea - eb;
|
|
63
|
+
return ctx.instanceById.get(a).startMinute - ctx.instanceById.get(b).startMinute;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
exports.constructionOrder = constructionOrder;
|
|
67
|
+
/**
|
|
68
|
+
* Greedily staff `instanceIds` (default: everything, most-constrained first).
|
|
69
|
+
* Shared by initial construction and LNS repair.
|
|
70
|
+
*/
|
|
71
|
+
function greedyFill(ctx, state, propagation, objective, rand, instanceIds) {
|
|
72
|
+
const order = instanceIds ?? constructionOrder(ctx, propagation);
|
|
73
|
+
for (const instanceId of order) {
|
|
74
|
+
const inst = ctx.instanceById.get(instanceId);
|
|
75
|
+
if (!inst)
|
|
76
|
+
continue;
|
|
77
|
+
let need = inst.minEmployees - (state.assignments.get(instanceId)?.size ?? 0);
|
|
78
|
+
if (need <= 0)
|
|
79
|
+
continue;
|
|
80
|
+
const candidates = [];
|
|
81
|
+
for (const employeeId of propagation.eligibleByInstance.get(instanceId) ?? []) {
|
|
82
|
+
if (state.isAssigned(employeeId, instanceId))
|
|
83
|
+
continue;
|
|
84
|
+
if (!hardCompliant(ctx, state, employeeId, instanceId))
|
|
85
|
+
continue;
|
|
86
|
+
candidates.push(rankCandidate(ctx, state, instanceId, employeeId, objective, rand));
|
|
87
|
+
}
|
|
88
|
+
candidates.sort((a, b) => a.rank - b.rank || (a.employeeId < b.employeeId ? -1 : 1));
|
|
89
|
+
for (const c of candidates) {
|
|
90
|
+
if (need <= 0)
|
|
91
|
+
break;
|
|
92
|
+
(0, state_1.assign)(state, c.employeeId, instanceId, c.reasons);
|
|
93
|
+
need--;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.greedyFill = greedyFill;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Explain — per-assignment reason strings from constraint evaluations.
|
|
3
|
+
*
|
|
4
|
+
* Reasons combine what construction/repair recorded at selection time with a
|
|
5
|
+
* compliance confirmation per constraint (`explain` returning null means the
|
|
6
|
+
* pair satisfies the rule). Fits the repo's decision-trace philosophy: every
|
|
7
|
+
* rostered pair can say why it exists.
|
|
8
|
+
*/
|
|
9
|
+
import { type InternalState } from './state';
|
|
10
|
+
/** Reason strings for one assigned pair; empty only for a state built outside the engine. */
|
|
11
|
+
export declare function reasonsFor(state: InternalState, employeeId: string, instanceId: string): string[];
|
|
12
|
+
//# sourceMappingURL=explain.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"explain.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/explain.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAW,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AAEtD,6FAA6F;AAC7F,wBAAgB,UAAU,CAAC,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CASjG"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Explain — per-assignment reason strings from constraint evaluations.
|
|
4
|
+
*
|
|
5
|
+
* Reasons combine what construction/repair recorded at selection time with a
|
|
6
|
+
* compliance confirmation per constraint (`explain` returning null means the
|
|
7
|
+
* pair satisfies the rule). Fits the repo's decision-trace philosophy: every
|
|
8
|
+
* rostered pair can say why it exists.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.reasonsFor = void 0;
|
|
12
|
+
const state_1 = require("./state");
|
|
13
|
+
/** Reason strings for one assigned pair; empty only for a state built outside the engine. */
|
|
14
|
+
function reasonsFor(state, employeeId, instanceId) {
|
|
15
|
+
const recorded = state.reasons.get((0, state_1.pairKey)(employeeId, instanceId)) ?? [];
|
|
16
|
+
const out = [...recorded];
|
|
17
|
+
for (const c of state.ctx.constraints) {
|
|
18
|
+
if (c.explain(state, { employeeId, shiftInstanceId: instanceId }) === null) {
|
|
19
|
+
out.push(`satisfies ${c.id}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
exports.reasonsFor = reasonsFor;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Objective scoring — pure logic.
|
|
3
|
+
*
|
|
4
|
+
* The score has three lexicographic levels rather than one number:
|
|
5
|
+
*
|
|
6
|
+
* - **hard** — legal or physical impossibility (rest, overlap, expired
|
|
7
|
+
* qualification, contract breach);
|
|
8
|
+
* - **medium** — coverage: unfilled slots and unmet tag requirements;
|
|
9
|
+
* - **soft** — cost, fairness, preferences, hour-balance.
|
|
10
|
+
*
|
|
11
|
+
* They are compared in order, never summed. A weighted sum would let a large
|
|
12
|
+
* enough soft gain buy a hard breach — arrange the weights unluckily and the
|
|
13
|
+
* solver "discovers" that under-resting one person improves fairness for
|
|
14
|
+
* everyone else. Lexicographic ordering makes that trade unrepresentable
|
|
15
|
+
* instead of merely unlikely.
|
|
16
|
+
*
|
|
17
|
+
* Within a level, breaches contribute their *magnitude*, so a rest gap ten
|
|
18
|
+
* minutes short scores better than one six hours short. Local search needs that
|
|
19
|
+
* gradient to climb out of an infeasible region; a 0/1 flag leaves it on a
|
|
20
|
+
* plateau with nothing to follow.
|
|
21
|
+
*
|
|
22
|
+
* Aggregate constraints (`evaluate`) are deliberately not run per move — they
|
|
23
|
+
* are O(roster) and the search calls the objective in its innermost loop. The
|
|
24
|
+
* cheap aggregates that steer the search (coverage, fairness) have dedicated
|
|
25
|
+
* terms here; the full `evaluate` pass runs once when the result is assembled.
|
|
26
|
+
*/
|
|
27
|
+
import type { FairnessRule, ModelContext, SearchState } from '../types';
|
|
28
|
+
export declare const UNFILLED_WEIGHT = 10000;
|
|
29
|
+
export declare const TAG_SHORTFALL_WEIGHT = 5000;
|
|
30
|
+
export declare const MIN_HOURS_WEIGHT = 100;
|
|
31
|
+
/** Default weight applied to a soft constraint that does not declare one. */
|
|
32
|
+
export declare const DEFAULT_SOFT_WEIGHT = 1;
|
|
33
|
+
/** A three-level score. Lower is better at every level. */
|
|
34
|
+
export interface LexScore {
|
|
35
|
+
hard: number;
|
|
36
|
+
medium: number;
|
|
37
|
+
soft: number;
|
|
38
|
+
}
|
|
39
|
+
/** Negative when `a` is better than `b`, positive when worse, 0 when equal. */
|
|
40
|
+
export declare function compareScores(a: LexScore, b: LexScore): number;
|
|
41
|
+
export declare function isBetter(a: LexScore, b: LexScore): boolean;
|
|
42
|
+
/** Missing assignees for an instance (0 when fully staffed). */
|
|
43
|
+
export declare function slotShortage(ctx: ModelContext, state: SearchState, instanceId: string): number;
|
|
44
|
+
/** Missing tagged assignees, summed over the instance's tag requirements. */
|
|
45
|
+
export declare function tagShortage(ctx: ModelContext, state: SearchState, instanceId: string): number;
|
|
46
|
+
/** Total unfilled capacity across the period, in person-slots. */
|
|
47
|
+
export declare function unfilledSlots(ctx: ModelContext, state: SearchState): number;
|
|
48
|
+
/** Variance of worked minutes across employees (0 for an empty roster). */
|
|
49
|
+
export declare function hoursVariance(ctx: ModelContext, state: SearchState): number;
|
|
50
|
+
/** Number of employees below their soft min-hours bound. */
|
|
51
|
+
export declare function minHoursShortfalls(ctx: ModelContext, state: SearchState): number;
|
|
52
|
+
/**
|
|
53
|
+
* Weighted breach total per level, over the currently assigned pairs.
|
|
54
|
+
*
|
|
55
|
+
* This is what gives `SchedulingConstraint.weight` teeth — a caller who softens
|
|
56
|
+
* a built-in via `overrides`, or registers a custom rule, now moves the score
|
|
57
|
+
* instead of being silently ignored.
|
|
58
|
+
*/
|
|
59
|
+
export declare function breachTotals(ctx: ModelContext, state: SearchState): LexScore;
|
|
60
|
+
/** Backwards-compatible soft-breach total, for callers that want a single figure. */
|
|
61
|
+
export declare function softBreaches(ctx: ModelContext, state: SearchState): number;
|
|
62
|
+
/** The full three-level score. Lower is better at every level. */
|
|
63
|
+
export declare function scoreLex(ctx: ModelContext, state: SearchState, objective: 'standard' | 'balanced', minHoursWeight: number, fairnessRules?: FairnessRule[]): LexScore;
|
|
64
|
+
/**
|
|
65
|
+
* Flattened score, lower is better.
|
|
66
|
+
*
|
|
67
|
+
* Retained for callers that need one number. The level separation is preserved
|
|
68
|
+
* by scaling, but comparisons should prefer `compareScores` — a flattened score
|
|
69
|
+
* can only approximate a lexicographic order, and at extreme magnitudes the
|
|
70
|
+
* approximation leaks.
|
|
71
|
+
*/
|
|
72
|
+
export declare function score(ctx: ModelContext, state: SearchState, objective: 'standard' | 'balanced', minHoursWeight: number): number;
|
|
73
|
+
//# sourceMappingURL=objective.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objective.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/objective.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAIxE,eAAO,MAAM,eAAe,QAAS,CAAC;AACtC,eAAO,MAAM,oBAAoB,OAAQ,CAAC;AAC1C,eAAO,MAAM,gBAAgB,MAAM,CAAC;AACpC,6EAA6E;AAC7E,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC,2DAA2D;AAC3D,MAAM,WAAW,QAAQ;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,MAAM,CAI9D;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,QAAQ,GAAG,OAAO,CAE1D;AAED,gEAAgE;AAChE,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAI9F;AAED,6EAA6E;AAC7E,wBAAgB,WAAW,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAa7F;AAED,kEAAkE;AAClE,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,CAI3E;AAED,2EAA2E;AAC3E,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,CAK3E;AAED,4DAA4D;AAC5D,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,CAMhF;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,QAAQ,CAgB5E;AAED,qFAAqF;AACrF,wBAAgB,YAAY,CAAC,GAAG,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,CAE1E;AAED,kEAAkE;AAClE,wBAAgB,QAAQ,CACpB,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,cAAc,EAAE,MAAM,EACtB,aAAa,CAAC,EAAE,YAAY,EAAE,GAC/B,QAAQ,CAgBV;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CACjB,GAAG,EAAE,YAAY,EACjB,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,UAAU,GAAG,UAAU,EAClC,cAAc,EAAE,MAAM,GACvB,MAAM,CAGR"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Objective scoring — pure logic.
|
|
4
|
+
*
|
|
5
|
+
* The score has three lexicographic levels rather than one number:
|
|
6
|
+
*
|
|
7
|
+
* - **hard** — legal or physical impossibility (rest, overlap, expired
|
|
8
|
+
* qualification, contract breach);
|
|
9
|
+
* - **medium** — coverage: unfilled slots and unmet tag requirements;
|
|
10
|
+
* - **soft** — cost, fairness, preferences, hour-balance.
|
|
11
|
+
*
|
|
12
|
+
* They are compared in order, never summed. A weighted sum would let a large
|
|
13
|
+
* enough soft gain buy a hard breach — arrange the weights unluckily and the
|
|
14
|
+
* solver "discovers" that under-resting one person improves fairness for
|
|
15
|
+
* everyone else. Lexicographic ordering makes that trade unrepresentable
|
|
16
|
+
* instead of merely unlikely.
|
|
17
|
+
*
|
|
18
|
+
* Within a level, breaches contribute their *magnitude*, so a rest gap ten
|
|
19
|
+
* minutes short scores better than one six hours short. Local search needs that
|
|
20
|
+
* gradient to climb out of an infeasible region; a 0/1 flag leaves it on a
|
|
21
|
+
* plateau with nothing to follow.
|
|
22
|
+
*
|
|
23
|
+
* Aggregate constraints (`evaluate`) are deliberately not run per move — they
|
|
24
|
+
* are O(roster) and the search calls the objective in its innermost loop. The
|
|
25
|
+
* cheap aggregates that steer the search (coverage, fairness) have dedicated
|
|
26
|
+
* terms here; the full `evaluate` pass runs once when the result is assembled.
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.score = exports.scoreLex = exports.softBreaches = exports.breachTotals = exports.minHoursShortfalls = exports.hoursVariance = exports.unfilledSlots = exports.tagShortage = exports.slotShortage = exports.isBetter = exports.compareScores = exports.DEFAULT_SOFT_WEIGHT = exports.MIN_HOURS_WEIGHT = exports.TAG_SHORTFALL_WEIGHT = exports.UNFILLED_WEIGHT = void 0;
|
|
30
|
+
const fairness_1 = require("../constraints/fairness");
|
|
31
|
+
const availability_1 = require("../constraints/availability");
|
|
32
|
+
exports.UNFILLED_WEIGHT = 10000;
|
|
33
|
+
exports.TAG_SHORTFALL_WEIGHT = 5000;
|
|
34
|
+
exports.MIN_HOURS_WEIGHT = 100;
|
|
35
|
+
/** Default weight applied to a soft constraint that does not declare one. */
|
|
36
|
+
exports.DEFAULT_SOFT_WEIGHT = 1;
|
|
37
|
+
/** Negative when `a` is better than `b`, positive when worse, 0 when equal. */
|
|
38
|
+
function compareScores(a, b) {
|
|
39
|
+
if (a.hard !== b.hard)
|
|
40
|
+
return a.hard - b.hard;
|
|
41
|
+
if (a.medium !== b.medium)
|
|
42
|
+
return a.medium - b.medium;
|
|
43
|
+
return a.soft - b.soft;
|
|
44
|
+
}
|
|
45
|
+
exports.compareScores = compareScores;
|
|
46
|
+
function isBetter(a, b) {
|
|
47
|
+
return compareScores(a, b) < 0;
|
|
48
|
+
}
|
|
49
|
+
exports.isBetter = isBetter;
|
|
50
|
+
/** Missing assignees for an instance (0 when fully staffed). */
|
|
51
|
+
function slotShortage(ctx, state, instanceId) {
|
|
52
|
+
const inst = ctx.instanceById.get(instanceId);
|
|
53
|
+
if (!inst)
|
|
54
|
+
return 0;
|
|
55
|
+
return Math.max(0, inst.minEmployees - (state.assignments.get(instanceId)?.size ?? 0));
|
|
56
|
+
}
|
|
57
|
+
exports.slotShortage = slotShortage;
|
|
58
|
+
/** Missing tagged assignees, summed over the instance's tag requirements. */
|
|
59
|
+
function tagShortage(ctx, state, instanceId) {
|
|
60
|
+
const inst = ctx.instanceById.get(instanceId);
|
|
61
|
+
if (!inst)
|
|
62
|
+
return 0;
|
|
63
|
+
const assigned = state.assignments.get(instanceId);
|
|
64
|
+
let short = 0;
|
|
65
|
+
for (const [tag, needed] of Object.entries(inst.tagRequirements)) {
|
|
66
|
+
let have = 0;
|
|
67
|
+
if (assigned) {
|
|
68
|
+
for (const employeeId of assigned)
|
|
69
|
+
if (ctx.employeeTags.get(employeeId)?.has(tag))
|
|
70
|
+
have++;
|
|
71
|
+
}
|
|
72
|
+
short += Math.max(0, needed - have);
|
|
73
|
+
}
|
|
74
|
+
return short;
|
|
75
|
+
}
|
|
76
|
+
exports.tagShortage = tagShortage;
|
|
77
|
+
/** Total unfilled capacity across the period, in person-slots. */
|
|
78
|
+
function unfilledSlots(ctx, state) {
|
|
79
|
+
let total = 0;
|
|
80
|
+
for (const inst of ctx.instances)
|
|
81
|
+
total += slotShortage(ctx, state, inst.id);
|
|
82
|
+
return total;
|
|
83
|
+
}
|
|
84
|
+
exports.unfilledSlots = unfilledSlots;
|
|
85
|
+
/** Variance of worked minutes across employees (0 for an empty roster). */
|
|
86
|
+
function hoursVariance(ctx, state) {
|
|
87
|
+
if (ctx.employees.length === 0)
|
|
88
|
+
return 0;
|
|
89
|
+
const values = ctx.employees.map((e) => state.minutesByEmployee.get(e.id) ?? 0);
|
|
90
|
+
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
|
91
|
+
return values.reduce((acc, v) => acc + (v - mean) * (v - mean), 0) / values.length;
|
|
92
|
+
}
|
|
93
|
+
exports.hoursVariance = hoursVariance;
|
|
94
|
+
/** Number of employees below their soft min-hours bound. */
|
|
95
|
+
function minHoursShortfalls(ctx, state) {
|
|
96
|
+
let n = 0;
|
|
97
|
+
for (const e of ctx.employees) {
|
|
98
|
+
if (e.minHoursForPeriod !== undefined && (state.minutesByEmployee.get(e.id) ?? 0) < e.minHoursForPeriod * 60)
|
|
99
|
+
n++;
|
|
100
|
+
}
|
|
101
|
+
return n;
|
|
102
|
+
}
|
|
103
|
+
exports.minHoursShortfalls = minHoursShortfalls;
|
|
104
|
+
/**
|
|
105
|
+
* Weighted breach total per level, over the currently assigned pairs.
|
|
106
|
+
*
|
|
107
|
+
* This is what gives `SchedulingConstraint.weight` teeth — a caller who softens
|
|
108
|
+
* a built-in via `overrides`, or registers a custom rule, now moves the score
|
|
109
|
+
* instead of being silently ignored.
|
|
110
|
+
*/
|
|
111
|
+
function breachTotals(ctx, state) {
|
|
112
|
+
const totals = { hard: 0, medium: 0, soft: 0 };
|
|
113
|
+
// `min-staffing` and `fairness` have dedicated terms below; evaluating their
|
|
114
|
+
// per-pair delta here would either double-count or contribute a constant 0.
|
|
115
|
+
const scored = ctx.constraints.filter((c) => c.id !== 'min-staffing' && c.id !== 'fairness');
|
|
116
|
+
if (scored.length === 0)
|
|
117
|
+
return totals;
|
|
118
|
+
for (const [instanceId, employees] of state.assignments) {
|
|
119
|
+
for (const employeeId of employees) {
|
|
120
|
+
for (const c of scored) {
|
|
121
|
+
const d = c.delta(state, { employeeId, shiftInstanceId: instanceId });
|
|
122
|
+
if (d > 0)
|
|
123
|
+
totals[c.hardness] += d * (c.weight ?? exports.DEFAULT_SOFT_WEIGHT);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return totals;
|
|
128
|
+
}
|
|
129
|
+
exports.breachTotals = breachTotals;
|
|
130
|
+
/** Backwards-compatible soft-breach total, for callers that want a single figure. */
|
|
131
|
+
function softBreaches(ctx, state) {
|
|
132
|
+
return breachTotals(ctx, state).soft;
|
|
133
|
+
}
|
|
134
|
+
exports.softBreaches = softBreaches;
|
|
135
|
+
/** The full three-level score. Lower is better at every level. */
|
|
136
|
+
function scoreLex(ctx, state, objective, minHoursWeight, fairnessRules) {
|
|
137
|
+
const totals = breachTotals(ctx, state);
|
|
138
|
+
for (const inst of ctx.instances) {
|
|
139
|
+
totals.medium += slotShortage(ctx, state, inst.id) * exports.UNFILLED_WEIGHT;
|
|
140
|
+
totals.medium += tagShortage(ctx, state, inst.id) * exports.TAG_SHORTFALL_WEIGHT;
|
|
141
|
+
}
|
|
142
|
+
totals.soft += minHoursShortfalls(ctx, state) * (minHoursWeight || exports.MIN_HOURS_WEIGHT);
|
|
143
|
+
totals.soft += (0, fairness_1.fairnessPenalty)(state, fairnessRules ?? ctx.rules?.fairness);
|
|
144
|
+
// Preferred/avoid availability windows are scored, not advisory: avoided
|
|
145
|
+
// assignments cost, preferred ones credit, weighted per rule.
|
|
146
|
+
totals.soft += (0, availability_1.preferencePenalty)(state);
|
|
147
|
+
if (objective === 'balanced')
|
|
148
|
+
totals.soft += hoursVariance(ctx, state) / 60; // hours-scale variance term
|
|
149
|
+
return totals;
|
|
150
|
+
}
|
|
151
|
+
exports.scoreLex = scoreLex;
|
|
152
|
+
/**
|
|
153
|
+
* Flattened score, lower is better.
|
|
154
|
+
*
|
|
155
|
+
* Retained for callers that need one number. The level separation is preserved
|
|
156
|
+
* by scaling, but comparisons should prefer `compareScores` — a flattened score
|
|
157
|
+
* can only approximate a lexicographic order, and at extreme magnitudes the
|
|
158
|
+
* approximation leaks.
|
|
159
|
+
*/
|
|
160
|
+
function score(ctx, state, objective, minHoursWeight) {
|
|
161
|
+
const lex = scoreLex(ctx, state, objective, minHoursWeight);
|
|
162
|
+
return lex.hard * 1e12 + lex.medium * 1e6 + lex.soft;
|
|
163
|
+
}
|
|
164
|
+
exports.score = score;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** mulberry32 — small seeded PRNG so runs are reproducible without a dependency. */
|
|
2
|
+
export declare function createPrng(seed: number): () => number;
|
|
3
|
+
/** In-place Fisher–Yates shuffle driven by `rand`. */
|
|
4
|
+
export declare function shuffle<T>(arr: T[], rand: () => number): T[];
|
|
5
|
+
//# sourceMappingURL=prng.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prng.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/prng.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,MAAM,CASrD;AAED,sDAAsD;AACtD,wBAAgB,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,MAAM,GAAG,CAAC,EAAE,CAM5D"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shuffle = exports.createPrng = void 0;
|
|
4
|
+
/** mulberry32 — small seeded PRNG so runs are reproducible without a dependency. */
|
|
5
|
+
function createPrng(seed) {
|
|
6
|
+
let a = seed >>> 0;
|
|
7
|
+
return () => {
|
|
8
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
9
|
+
let t = a;
|
|
10
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
11
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
12
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
exports.createPrng = createPrng;
|
|
16
|
+
/** In-place Fisher–Yates shuffle driven by `rand`. */
|
|
17
|
+
function shuffle(arr, rand) {
|
|
18
|
+
for (let i = arr.length - 1; i > 0; i--) {
|
|
19
|
+
const j = Math.floor(rand() * (i + 1));
|
|
20
|
+
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
21
|
+
}
|
|
22
|
+
return arr;
|
|
23
|
+
}
|
|
24
|
+
exports.shuffle = shuffle;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Propagation — domain pruning before search.
|
|
3
|
+
*
|
|
4
|
+
* Builds the eligibility matrix (employee × shift instance) and applies every
|
|
5
|
+
* constraint's `prune()` hook (time-off, impossible per-shift duration, …).
|
|
6
|
+
* A slot left with zero eligible employees is an early infeasibility signal
|
|
7
|
+
* and is reported as a violation before any search runs.
|
|
8
|
+
*/
|
|
9
|
+
import type { ConstraintViolation, ModelContext } from '../types';
|
|
10
|
+
export interface PropagationResult {
|
|
11
|
+
/** employeeId -> set of shift-instance ids they may legally take. */
|
|
12
|
+
eligibility: Map<string, Set<string>>;
|
|
13
|
+
/** instanceId -> eligible employee ids (the transposed view). */
|
|
14
|
+
eligibleByInstance: Map<string, string[]>;
|
|
15
|
+
/** Slots with zero eligible employees, reported before search. */
|
|
16
|
+
violations: ConstraintViolation[];
|
|
17
|
+
}
|
|
18
|
+
export declare function propagate(ctx: ModelContext): PropagationResult;
|
|
19
|
+
//# sourceMappingURL=propagation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"propagation.d.ts","sourceRoot":"","sources":["../../../src/scheduling/engine/propagation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAElE,MAAM,WAAW,iBAAiB;IAC9B,qEAAqE;IACrE,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;IACtC,iEAAiE;IACjE,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC1C,kEAAkE;IAClE,UAAU,EAAE,mBAAmB,EAAE,CAAC;CACrC;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,YAAY,GAAG,iBAAiB,CA6B9D"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Propagation — domain pruning before search.
|
|
4
|
+
*
|
|
5
|
+
* Builds the eligibility matrix (employee × shift instance) and applies every
|
|
6
|
+
* constraint's `prune()` hook (time-off, impossible per-shift duration, …).
|
|
7
|
+
* A slot left with zero eligible employees is an early infeasibility signal
|
|
8
|
+
* and is reported as a violation before any search runs.
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.propagate = void 0;
|
|
12
|
+
function propagate(ctx) {
|
|
13
|
+
const eligibility = new Map();
|
|
14
|
+
for (const emp of ctx.employees) {
|
|
15
|
+
eligibility.set(emp.id, new Set(ctx.instances.map((i) => i.id)));
|
|
16
|
+
}
|
|
17
|
+
for (const constraint of ctx.constraints) {
|
|
18
|
+
constraint.prune?.(ctx, eligibility);
|
|
19
|
+
}
|
|
20
|
+
const eligibleByInstance = new Map();
|
|
21
|
+
for (const inst of ctx.instances)
|
|
22
|
+
eligibleByInstance.set(inst.id, []);
|
|
23
|
+
for (const [employeeId, instances] of eligibility) {
|
|
24
|
+
for (const instanceId of instances)
|
|
25
|
+
eligibleByInstance.get(instanceId)?.push(employeeId);
|
|
26
|
+
}
|
|
27
|
+
const violations = [];
|
|
28
|
+
for (const inst of ctx.instances) {
|
|
29
|
+
if ((eligibleByInstance.get(inst.id) ?? []).length === 0 && inst.minEmployees > 0) {
|
|
30
|
+
violations.push({
|
|
31
|
+
constraintId: 'min-staffing',
|
|
32
|
+
severity: 'hard',
|
|
33
|
+
shiftInstanceId: inst.id,
|
|
34
|
+
message: `shift "${inst.id}" has zero eligible employees`,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return { eligibility, eligibleByInstance, violations };
|
|
39
|
+
}
|
|
40
|
+
exports.propagate = propagate;
|