|
197 | 197 | {} a) |
198 | 198 | b)) |
199 | 199 |
|
200 | | -(defn generate-transition-key [cy ay need setting] |
201 | | - (vector cy ay :NONSEND (states/state need setting))) |
| 200 | +(defn generate-transition-key [{:keys [transition-type cy ay need setting move-state]}] |
| 201 | + (when (not= move-state (states/state need setting)) |
| 202 | + (case transition-type |
| 203 | + "joiners" |
| 204 | + (vector cy ay :NONSEND (states/state need setting)) |
| 205 | + |
| 206 | + "leavers" |
| 207 | + (vector cy ay (states/state need setting) :NONSEND) |
| 208 | + |
| 209 | + "movers-to" |
| 210 | + (vector cy ay move-state (states/state need setting)) |
| 211 | + |
| 212 | + "movers-from" |
| 213 | + (vector cy ay (states/state need setting) move-state)))) |
202 | 214 |
|
203 | 215 | (defn update-ifelse-assoc [m k arithmetic-fn v] |
204 | 216 | (if (contains? m k) |
|
260 | 272 | :mover-beta-params (p/beta-params-movers valid-states valid-transitions transition-matrix) |
261 | 273 | :mover-state-alphas (p/alpha-params-movers valid-states valid-transitions transition-matrix)})))) |
262 | 274 |
|
263 | | -(defn build-states-to-change [settings-to-change valid-needs ages years] |
264 | | - (if (= :nil (-> settings-to-change |
265 | | - ds/row-maps |
266 | | - first |
267 | | - :setting-2)) |
268 | | - (let [states (->> settings-to-change |
269 | | - ds/row-maps |
270 | | - (map #(vector (:setting-1 %))))] |
271 | | - (vec (mapcat (fn [year] |
272 | | - (mapcat (fn [age] |
273 | | - (mapcat (fn [need] |
274 | | - (map (fn [setting] (vector (generate-transition-key year age need (first setting)))) |
275 | | - states)) valid-needs)) ages)) years))) |
276 | | - (let [state-pairs (->> settings-to-change |
277 | | - ds/row-maps |
278 | | - (map #(vector (:setting-1 %) (:setting-2 %))))] |
279 | | - (vec (mapcat (fn [year] |
280 | | - (mapcat (fn [age] |
281 | | - (mapcat (fn [need] |
282 | | - (map (fn [setting] (vector (generate-transition-key year age need (first setting)) |
283 | | - (generate-transition-key year age need (second setting)))) |
284 | | - state-pairs)) valid-needs)) ages)) years))))) |
| 275 | +(defn build-states-to-change [input valid-needs valid-settings ages years transition-type] |
| 276 | + (let [to-maps (ds/row-maps input) |
| 277 | + settings-to-change (if (= :nil (-> to-maps |
| 278 | + first |
| 279 | + :setting-2)) |
| 280 | + (map #(vector (:setting-1 %)) to-maps) |
| 281 | + (map #(vector (:setting-1 %) (:setting-2 %)) to-maps))] |
| 282 | + (->> (for [year years |
| 283 | + age ages |
| 284 | + need valid-needs |
| 285 | + setting valid-settings |
| 286 | + setting-to-change settings-to-change] |
| 287 | + (let [keys {:transition-type transition-type :cy year :ay age |
| 288 | + :need need :move-state (states/state need setting)}] |
| 289 | + (if (= :nil (-> to-maps |
| 290 | + first |
| 291 | + :setting-2)) |
| 292 | + (vector (generate-transition-key (merge keys {:setting (first setting-to-change)}))) |
| 293 | + (vector (generate-transition-key (merge keys {:setting (first setting-to-change)})) |
| 294 | + (generate-transition-key (merge keys {:setting (second setting-to-change)})))))) |
| 295 | + (remove #(nil? (first %))) |
| 296 | + distinct))) |
285 | 297 |
|
286 | 298 | (defworkflowfn prepare-send-inputs-1-0-0 |
287 | 299 | "Outputs the population for the last year of historic data, with one |
|
294 | 306 | :transition-matrix sc/TransitionCounts |
295 | 307 | :setting-cost sc/NeedSettingCost |
296 | 308 | :valid-setting-academic-years sc/ValidSettingAcademicYears} |
297 | | - :witan/param-schema {:modify-transition-by s/Num |
| 309 | + :witan/param-schema {:which-transitions? sc/transition-type |
| 310 | + :modify-transition-by s/Num |
298 | 311 | :splice-ncy (s/maybe sc/AcademicYear) |
299 | 312 | :filter-transitions-from (s/maybe [sc/CalendarYear])} |
300 | 313 | :witan/output-schema {:standard-projection sc/projection-map |
|
303 | 316 | :settings-to-change sc/SettingsToChange}} |
304 | 317 | [{:keys [settings-to-change initial-send-population transition-matrix population |
305 | 318 | setting-cost valid-setting-academic-years]} |
306 | | - {:keys [modify-transition-by splice-ncy filter-transitions-from]}] |
| 319 | + {:keys [which-transitions? modify-transition-by splice-ncy filter-transitions-from]}] |
307 | 320 | (let [original-transitions transition-matrix |
308 | 321 | ages (distinct (map :academic-year (ds/row-maps population))) |
309 | 322 | years (distinct (map :calendar-year (ds/row-maps population))) |
|
314 | 327 | valid-states (states/calculate-valid-states-from-setting-academic-years initialise-validation) |
315 | 328 | valid-year-settings (states/calculate-valid-year-settings-from-setting-academic-years initialise-validation) |
316 | 329 | states-to-change (when (not= 1 modify-transition-by) |
317 | | - (build-states-to-change settings-to-change valid-needs ages years)) |
| 330 | + (mapcat (fn [transition-type] (build-states-to-change settings-to-change valid-needs valid-settings ages years transition-type)) which-transitions?)) |
318 | 331 | transition-matrix (ds/row-maps transition-matrix) |
319 | 332 | modified-transition-matrix (when (not= 1 modify-transition-by) |
320 | 333 | (let [convert (-> transition-matrix |
|
0 commit comments