Skip to content
Permalink
Browse files

Complete restructuring take 2. Bench: 3179538.

  • Loading branch information...
Stefano80 committed Mar 14, 2019
1 parent 8bf3ef7 commit 3bcae5c494fd5d42405aa555a1db8493e0751a41
Showing with 22 additions and 35 deletions.
  1. +7 −8 src/search.cpp
  2. +14 −26 src/thread.cpp
  3. +1 −1 src/thread.h
@@ -1064,22 +1064,21 @@ namespace {
}

// Predict using a perceptron
features[0] = float(ttCapture) * 1000;
features[1] = float(depth) * 1000;
features[2] = float(captureOrPromotion) * 1000;
features[3] = float(cutNode) * 1000;

features[0] = float(inCheck) ;
features[1] = float(givesCheck) ;
features[2] = float(captureOrPromotion) ;
features[3] = float(cutNode);
prediction = thisThread->infer(features);

if (thisThread->perceptronAccuracy > 10000000)
if (thisThread->perceptronAccuracy > 4700)
r -= prediction * ONE_PLY;

dbg_hit_on(thisThread->perceptronAccuracy > 4800);

Depth d = std::max(newDepth - std::max(r, DEPTH_ZERO), ONE_PLY);

value = -search<NonPV>(pos, ss+1, -(alpha+1), -alpha, d, true);

int result = value > alpha;
int result = (value > alpha);

// Train the perceptron if needed
thisThread->train(features, 1e-2, prediction, result);
@@ -68,10 +68,10 @@ void Thread::clear() {

continuationHistory[NO_PIECE][0]->fill(Search::CounterMovePruneThreshold - 1);

perceptronAccuracy = 0;
for (int d1 = 0; d1 <= PercInput; d1++)
for (int d2 = 0; d2 < PercOutput; d2++)
{
perceptronWeights[d1][d2] = 0;
perceptronWeights[d1] = 0;
}
}

@@ -125,37 +125,25 @@ void Thread::idle_loop() {
}

int Thread::infer(float input[PercInput]){
float bestFit = -10000000;
float internalStates[PercOutput];
int bestClass = 0;

for (int d1 = 0; d1 < PercOutput; d1++){
internalStates[d1] = perceptronWeights[0][d1]; // bias
for (int d2 = 0; d2 < PercInput; d2++){
internalStates[d1] += perceptronWeights[1 + d2][d1] * input[d2];
}
if (internalStates[d1] > bestFit){
bestFit = internalStates[d1];
bestClass = d1;
}

float x = perceptronWeights[PercInput]; // bias
for (int d = 0; d < PercInput; d++){
x += perceptronWeights[d] * input[d];
}
return bestClass;
return x > 100? 1 : 0;
}

void Thread::train(float input[PercInput], float rate, int prediction, int result){
int error = 0;

int error = (result - prediction);

perceptronAccuracy += 100 * (prediction == result);
perceptronAccuracy = 98 * perceptronAccuracy / 100;
if (prediction == result)
return;
for (int d1 = 0; d1 < PercOutput; d1++){
error = d1 == result? 1 : -1;
perceptronWeights[0][d1] += rate * error;
for (int d2 = 0; d2 < PercInput; d2++){
perceptronWeights[1 + d2][d1] += input[d2] * rate * error;
}
perceptronWeights[PercInput] += rate * error;
for (int d = 0; d < PercInput; d++){
perceptronWeights[d] += input[d] * rate * error;
}
}
}

/// ThreadPool::set() creates/destroys threads to match the requested number.
/// Created and launched threads will immediately go to sleep in idle_loop.
@@ -60,7 +60,7 @@ class Thread {
int infer(float input[PercInput]);
void train(float input[PercInput], float rate, int prediction, int result);

float perceptronWeights[PercInput + 1][PercOutput];
float perceptronWeights[PercInput + 1];
int perceptronAccuracy;

Pawns::Table pawnsTable;

0 comments on commit 3bcae5c

Please sign in to comment.
You can’t perform that action at this time.