Skip to content

Commit

Permalink
Merge pull request #61 from franciscoBSalgueiro/42-access-to-all-of-t…
Browse files Browse the repository at this point in the history
…he-engine-settings

Add access to all of the engine settings
  • Loading branch information
franciscoBSalgueiro authored Oct 29, 2023
2 parents febf9ee + c74fb2f commit e1b4fa5
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 104 deletions.
22 changes: 14 additions & 8 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ impl EngineProcess {
.await?;
self.set_option("MultiPV", &options.multipv.to_string())
.await?;

for option in options.extra_options {
self.set_option(&option.name, &option.value).await?;
}

self.set_position(&options.fen).await?;
self.last_depth = 0;
self.best_moves.clear();
Expand Down Expand Up @@ -262,10 +267,18 @@ async fn send_command(stdin: &mut ChildStdin, command: impl AsRef<str>) {
}

#[derive(Deserialize, Debug, Clone, Type)]
#[serde(rename_all = "camelCase")]
pub struct EngineOptions {
pub multipv: u16,
pub threads: u16,
pub fen: String,
pub extra_options: Vec<EngineOption>,
}

#[derive(Deserialize, Debug, Clone, Type)]
pub struct EngineOption {
name: String,
value: String,
}

#[derive(Deserialize, Debug, Clone, Type)]
Expand Down Expand Up @@ -430,14 +443,6 @@ pub async fn analyze_game(

let mut chess: Chess = fen.into_position(CastlingMode::Standard)?;

process
.set_options(EngineOptions {
threads: 4,
multipv: 1,
fen: options.fen.to_string(),
})
.await?;

let len_moves = moves.len();

let mut novelty_found = false;
Expand Down Expand Up @@ -465,6 +470,7 @@ pub async fn analyze_game(
threads: 4,
multipv: 1,
fen: fen.to_string(),
extra_options: Vec::new(),
})
.await?;

Expand Down
31 changes: 18 additions & 13 deletions src/atoms/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { atomFamily, atomWithStorage, createJSONStorage, loadable } from "jotai/utils";
import { Tab, genID } from "@/utils/tabs";
import { MantineColor } from "@mantine/core";
import { Session } from "../utils/session";
import { PrimitiveAtom, atom } from "jotai";
import { GoMode } from "@/bindings";
import { Card, buildFromTree } from "@/components/files/opening";
import { LocalOptions } from "@/components/panels/database/DatabasePanel";
import { DatabaseInfo } from "@/utils/db";
import { Engine } from "@/utils/engines";
import { LichessGamesOptions, MasterGamesOptions } from "@/utils/lichess/lichessexplorer";
import { MissingMove } from "@/utils/repertoire";
import { Card, buildFromTree } from "@/components/files/opening";
import { Tab, genID } from "@/utils/tabs";
import { GameHeaders, TreeNode } from "@/utils/treeReducer";
import { MantineColor } from "@mantine/core";
import { BaseDirectory, readTextFile, removeFile, writeTextFile } from "@tauri-apps/api/fs";
import { PrimitiveAtom, atom } from "jotai";
import { atomFamily, atomWithStorage, createJSONStorage, loadable } from "jotai/utils";
import { AtomFamily } from "jotai/vanilla/utils/atomFamily";
import EngineSettings from "@/components/panels/analysis/EngineSettings";
import { AsyncStringStorage } from "jotai/vanilla/utils/atomWithStorage";
import { BaseDirectory, readTextFile, removeFile, writeTextFile } from "@tauri-apps/api/fs";
import { Engine } from "@/utils/engines";
import { LichessGamesOptions, MasterGamesOptions } from "@/utils/lichess/lichessexplorer";
import { LocalOptions } from "@/components/panels/database/DatabasePanel";
import { Session } from "../utils/session";


const options = { dir: BaseDirectory.AppData };
Expand Down Expand Up @@ -239,18 +239,23 @@ export const deckAtomFamily = atomFamily(

export type EngineSettings = {
enabled: boolean;
maxDepth: number;
go: GoMode;
cores: number;
numberLines: number;
extraOptions: { name: string, value: string }[];
};

export const tabEngineSettingsFamily = atomFamily(
({ tab, engine }: { tab: string; engine: string }) =>
atom<EngineSettings>({
enabled: false,
maxDepth: 24,
go: {
t: "Depth",
c: 24,
},
cores: 2,
numberLines: 3,
extraOptions: [],
}),
(a, b) => a.tab === b.tab && a.engine === b.engine
);
Expand Down
3 changes: 2 additions & 1 deletion src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ bestMovesPayload: "plugin:tauri-specta:best-moves-payload"
export type AnalysisOptions = { fen: string; annotateNovelties: boolean; referenceDb: string | null }
export type BestMoves = { depth: number; score: Score; uciMoves: string[]; sanMoves: string[]; multipv: number; nps: number }
export type BestMovesPayload = { bestLines: BestMoves[]; engine: string; tab: string }
export type EngineOptions = { multipv: number; threads: number; fen: string }
export type EngineOption = { name: string; value: string }
export type EngineOptions = { multipv: number; threads: number; fen: string; extraOptions: EngineOption[] }
export type GoMode = { t: "Depth"; c: number } | { t: "Time"; c: number } | { t: "Nodes"; c: number } | { t: "Infinite" }
export type Score = { type: "cp"; value: number } | { type: "mate"; value: number }

Expand Down
60 changes: 26 additions & 34 deletions src/components/panels/analysis/BestMoves.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { activeTabAtom, tabEngineSettingsFamily } from "@/atoms/atoms";
import { commands, events } from "@/bindings";
import { TreeDispatchContext } from "@/components/common/TreeStateContext";
import { BestMoves, swapMove } from "@/utils/chess";
import { Engine } from "@/utils/engines";
import { unwrap } from "@/utils/invoke";
import { useThrottledEffect } from "@/utils/misc";
import { formatScore } from "@/utils/score";
import {
Accordion,
ActionIcon,
Box,
createStyles,
Group,
Progress,
Skeleton,
Stack,
Table,
Text,
Tooltip,
createStyles,
useMantineTheme,
} from "@mantine/core";
import { useToggle } from "@mantine/hooks";
Expand All @@ -19,25 +27,18 @@ import {
IconSettings,
IconTargetArrow,
} from "@tabler/icons-react";
import { Chess } from "chess.js";
import { useAtom, useAtomValue } from "jotai";
import {
startTransition,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { BestMoves, swapMove } from "@/utils/chess";
import { Engine } from "@/utils/engines";
import { invoke, unwrap } from "@/utils/invoke";
import { useThrottledEffect } from "@/utils/misc";
import { TreeDispatchContext } from "@/components/common/TreeStateContext";
import EngineSettings from "./EngineSettings";
import { Chess } from "chess.js";
import { match } from "ts-pattern";
import AnalysisRow from "./AnalysisRow";
import { useAtom, useAtomValue } from "jotai";
import { activeTabAtom, tabEngineSettingsFamily } from "@/atoms/atoms";
import { formatScore } from "@/utils/score";
import { commands, events } from "@/bindings";
import EngineSettings from "./EngineSettings";

const useStyles = createStyles((theme) => ({
subtitle: {
Expand Down Expand Up @@ -80,7 +81,10 @@ export default function BestMovesComponent({
const { classes } = useStyles();
const depth = engineVariations[0]?.depth ?? 0;
const nps = Math.floor(engineVariations[0]?.nps / 1000 ?? 0);
const progress = (depth / settings.maxDepth) * 100;
const progress = match(settings.go)
.with({ t: "Depth" }, ({ c }) => (depth / c) * 100)
.with({ t: "Infinite" }, () => 99.9)
.otherwise(() => 0);
const theme = useMantineTheme();

useEffect(() => {
Expand Down Expand Up @@ -125,19 +129,12 @@ export default function BestMovesComponent({
setEngineVariation([]);
} else {
commands
.getBestMoves(
engine.path,
activeTab!,
{
t: "Depth",
c: settings.maxDepth,
},
{
fen: threat ? swapMove(fen) : fen,
multipv: settings.numberLines,
threads: 2 ** settings.cores,
}
)
.getBestMoves(engine.path, activeTab!, settings.go, {
fen: threat ? swapMove(fen) : fen,
multipv: settings.numberLines,
threads: settings.cores,
extraOptions: settings.extraOptions,
})
.then((res) => {
unwrap(res);
});
Expand All @@ -150,7 +147,7 @@ export default function BestMovesComponent({
[
settings.enabled,
settings.cores,
settings.maxDepth,
settings.go,
settings.numberLines,
threat,
fen,
Expand Down Expand Up @@ -245,9 +242,7 @@ export default function BestMovesComponent({
</Box>
<EngineSettings
settingsOn={settingsOn}
cores={settings.cores}
maxDepth={settings.maxDepth}
numberLines={settings.numberLines}
settings={settings}
setSettings={setSettings}
/>

Expand Down Expand Up @@ -297,10 +292,7 @@ export default function BestMovesComponent({
</>
),
[
settings.enabled,
settings.cores,
settings.maxDepth,
settings.numberLines,
settings,
theme.primaryColor,
isGameOver,
engine.name,
Expand Down
10 changes: 7 additions & 3 deletions src/components/panels/analysis/CoresSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Slider } from "@mantine/core";
import { useState } from "react";
import { useEffect, useState } from "react";

export default function CoresSlide({
value,
Expand All @@ -8,7 +8,7 @@ export default function CoresSlide({
value: number;
setValue: (v: number) => void;
}) {
const [tempValue, setTempValue] = useState(value);
const [tempValue, setTempValue] = useState(Math.log2(value));
const MARKS = [
{ value: 0 },
{ value: 1 },
Expand All @@ -19,14 +19,18 @@ export default function CoresSlide({
{ value: 6 },
];

useEffect(() => {
setTempValue(Math.log2(value));
}, [value]);

return (
<>
<Slider
min={0}
max={Math.log2(navigator.hardwareConcurrency)}
value={tempValue}
onChange={setTempValue}
onChangeEnd={setValue}
onChangeEnd={(v) => setValue(2 ** v)}
marks={MARKS}
label={(value) => 2 ** value}
/>
Expand Down
32 changes: 26 additions & 6 deletions src/components/panels/analysis/DepthSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { GoMode } from "@/bindings";
import { Slider } from "@mantine/core";
import { useState } from "react";
import { useEffect, useState } from "react";

export default function DepthSlider({
value,
setValue,
}: {
value: number;
setValue: (v: number) => void;
value: GoMode;
setValue: (v: GoMode) => void;
}) {
const [tempValue, setTempValue] = useState(value);
const MARKS = [
Expand All @@ -18,14 +19,33 @@ export default function DepthSlider({
{ value: 60 },
];

useEffect(() => {
setTempValue(value);
}, [value]);

const v = tempValue.t === "Infinite" ? 60 : tempValue.c;
const handleSliderChange = (v: number, setState: (v: GoMode) => void) => {
if (v === 60) {
setState({
t: "Infinite",
});
} else {
setState({
t: "Depth",
c: v,
});
}
};

return (
<>
<Slider
min={10}
max={60}
value={tempValue}
onChange={setTempValue}
onChangeEnd={setValue}
value={v}
label={(v) => (v === 60 ? "Infinite" : v)}
onChange={(v) => handleSliderChange(v, setTempValue)}
onChangeEnd={(v) => handleSliderChange(v, setValue)}
marks={MARKS}
/>
</>
Expand Down
Loading

0 comments on commit e1b4fa5

Please sign in to comment.