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 { info_of_guess } from "../game/simulator.ts";
import { Awaitable, dbg, enumerate, first, range, zip } from "../utils.ts"; import { Awaitable, dbg, enumerate, first, range, zip } from "../utils.ts";
import { Guessing, pick_random_common_word } from "./guesser.ts"; import { Guessing, pick_random_common_word } from "./guesser.ts";
import { import { tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts";
section,
tip,
top,
} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts";
export class ReducingGuesser implements Guessing { export class ReducingGuesser implements Guessing {
length; length;
@ -27,8 +23,7 @@ export class ReducingGuesser implements Guessing {
return ["candidates", "best score"]; return ["candidates", "best score"];
} }
public async guess(try_: (guess: string, candidates: number, score: number) => Awaitable<GuessResult>) { public async guess_impl(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); 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); const [guess, score] = get_word_with_smallest_cuts(this.candidates, this.words);
if (score >= this.candidates.size) { if (score >= this.candidates.size) {
@ -42,7 +37,13 @@ export class ReducingGuesser implements Guessing {
if (result.kind === "success") return result; if (result.kind === "success") return result;
this.learn(guess, result.informations); this.learn(guess, result.informations);
return null; 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[]) { learn(word: string, infos: Info[]) {
@ -115,7 +116,7 @@ function word_cuts(word: string, dict: Set<string>) {
} }
function get_word_with_smallest_cuts(candidates: Set<string>, dict: Set<string>) { function get_word_with_smallest_cuts(candidates: Set<string>, dict: Set<string>) {
return section("get_word_with_smallest_cuts", () => { tip("get_word_with_smallest_cuts");
let best = null as null | [string, number]; let best = null as null | [string, number];
for (const candidate of dict.values()) { for (const candidate of dict.values()) {
const cuts = word_cuts(candidate, candidates); const cuts = word_cuts(candidate, candidates);
@ -125,8 +126,8 @@ function get_word_with_smallest_cuts(candidates: Set<string>, dict: Set<string>)
else if (max_part_size < best[1]) best = [candidate, max_part_size]; else if (max_part_size < best[1]) best = [candidate, max_part_size];
} }
assertExists(best); assertExists(best);
top("get_word_with_smallest_cuts");
return best; return best;
});
} }
function matches_constraints(candidate: string, constraints: [string, Info][]) { 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 { 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) { export async function initialize_prompt(lines: LineReader) {
console.log("Please input initial state of the game. Format is :"); 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) { async function parse_initialization_until_correct(lines: LineReader) {
console.log("initial : "); print(Deno.stdout, "initial : ");
while (true) { while (true) {
const input = await lines.read(); const input = await lines.read();
assertExists(input); assertExists(input);
const parsed = parse_initialization(input); const parsed = parse_initialization(input);
if (parsed !== null) return parsed; 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 }; 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 { Guessing } from "./guesser/guesser.ts";
import { Gaming, GuessResult } from "./game/game.ts"; import { Gaming, GuessResult } from "./game/game.ts";
import { last, wait, zip } from "./utils.ts"; import { last, wait, zip } from "./utils.ts";
import { import { tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts";
tip,
top,
} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts";
export class Runner<Ga extends Gaming, Gu extends Guessing> { export class Runner<Ga extends Gaming, Gu extends Guessing> {
game; game;
@ -42,7 +39,7 @@ export class Runner<Ga extends Gaming, Gu extends Guessing> {
while (true) { while (true) {
const result = await this.play_once(); const result = await this.play_once();
if (result !== null) break; 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); await wait(this.delay_ms);
} }
this.logging.on_finish(this.turns); 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"; 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 #!/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 { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts";
import { import { report, tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts";
report,
section,
startup,
tip,
top,
} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts";
import { import {
Dict, Dict,