Skip to content

Commit

Permalink
Merge pull request #244 from rooklift/napthalin_graph
Browse files Browse the repository at this point in the history
Centipawn based graph, with sigmoid squishing
  • Loading branch information
rooklift authored Sep 24, 2023
2 parents dd5c686 + 34dba0a commit c2709f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
29 changes: 18 additions & 11 deletions files/src/renderer/50_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,38 @@ const table_prototype = {
this.time = 0; // Stat sent by engine
this.limit = null; // The limit of the last search that updated this.
this.terminal = null; // null = unknown, "" = not terminal, "Non-empty string" = terminal reason
this.eval = null; // Used by grapher only, value from White's POV
this.eval_version = 0; // Which version (above) was used to generate the eval
this.graph_y = null; // Used by grapher only, value from White's POV between 0 and 1
this.graph_y_version = 0; // Which version (above) was used to generate the graph_y value
this.already_autopopulated = false;
},

get_eval: function() {
if (this.eval_version === this.version) {
return this.eval;
get_graph_y: function() {

// Naphthalin's scheme: based on centipawns.

if (this.graph_y_version === this.version) {
return this.graph_y;
} else {
let info = SortedMoveInfoFromTable(this)[0];
if (info && !info.__ghost && info.__touched && (this.nodes > 1 || this.limit === 1)) {
this.eval = info.board.active === "w" ? info.value() : 1 - info.value();
let cp = info.cp;
if (info.board.active === "b") {
cp *= -1;
}
this.graph_y = 1 / (1 + Math.pow(0.5, cp / 100));
} else {
this.eval = null;
this.graph_y = null;
}
this.eval_version = this.version;
return this.eval;
this.graph_y_version = this.version;
return this.graph_y;
}
},

set_terminal_info: function(reason, ev) { // ev is ignored if reason is "" (i.e. not a terminal position)
if (reason) {
this.terminal = reason;
this.eval = ev;
this.eval_version = this.version;
this.graph_y = ev;
this.graph_y_version = this.version;
} else {
this.terminal = "";
}
Expand Down
14 changes: 6 additions & 8 deletions files/src/renderer/51_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ const node_prototype = {
return ret;
},

eval_history: function() {
all_graph_values: function() {

// Call this on any node in the line will give the same result.

let ret = [];
let node = this;
let node = this.get_end();

while (node) {
ret.push(node.table.get_eval());
ret.push(node.table.get_graph_y());
node = node.parent;
}

Expand All @@ -147,10 +149,6 @@ const node_prototype = {
return this.get_end().node_history();
},

future_eval_history: function() {
return this.get_end().eval_history();
},

get_root: function() {

let node = this;
Expand Down Expand Up @@ -350,7 +348,7 @@ const node_prototype = {
terminal_reason: function() {

// Returns "" if not a terminal position, otherwise returns the reason.
// Also updates table.eval (for the graph) if needed.
// Also updates table.graph_y if needed.

if (typeof this.table.terminal === "string") {
return this.table.terminal;
Expand Down
31 changes: 14 additions & 17 deletions files/src/renderer/55_winrate_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function NewGrapher() {

let grapher = Object.create(null);

grapher.dragging = false; // Used by the event handlers in start.js

grapher.clear_graph = function() {
Expand Down Expand Up @@ -31,9 +31,8 @@ function NewGrapher() {
let width = graph.width; // After the above.
let height = graph.height;

let eval_list = node.future_eval_history();

this.draw_50_percent_line(width, height);
let eval_list = node.all_graph_values();
this.draw_horizontal_lines(width, height, [1/3, 2/3]);
this.draw_position_line(eval_list.length, node);

// We make lists of contiguous edges that can be drawn at once...
Expand Down Expand Up @@ -138,18 +137,21 @@ function NewGrapher() {
return {normal_runs, dashed_runs};
};

grapher.draw_50_percent_line = function(width, height) {
grapher.draw_horizontal_lines = function(width, height, y_fractions = [0.5]) {

// Avoid anti-aliasing... (FIXME: we assumed graph size was even)
let pixel_y_adjustment = config.graph_line_width % 2 === 0 ? 0 : -0.5;

graphctx.strokeStyle = "#666666";
graphctx.lineWidth = config.graph_line_width;
graphctx.setLineDash([config.graph_line_width, config.graph_line_width]);
graphctx.beginPath();
graphctx.moveTo(0, height / 2 + pixel_y_adjustment);
graphctx.lineTo(width, height / 2 + pixel_y_adjustment);
graphctx.stroke();

for (let y_fraction of y_fractions) {
graphctx.beginPath();
graphctx.moveTo(0, height * y_fraction + pixel_y_adjustment);
graphctx.lineTo(width, height * y_fraction + pixel_y_adjustment);
graphctx.stroke();
}
};

grapher.draw_position_line = function(eval_list_length, node) {
Expand All @@ -161,9 +163,8 @@ function NewGrapher() {
let width = graph.width;
let height = graph.height;

// Avoid anti-aliasing with x value, line up with 50 percent line (above) with y value...
// Avoid anti-aliasing...
let pixel_x_adjustment = config.graph_line_width % 2 === 0 ? 0 : 0.5;
let pixel_y_adjustment = config.graph_line_width % 2 === 0 ? 0 : -0.5;

let x = Math.floor(width * node.depth / node.graph_length_knower.val) + pixel_x_adjustment;

Expand All @@ -172,14 +173,10 @@ function NewGrapher() {
graphctx.setLineDash([config.graph_line_width, config.graph_line_width]);

graphctx.beginPath();
graphctx.moveTo(x, height / 2 + pixel_y_adjustment - config.graph_line_width - 1);
graphctx.lineTo(x, 0);
graphctx.stroke();

graphctx.beginPath();
graphctx.moveTo(x, height / 2 + pixel_y_adjustment + config.graph_line_width + 1);
graphctx.moveTo(x, 0);
graphctx.lineTo(x, height);
graphctx.stroke();

};

grapher.node_from_click = function(node, event) {
Expand Down

0 comments on commit c2709f7

Please sign in to comment.