🚀 benchmark ?

This commit is contained in:
platane
2020-10-06 23:53:46 +02:00
committed by Platane
parent b0784fbaca
commit 16a47349be
10 changed files with 181 additions and 11 deletions

View File

@@ -19,6 +19,16 @@ setColor(corner, 4, 0, 1 as Color);
setColor(corner, 4, 4, 1 as Color);
setColor(corner, 0, 0, 1 as Color);
// enclaved color
export const enclave = createEmptyGrid(7, 7);
setColor(enclave, 3, 4, 2 as Color);
setColor(enclave, 2, 3, 2 as Color);
setColor(enclave, 2, 4, 2 as Color);
setColor(enclave, 4, 4, 2 as Color);
setColor(enclave, 4, 3, 2 as Color);
setColor(enclave, 3, 3, 1 as Color);
setColor(enclave, 5, 5, 1 as Color);
const create = (width: number, height: number, emptyP: number) => {
const grid = createEmptyGrid(width, height);
const random = new ParkMiller(10);
@@ -31,3 +41,7 @@ const create = (width: number, height: number, emptyP: number) => {
export const small = create(10, 7, 3);
export const smallPacked = create(10, 7, 1);
export const smallFull = create(10, 7, 0);
// small realistic
export const realistic = create(52, 7, 3);
export const realisticFull = create(52, 7, 0);

View File

@@ -0,0 +1,33 @@
import { getAvailableInterestingRoutes } from "../getAvailableRoutes";
import { small as grid } from "../__fixtures__/grid";
import { snake3 } from "../__fixtures__/snake";
import { performance } from "perf_hooks";
import { getAvailableRoutes } from "../getAvailableRoutes2";
const m = 1000;
{
const s = performance.now();
for (let k = m; k--; ) {
const solutions = [];
getAvailableInterestingRoutes(
grid,
snake3,
(snakes) => {
solutions.push(snakes);
return false;
},
2
);
}
console.log((performance.now() - s) / m, "ms");
}
{
const s = performance.now();
for (let k = m; k--; ) {
getAvailableRoutes(grid, snake3, 2);
}
console.log((performance.now() - s) / m, "ms");
}

View File

@@ -0,0 +1,65 @@
import { Grid, isInsideLarge, getColor, isInside, isEmpty } from "./grid";
import { around4 } from "./point";
import {
getHeadX,
getHeadY,
nextSnake,
Snake,
snakeEquals,
snakeWillSelfCollide,
} from "./snake";
const snakeEqualsN = (a: Snake, b: Snake, n = a.length / 2) => {
for (let i = 0; i < n * 2; i++) if (a[i] !== b[i]) return false;
return true;
};
export const getAvailableRoutes = (grid: Grid, snake0: Snake, n?: number) => {
const openList: Snake[][] = [[snake0]];
const closeList: Snake[] = [];
const solutions: Snake[][] = [];
while (openList.length) {
const c = openList.shift()!;
const [snake] = c;
closeList.push(snake);
const cx = getHeadX(snake);
const cy = getHeadY(snake);
for (let i = 0; i < around4.length; i++) {
const { x: dx, y: dy } = around4[i];
const nx = cx + dx;
const ny = cy + dy;
if (
isInsideLarge(grid, 1, nx, ny) &&
!snakeWillSelfCollide(snake, dx, dy)
) {
const nsnake = nextSnake(snake, dx, dy);
if (!closeList.some((s) => snakeEquals(nsnake, s))) {
const color = isInside(grid, nx, ny) && getColor(grid, nx, ny);
if (color && !isEmpty(color)) {
if (solutions.every(([s]) => !snakeEqualsN(s, nsnake, n))) {
const solution = [nsnake, ...c.slice(0, -1)];
solutions.push(solution);
}
} else {
if (!openList.some(([s]) => snakeEquals(nsnake, s))) {
const chain = [nsnake, ...c];
openList.push(chain);
openList.sort((a, b) => a.length - b.length);
}
}
}
}
}
}
return solutions;
};

View File

@@ -20,6 +20,8 @@ const createHeuristic = (grid0: Grid) => {
colorCount[color] = (0 | colorCount[color]) + 1;
}
// const colors = Object.keys(colorCount).map((x) => +x);
const target = Object.entries(colorCount)
.sort(([a], [b]) => +a - +b)
.map(([color, length]: any) => Array.from({ length }, () => +color))
@@ -33,9 +35,22 @@ const createHeuristic = (grid0: Grid) => {
_snake: Snake,
stack: Color[]
) => {
const x = target[stack.length];
const colorTarget = target[stack.length];
return (c: Color) => (x === c ? 1 : 0);
// const cc = { ...colorCount };
// for (const color of stack) cc[color]--;
// let colorTarget;
// for (let i = colors.length; i--; )
// if (cc[colors[i]] > 0) colorTarget = colors[i];
return (c: Color) => {
if (colorTarget === c) return 1;
return 0;
// return 1 - Math.abs(colorTarget - c) / 10;
};
};
const isEnd = (_grid: Grid, _snake: Snake, stack: Color[]) =>