Skip to content

Commit

Permalink
Replace previous algorithm implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
niktekusho committed Jun 29, 2024
1 parent 0538e90 commit d6491b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 199 deletions.
41 changes: 21 additions & 20 deletions src/morph/benchmark/benchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "node:path";
import { test } from "vitest";
import { validateActionInstance } from "../actions";
import { validateRule } from "../rule";
import { Ruleset, applyRuleset, applyRulesetNew } from "../ruleset";
import { Ruleset, applyRuleset } from "../ruleset";
import { GOOD } from "@/good/good_spec";

test("validate 10.000 action instances", () => {
Expand Down Expand Up @@ -66,24 +66,25 @@ test("morph 10.000 artifacts with 100 rules", () => {

const good = JSON.parse(goodFile) as GOOD;

const oldStart = Date.now();
console.time("morph 10.000 artifacts with 100 rules");
// const oldStart = Date.now();
applyRuleset(ruleset, good);
const oldEnd = Date.now();

const newStart = Date.now();
applyRulesetNew(ruleset, good);
const newEnd = Date.now();

const oldTime = oldEnd - oldStart;
const newTime = newEnd - newStart;

const speedup = (Math.max(newTime, oldTime)) / Math.min(newTime, oldTime);
const stats = {
oldTime,
newTime,
fastest: newTime < oldTime ? 'new' : 'old',
speedup: `${speedup.toFixed(2)}x`
}
console.table(stats);

console.timeEnd("morph 10.000 artifacts with 100 rules");
// const oldEnd = Date.now();

// const newStart = Date.now();
// applyRulesetNew(ruleset, good);
// const newEnd = Date.now();

// const oldTime = oldEnd - oldStart;
// const newTime = newEnd - newStart;

// const speedup = (Math.max(newTime, oldTime)) / Math.min(newTime, oldTime);
// const stats = {
// oldTime,
// newTime,
// fastest: newTime < oldTime ? 'new' : 'old',
// speedup: `${speedup.toFixed(2)}x`
// }
// console.table(stats);
});
136 changes: 1 addition & 135 deletions src/morph/ruleset.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GOOD } from "@/good/good_spec";
import { assert, test } from "vitest";
import { Ruleset, applyRuleset, applyRulesetNew, validateRuleset } from "./ruleset";
import { Ruleset, applyRuleset, validateRuleset } from "./ruleset";

const GOODFile: GOOD = {
format: "GOOD",
Expand Down Expand Up @@ -388,140 +388,6 @@ test("applyRuleset with 2 rules that both match should return expected morphed G
assert.deepEqual(morphedGOOD, expectedGOODFile);
});

test("applyRulesetNew with 2 rules that both match should return expected morphed GOOD", async () => {
// Arrange
const ruleset: Ruleset = {
name: "test",
rules: [
{
action: {
type: "equip",
to: "Xingqiu",
},
filter: {
characterName: "Yelan",
type: "equippingCharacter",
},
id: 1,
},
{
action: {
type: "equip",
to: "Neuvillette",
},
filter: {
characterName: "Barbara",
type: "equippingCharacter",
},
id: 2,
},
],
};

// Act
const morphedGOOD = applyRulesetNew(ruleset, GOODFile);

// Assert
const expectedGOODFile: GOOD = {
format: "GOOD",
source: "test",
version: 1,
artifacts: [
{
setKey: "HeartOfDepth",
rarity: 5,
level: 20,
slotKey: "goblet",
mainStatKey: "hydro_dmg_",
substats: [
{ key: "hp_", value: 4.7 },
{ key: "critDMG_", value: 14.8 },
{ key: "hp", value: 538 },
{ key: "critRate_", value: 14.4 },
],
location: "Xingqiu",
lock: true,
},
{
setKey: "EmblemOfSeveredFate",
rarity: 5,
level: 20,
slotKey: "sands",
mainStatKey: "hp_",
substats: [
{ key: "enerRech_", value: 16.8 },
{ key: "atk", value: 16 },
{ key: "eleMas", value: 42 },
{ key: "critRate_", value: 8.9 },
],
location: "Xingqiu",
lock: true,
},
{
setKey: "EmblemOfSeveredFate",
rarity: 5,
level: 20,
slotKey: "circlet",
mainStatKey: "critRate_",
substats: [
{ key: "hp_", value: 15.7 },
{ key: "atk_", value: 10.5 },
{ key: "def_", value: 5.1 },
{ key: "critDMG_", value: 18.7 },
],
location: "Xingqiu",
lock: true,
},
{
setKey: "EmblemOfSeveredFate",
rarity: 5,
level: 20,
slotKey: "plume",
mainStatKey: "atk",
substats: [
{ key: "enerRech_", value: 11.7 },
{ key: "critDMG_", value: 28.8 },
{ key: "hp_", value: 4.1 },
{ key: "hp", value: 448 },
],
location: "Xingqiu",
lock: true,
},
{
setKey: "EmblemOfSeveredFate",
rarity: 5,
level: 20,
slotKey: "flower",
mainStatKey: "hp",
substats: [
{ key: "eleMas", value: 37 },
{ key: "critRate_", value: 3.1 },
{ key: "enerRech_", value: 21.4 },
{ key: "critDMG_", value: 7.8 },
],
location: "Xingqiu",
lock: true,
},
{
setKey: "MarechausseeHunter",
rarity: 5,
level: 20,
slotKey: "flower",
mainStatKey: "hp",
substats: [
{ key: "eleMas", value: 37 },
{ key: "critRate_", value: 3.1 },
{ key: "enerRech_", value: 21.4 },
{ key: "critDMG_", value: 7.8 },
],
location: "Neuvillette",
lock: true,
},
],
};
assert.deepEqual(morphedGOOD, expectedGOODFile);
});

test("validateRuleset returns error when ruleset is not an object", () => {
// Arrange
// Act
Expand Down
44 changes: 0 additions & 44 deletions src/morph/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,49 +117,6 @@ export function applyRuleset(ruleset: Ruleset, good: GOOD): GOOD {
return good;
}

console.time("applyRuleset");

const { rules } = ruleset;

const editedGood = clone(good);

const artifacts = editedGood.artifacts!;

// artifacts.filter((art) => art.location === "Yelan").forEach(console.log);

for (let i = 0; i < artifacts.length; i++) {
const ogArtifact = artifacts[i];
// TODO: naive code...
for (const rule of rules) {
const predicate = createPredicate(rule.filter);
const mutation = createMutation(rule.action);
if (predicate(ogArtifact)) {
artifacts[i] = mutation(ogArtifact);
}
}
}

// console.log("editedGood", editedGood);

console.timeEnd("applyRuleset");
const stats = {
rules: ruleset.rules.length,
goodFile: good.artifacts.length,
};
console.log("stats", stats);
return editedGood;
}

export function applyRulesetNew(ruleset: Ruleset, good: GOOD): GOOD {
// TODO: Validate ruleset?

// Exit early in case of no artifacts...
if (good.artifacts === undefined) {
return good;
}

console.time("applyRuleset");

const { rules } = ruleset;

const ruleFns = rules.map((rule) => createRuleFunction(rule));
Expand All @@ -179,7 +136,6 @@ export function applyRulesetNew(ruleset: Ruleset, good: GOOD): GOOD {

// console.log("editedGood", editedGood);

console.timeEnd("applyRuleset");
const stats = {
rules: ruleset.rules.length,
goodFile: good.artifacts.length,
Expand Down

0 comments on commit d6491b4

Please sign in to comment.