@slithy/prim-lib 0.3.0 → 0.3.2
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/dist/index.d.ts +3 -0
- package/dist/index.js +30 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,7 @@ interface Cfg {
|
|
|
38
38
|
alpha: number;
|
|
39
39
|
mutateAlpha: boolean;
|
|
40
40
|
shapeTypes: Array<new (w: number, h: number) => ShapeInterface>;
|
|
41
|
+
shapeWeights?: number[];
|
|
41
42
|
shapes: number;
|
|
42
43
|
mutations: number;
|
|
43
44
|
steps: number;
|
|
@@ -166,6 +167,7 @@ declare class Step {
|
|
|
166
167
|
mutate(): Step;
|
|
167
168
|
}
|
|
168
169
|
|
|
170
|
+
type ShapeCtor = new (w: number, h: number) => ShapeInterface;
|
|
169
171
|
declare class Optimizer {
|
|
170
172
|
cfg: Cfg;
|
|
171
173
|
state: State;
|
|
@@ -173,6 +175,7 @@ declare class Optimizer {
|
|
|
173
175
|
_steps: number;
|
|
174
176
|
_stopped: boolean;
|
|
175
177
|
_paused: boolean;
|
|
178
|
+
_stepPlan: ShapeCtor[];
|
|
176
179
|
constructor(original: Canvas, cfg: Cfg);
|
|
177
180
|
start(): void;
|
|
178
181
|
stop(): void;
|
package/dist/index.js
CHANGED
|
@@ -349,6 +349,16 @@ var Shape = class {
|
|
|
349
349
|
}
|
|
350
350
|
static create(cfg) {
|
|
351
351
|
const ctors = cfg.shapeTypes;
|
|
352
|
+
const weights = cfg.shapeWeights;
|
|
353
|
+
if (weights && weights.length === ctors.length) {
|
|
354
|
+
const total = weights.reduce((sum, w) => sum + w, 0);
|
|
355
|
+
let r = Math.random() * total;
|
|
356
|
+
for (let i = 0; i < weights.length; i++) {
|
|
357
|
+
r -= weights[i];
|
|
358
|
+
if (r <= 0) return new ctors[i](cfg.width, cfg.height);
|
|
359
|
+
}
|
|
360
|
+
return new ctors[ctors.length - 1](cfg.width, cfg.height);
|
|
361
|
+
}
|
|
352
362
|
const index = Math.floor(Math.random() * ctors.length);
|
|
353
363
|
const ctor = ctors[index];
|
|
354
364
|
return new ctor(cfg.width, cfg.height);
|
|
@@ -844,6 +854,22 @@ var Debug = class extends Shape {
|
|
|
844
854
|
};
|
|
845
855
|
|
|
846
856
|
// src/optimizer.ts
|
|
857
|
+
function buildStepPlan(cfg) {
|
|
858
|
+
const { shapeTypes, shapeWeights, steps } = cfg;
|
|
859
|
+
if (!shapeWeights || shapeWeights.length !== shapeTypes.length) return [];
|
|
860
|
+
const total = shapeWeights.reduce((sum, w) => sum + w, 0);
|
|
861
|
+
const floats = shapeWeights.map((w) => w / total * steps);
|
|
862
|
+
const floors = floats.map(Math.floor);
|
|
863
|
+
const remainder = steps - floors.reduce((s, n) => s + n, 0);
|
|
864
|
+
const indices = floats.map((f, i) => ({ i, frac: f - floors[i] })).sort((a, b) => b.frac - a.frac).slice(0, remainder).map((x) => x.i);
|
|
865
|
+
const counts = floors.map((n, i) => indices.includes(i) ? n + 1 : n);
|
|
866
|
+
const plan = shapeTypes.flatMap((ctor, i) => Array(counts[i]).fill(ctor));
|
|
867
|
+
for (let i = plan.length - 1; i > 0; i--) {
|
|
868
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
869
|
+
[plan[i], plan[j]] = [plan[j], plan[i]];
|
|
870
|
+
}
|
|
871
|
+
return plan;
|
|
872
|
+
}
|
|
847
873
|
var Optimizer = class {
|
|
848
874
|
cfg;
|
|
849
875
|
state;
|
|
@@ -851,6 +877,7 @@ var Optimizer = class {
|
|
|
851
877
|
_steps;
|
|
852
878
|
_stopped;
|
|
853
879
|
_paused;
|
|
880
|
+
_stepPlan;
|
|
854
881
|
constructor(original, cfg) {
|
|
855
882
|
this.cfg = cfg;
|
|
856
883
|
this.state = new State(original, Canvas.empty(cfg));
|
|
@@ -859,6 +886,7 @@ var Optimizer = class {
|
|
|
859
886
|
this._paused = false;
|
|
860
887
|
this.onStep = () => {
|
|
861
888
|
};
|
|
889
|
+
this._stepPlan = buildStepPlan(cfg);
|
|
862
890
|
}
|
|
863
891
|
start() {
|
|
864
892
|
this._stopped = false;
|
|
@@ -899,10 +927,11 @@ var Optimizer = class {
|
|
|
899
927
|
}
|
|
900
928
|
_findBestStep() {
|
|
901
929
|
const LIMIT = this.cfg.shapes;
|
|
930
|
+
const ctor = this._stepPlan[this._steps];
|
|
902
931
|
let bestStep = null;
|
|
903
932
|
const promises = [];
|
|
904
933
|
for (let i = 0; i < LIMIT; i++) {
|
|
905
|
-
const shape = Shape.create(this.cfg);
|
|
934
|
+
const shape = ctor ? new ctor(this.cfg.width, this.cfg.height) : Shape.create(this.cfg);
|
|
906
935
|
const promise = new Step(shape, this.cfg).compute(this.state).then((step) => {
|
|
907
936
|
if (!bestStep || step.distance < bestStep.distance) {
|
|
908
937
|
bestStep = step;
|