fix profiling preventing progress

This commit is contained in:
Matthieu Jolimaitre 2024-06-10 11:03:54 +02:00
parent 334c7361fc
commit 8e399fd2bf
5 changed files with 47 additions and 48 deletions

View file

@ -6,11 +6,7 @@ import { GuessResult, Info } from "../game/game.ts";
import { info_of_guess } from "../game/simulator.ts";
import { Awaitable, dbg, enumerate, first, range, zip } from "../utils.ts";
import { Guessing, pick_random_common_word } from "./guesser.ts";
import {
section,
tip,
top,
} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts";
import { tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts";
export class ReducingGuesser implements Guessing {
length;
@ -27,22 +23,27 @@ export class ReducingGuesser implements Guessing {
return ["candidates", "best score"];
}
public async guess(try_: (guess: string, candidates: number, score: number) => Awaitable<GuessResult>) {
return await section("guess", async () => {
if (this.candidates.size === 1) return await try_(first(this.candidates)!, this.candidates.size, 1);
const [guess, score] = get_word_with_smallest_cuts(this.candidates, this.words);
if (score >= this.candidates.size) {
const pick = pick_random_common_word([...this.candidates.values()]);
assertExists(pick);
const result = await try_(pick, this.candidates.size, score);
if (result.kind === "success") return result;
else return null;
}
const result = await try_(guess, this.candidates.size, score);
public async guess_impl(try_: (guess: string, candidates: number, score: number) => Awaitable<GuessResult>) {
if (this.candidates.size === 1) return await try_(first(this.candidates)!, this.candidates.size, 1);
const [guess, score] = get_word_with_smallest_cuts(this.candidates, this.words);
if (score >= this.candidates.size) {
const pick = pick_random_common_word([...this.candidates.values()]);
assertExists(pick);
const result = await try_(pick, this.candidates.size, score);
if (result.kind === "success") return result;
this.learn(guess, result.informations);
return null;
});
else return null;
}
const result = await try_(guess, this.candidates.size, score);
if (result.kind === "success") return result;
this.learn(guess, result.informations);
return null;
}
public async guess(try_: (guess: string, candidates: number, score: number) => Awaitable<GuessResult>) {
tip("guess");
const res = await this.guess_impl(try_);
top("guess");
return res;
}
learn(word: string, infos: Info[]) {
@ -115,18 +116,18 @@ function word_cuts(word: string, dict: Set<string>) {
}
function get_word_with_smallest_cuts(candidates: Set<string>, dict: Set<string>) {
return section("get_word_with_smallest_cuts", () => {
let best = null as null | [string, number];
for (const candidate of dict.values()) {
const cuts = word_cuts(candidate, candidates);
let max_part_size = 0;
for (const cut of cuts) for (const part of cut) if (part.size > max_part_size) max_part_size = part.size;
if (best === null) best = [candidate, max_part_size];
else if (max_part_size < best[1]) best = [candidate, max_part_size];
}
assertExists(best);
return best;
});
tip("get_word_with_smallest_cuts");
let best = null as null | [string, number];
for (const candidate of dict.values()) {
const cuts = word_cuts(candidate, candidates);
let max_part_size = 0;
for (const cut of cuts) for (const part of cut) if (part.size > max_part_size) max_part_size = part.size;
if (best === null) best = [candidate, max_part_size];
else if (max_part_size < best[1]) best = [candidate, max_part_size];
}
assertExists(best);
top("get_word_with_smallest_cuts");
return best;
}
function matches_constraints(candidate: string, constraints: [string, Info][]) {

View file

@ -1,6 +1,8 @@
import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts";
import { async_next, enumerate, LineReader, next } from "./utils.ts";
import { enumerate, LineReader } from "./utils.ts";
import { writeAll } from "https://deno.land/std@0.224.0/io/write_all.ts";
import { Writer } from "https://deno.land/std@0.224.0/io/types.ts";
export async function initialize_prompt(lines: LineReader) {
console.log("Please input initial state of the game. Format is :");
@ -20,13 +22,13 @@ export async function initialize_prompt(lines: LineReader) {
}
async function parse_initialization_until_correct(lines: LineReader) {
console.log("initial : ");
print(Deno.stdout, "initial : ");
while (true) {
const input = await lines.read();
assertExists(input);
const parsed = parse_initialization(input);
if (parsed !== null) return parsed;
console.log("Invalid, please try again : ");
print(Deno.stdout, "Invalid, please try again : ");
}
}
@ -45,3 +47,8 @@ function parse_initialization(input: string) {
}
return { length, constraints };
}
async function print(writer: Writer, ...rest: unknown[]) {
const text = rest.map((o) => `${o}`).join(" ");
await writeAll(writer, new TextEncoder().encode(text));
}

View file

@ -3,10 +3,7 @@ import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists
import { Guessing } from "./guesser/guesser.ts";
import { Gaming, GuessResult } from "./game/game.ts";
import { last, wait, zip } from "./utils.ts";
import {
tip,
top,
} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts";
import { tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts";
export class Runner<Ga extends Gaming, Gu extends Guessing> {
game;
@ -42,7 +39,7 @@ export class Runner<Ga extends Gaming, Gu extends Guessing> {
while (true) {
const result = await this.play_once();
if (result !== null) break;
if (this.max !== undefined) { if (this.turns.length >= this.max) return this.turns; }
if (this.max !== undefined) if (this.turns.length >= this.max) return this.turns;
await wait(this.delay_ms);
}
this.logging.on_finish(this.turns);

View file

@ -1,4 +1,4 @@
#!/usr/bin/env -S deno run --allow-read
#!/usr/bin/env -S deno run --allow-read --allow-env
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts";

View file

@ -1,13 +1,7 @@
#!/usr/bin/env -S deno run -A --unstable-temporal
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts";
import {
report,
section,
startup,
tip,
top,
} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts";
import { report, tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts";
import {
Dict,