Skip to content

Commit

Permalink
FIX generating consistent scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
driver-deploy-2 committed Sep 8, 2023
1 parent bee25cb commit 025ca95
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 79 deletions.
80 changes: 2 additions & 78 deletions src/components/create-scenario-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
TextInput,
uniqueId,
} from 'mithril-materialized';
import { Dashboards, ID, Narrative, Scenario } from '../models';
import { Dashboards, ID, Narrative } from '../models';
import { MeiosisComponent, saveModel, setPage, t } from '../services';
import { deepCopy, getRandomValue } from '../utils';
import { deepCopy, generateNarrative } from '../utils';

const ToggleIcon: FactoryComponent<{
on: string;
Expand Down Expand Up @@ -157,82 +157,6 @@ export const CategoryTable: MeiosisComponent<{
};
};

const generateNarrative = (
scenario: Scenario,
locked: Record<ID, ID[]> = {}
// excludedComps: Record<ID, boolean> = {}
) => {
const { categories, components, inconsistencies } = scenario;

// const chosen = { ...locked } as Record<ID, ID>;
let tries = 0;
const generate = () => {
const chosen = { ...locked } as Record<ID, ID[]>;
for (const category of categories) {
const catComps = components
.filter(
(c) => category.componentIds && category.componentIds.includes(c.id)
)
.map((c) => ({ ...c, inc: inconsistencies[c.id] }))
.sort((a, b) =>
a.inc && b.inc
? Object.keys(a.inc).length > Object.keys(b.inc).length
? 1
: -1
: a.inc
? 1
: b.inc
? -1
: 1
);
const excluded: ID[] = [];
for (const catComp of catComps) {
if (chosen.hasOwnProperty(catComp.id)) {
const chosenValue = chosen[catComp.id];
if (
chosenValue &&
chosenValue.length &&
inconsistencies.hasOwnProperty(chosenValue[0])
) {
Object.keys(inconsistencies[chosenValue[0]]).forEach((id) =>
excluded.push(id)
);
}
continue;
}
const valuesToChooseFrom =
catComp.values &&
catComp.values
.map(({ id }) => id)
.filter((id) => !excluded.includes(id));
if (!valuesToChooseFrom || valuesToChooseFrom.length === 0)
return false;
const value = getRandomValue(valuesToChooseFrom);
if (value) {
chosen[catComp.id] = [value];
} else {
return false;
}
}
}
return chosen;
};

do {
const components = generate();
if (components) {
const narrative = {
id: uniqueId(),
components,
included: false,
} as Narrative;
return narrative;
}
tries++;
} while (tries < 100);
return false;
};

export const CreateScenarioPage: MeiosisComponent = () => {
let editor: Quill;
let version = 0;
Expand Down
83 changes: 82 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import m from 'mithril';
import { padLeft } from 'mithril-materialized';
import { padLeft, uniqueId } from 'mithril-materialized';
import { render } from 'mithril-ui-form';
import {
ContextType,
DataModel,
ID,
Inconsistencies,
Narrative,
OldDataModel,
OsmTypeList,
Scenario,
Expand Down Expand Up @@ -366,3 +367,83 @@ export const modelToSaveName = (model: DataModel, narrativeName?: string) => {
3
)}_${formatDate()}`;
};

export const generateNarrative = (
scenario: Scenario,
locked: Record<ID, ID[]> = {}
) => {
const { categories, components, inconsistencies } = scenario;

let tries = 0;
const generate = () => {
const chosen = { ...locked } as Record<ID, ID[]>;
for (const category of categories) {
const catComps = components
.filter(
(c) => category.componentIds && category.componentIds.includes(c.id)
)
.map((c) => {
const inc = c.values
? c.values.reduce((acc, cur) => {
return (
acc +
(inconsistencies[cur.id]
? Object.keys(inconsistencies[cur.id]).length
: 0)
);
}, 0)
: 0;
return { ...c, inc };
})
.sort((a, b) => (a.inc > b.inc ? -1 : 1));
const excluded: ID[] = [];
for (const catComp of catComps) {
if (chosen.hasOwnProperty(catComp.id)) {
const chosenValue = chosen[catComp.id];
if (chosenValue && chosenValue.length) {
if (chosenValue.some((v) => excluded.includes(v))) return false;
chosenValue.forEach((v) => {
inconsistencies[v] &&
Object.keys(inconsistencies[v]).forEach((id) =>
excluded.push(id)
);
});
}
continue;
}
const valuesToChooseFrom =
catComp.values &&
catComp.values
.map(({ id }) => id)
.filter((id) => !excluded.includes(id));
console.table(excluded);
console.table(valuesToChooseFrom);
if (!valuesToChooseFrom || valuesToChooseFrom.length === 0)
return false;
const v = getRandomValue(valuesToChooseFrom);
if (v) {
inconsistencies[v] &&
Object.keys(inconsistencies[v]).forEach((id) => excluded.push(id));
chosen[catComp.id] = [v];
} else {
return false;
}
}
}
return chosen;
};

do {
const components = generate();
if (components) {
const narrative = {
id: uniqueId(),
components,
included: false,
} as Narrative;
return narrative;
}
tries++;
} while (tries < 100);
return false;
};

0 comments on commit 025ca95

Please sign in to comment.