Skip to content

Commit

Permalink
add custom settings to engine
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Oct 29, 2023
1 parent 18b5452 commit c74fb2f
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 46 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
2 changes: 2 additions & 0 deletions src/atoms/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export type EngineSettings = {
go: GoMode;
cores: number;
numberLines: number;
extraOptions: { name: string, value: string }[];
};

export const tabEngineSettingsFamily = atomFamily(
Expand All @@ -254,6 +255,7 @@ export const tabEngineSettingsFamily = atomFamily(
},
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
3 changes: 2 additions & 1 deletion src/components/panels/analysis/BestMoves.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export default function BestMovesComponent({
.getBestMoves(engine.path, activeTab!, settings.go, {
fen: threat ? swapMove(fen) : fen,
multipv: settings.numberLines,
threads: 2 ** settings.cores,
threads: settings.cores,
extraOptions: settings.extraOptions,
})
.then((res) => {
unwrap(res);
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
6 changes: 5 additions & 1 deletion src/components/panels/analysis/DepthSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GoMode } from "@/bindings";
import { Slider } from "@mantine/core";
import { useState } from "react";
import { useEffect, useState } from "react";

export default function DepthSlider({
value,
Expand All @@ -19,6 +19,10 @@ 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) {
Expand Down
Loading

0 comments on commit c74fb2f

Please sign in to comment.