Problem implementing alpha-beta pruning for chess engine - artificial-intelligence

I have been working on a chess engine recently, and I am ready to implement some kind of AI to actually play the game (search positions). I have written an alpha-beta pruning algorithm, but it doesn't return the best moves when i test it.
The code for the alpha-beta search is:
float Search::alphabeta(S_BOARD* pos, S_SEARCHINFO *info, int depth, float alpha, float beta){
if (depth == 0) {
info->nodes++;
return eval::staticEval(pos);
}
info->nodes++;
S_MOVELIST list;
MoveGeneration::validMoves(pos, list);
float value = 0;
S_MOVE bestMove;
bestMove.move = NOMOVE;
bestMove.score = 0;
float prevBound = (pos->whitesMove == WHITE) ? alpha : beta;
int pvMove = TT::probeMove(pos);
if (pvMove != NOMOVE) {
for (int i = 0; i < list.count; i++) {
if (list.moves[i].move == pvMove) {
list.moves[i].score = 20000000;
break;
}
}
}
if (pos->whitesMove == WHITE) {
value = -INFINITE;
for (int moveNum = 0; moveNum < list.count; moveNum++) {
pickNextMove(moveNum, &list);
MoveGeneration::makeMove(*pos, list.moves[moveNum].move);
value = max(value, alphabeta(pos, info, depth - 1, alpha, beta));
MoveGeneration::undoMove(*pos);
if (value > alpha) {
if (value >= beta) {
if (moveNum == 0) {
info->fhf++;
}
info->fh++;
break;
}
alpha = value;
bestMove = list.moves[moveNum];
}
}
if (pos->is_checkmate) {
return -MATE + pos->ply;
}
else if (pos->is_stalemate) {
return 0;
}
if (alpha != prevBound) {
TT::storePvMove(pos, bestMove);
}
return value;
}
else {
value = INFINITE;
for (int moveNum = 0; moveNum < list.count; moveNum++) {
pickNextMove(moveNum, &list);
MoveGeneration::makeMove(*pos, list.moves[moveNum].move);
value = min(value, alphabeta(pos, info, depth - 1, alpha, beta));
MoveGeneration::undoMove(*pos);
if (value < beta){
if (beta <= alpha) {
if (moveNum == 0) {
info->fhf++;
}
info->fh++;
break;
}
beta = value;
bestMove = list.moves[moveNum];
}
}
if (pos->is_checkmate) {
return MATE - pos->ply;
}
else if (pos->is_stalemate) {
return 0;
}
if (beta != prevBound) {
TT::storePvMove(pos, bestMove);
}
return value;
}
(MoveGeneration is a namespace, so it isn't a problem with calling functions outside of object instances.)
I run the function inside an iterative deepening function, which is as follows:
float Search::searchPosition(S_BOARD* pos, S_SEARCHINFO *info){
clearForSearch(pos, info);
float score = -INFINITE;
int bestMove = NOMOVE;
int pvMoves = 0;
// Iterative deepening.
for (int currDepth = 1; currDepth <= info->depth; currDepth++){
auto start = std::chrono::high_resolution_clock::now();
score = alphabeta(pos, info, currDepth, -INFINITE, INFINITE);
auto end = std::chrono::high_resolution_clock::now();
pvMoves = TT::getPvLine(pos, currDepth);
bestMove = pos->pvArray[0];
std::chrono::duration<double> elapsed = end - start;
std::cout << "[+] Depth: " << currDepth << " score: " << score << " move: " << printMove(bestMove)
<< " nodes: " << info->nodes << " kN/s: " << (info->nodes/elapsed.count())/1000 << std::endl;
std::cout << "pv";
for (int i = 0; i < pvMoves; i++){
std::cout << " " << printMove(pos->pvArray[i]);
}
std::cout << std::endl;
std::cout << "Ordering: " << info->fhf/info->fh << std::endl;
}
return score;}
Can anyone please help me by pointing out potential errors i have made?
Thank you for your help, and just tell me if i need to upload more of my code.

it doesn't return a move since you are only returning the value at the bottom of your alpha/beta function. In the original call, put this:
move, score = alphabeta(pos, info, currDepth, -INFINITE, INFINITE);
In your depth == 0, checkmate and stalemate you return:
return None, eval
At the end of your two player functions (minimizing and maximizing player) you return:
return move, value
Finally, when you make the recursive calls from your two player functions, you need to only obtain the value. I am not sure about your programming language, but in e.g. Python you put a [1] at the end to just get the value and not the move, something like this:
value = max(value, alphabeta(pos, info, depth - 1, alpha, beta))[1]

Related

C Return Going Back To Loop Predicate Not Ending The Function

Hello I have the following code in C and when the code clause:
if (idx - 1 == 0) {
return opening_address;
}
executes it returns to the While loop's predicate rather than actually returning and ending the function. The code does get hit and likewise for the other return statement in the loop and both have the same behavior and basically do a continue action.
int find_smallest_free_block(int block_size) {
int opening_address = 0;
int idx = 0;
while (idx + lowestPower <= highestPower) {
if (powers[idx].hasHoles) {
if (powers[idx].size > block_size) {
opening_address = powers[idx].head_p->start_address;
// Add 2 holes to the next power down
int try = 0;
try = addHole(idx - 1, powers[idx].head_p->start_address, powers[idx].head_p->end_address / 2);
try = addHole(idx - 1, powers[idx].head_p->end_address / 2, powers[idx].head_p->end_address);
// Remove hole at power
try = removeHole(idx, powers[idx].head_p->start_address);
if (idx - 1 == 0) {
return opening_address; // cant make holes at lowest level
}
idx -= 1;
continue;
} else {
return opening_address;
}
}
idx += 1;
}
return opening_address;
}

C, help continue..? in while loop - polynomial ADT

When the coef is 0, I used continue to not print, but only printTerm(a) comes out and the printTerm(b) part does not come out.
When I delete the (if & continue) statement, both printTerm(a) and printTerm(b) appear, so it seems that there is a problem here (if & continue) statement.
How can I solve this?
int main() {
a[0].coef = 2;
a[0].expon = 1000; // 2x^1000
a[1].coef = 1;
a[1].expon = 2; // x^2
a[2].coef = 1;
a[2].expon = 0; // 1
b[0].coef = 1;
b[0].expon = 4; // x^4
b[1].coef = 10;
b[1].expon = 3; // 10x^3
b[2].coef = 3;
b[2].expon = 2; // 3x^2
b[2].coef = 1;
b[2].expon = 0; // 1
printTerm(a);
printTerm(b);
return 0;
}
void printTerm(polynomial *p) {
int i=0;
printf("polynomial : ");
while(p[i].expon != -1) {
if(p[i].coef == 0) continue;
printf("%dx^%d", p[i].coef, p[i].expon);
i++;
if(p[i].expon != -1 && p[i].coef > 0) printf(" + ");
}
printf("\n");
}
Because you only increment i if p[i].coef is not equal to 0.
If p[i].coef == 0 it skips the increment part and function is stuck in infinite loop, always checking the same array item.
EDIT:
Way to fix this:
Instead of if(p[i].coef == 0) continue; use:
if (p[i].coef == 0)
{
i++;
continue;
}
This way while loop evaluetes next array item instead of being stuck on the same.

Merkle tree - setting data and comparing results

This code is for Merkle trees and the full code can be accesed on this link https://github.com/weichaoguo/merkle-tree/blob/master/merkle_tree.c
The part that confuses me is within these block of code:
So I was wondering why is there (1 << a->tree_height)-1 and all the other lines that contain <<. What is << used here for?
//compare two merkle trees from node i
//make sure the two trees in same height
//return different data block number
//if no differnece return 0
int tree_cmp(merkle_tree *a, merkle_tree *b, size_t i) {
int cmp;
if (i > (1<<a->tree_height)-1)
return -1;
if (memcmp(a->nodes[i].hash, b->nodes[i].hash, a->hash_size) != 0) {
if (i<<1 > (1<<a->tree_height)-1)
return i - (1 << (a->tree_height - 1)) + 1;
else {
cmp = tree_cmp(a, b, i<<1);
if (cmp == 0)
return tree_cmp(a, b, (i<<1)+1);
else
return cmp;
}
}
else
return 0;
}
// set tree data with specific block number
//
int set_tree_data(merkle_tree *mt, size_t block_num, char *data) {
if (block_num > mt->data_blocks)
return -1;
size_t i = (1 << (mt->tree_height - 1)) + block_num - 1;
if (mt->nodes[i].data)
free(mt->nodes[i].data);
mt->nodes[i].data = data;
if (hash_node(mt, i) == -1)
return -1;
for (i>>=1; i>0; i>>=1)
if (hash_node(mt, i) == -1)
return -1;
return 0;
}
It's a bitshift. (1 << x) - 1 has x ones set.

PID Line follower with tank treads

I have made a (pretty bad) line follower.
Here is a sketch to roughly know the shape of the robot and location of the treads and sensors
[-] 0 0 [-] // 0 = color sensor
[-]------[-] // - = robot body
[-]------[-] // [-] = tank tread
[-] [-]
Here's what it does:
get Red, Green & Blue, make average of sensor 1 readings, do the same for 2
subtract to get value
this value will go through the PID part
steer with calculated steering
repeat (all of this is in a loop)
I use RGB and not reflected intensity (which is what is commonly used), because sometimes I need to detect if there's green color under the sensor (if there is, turn).
The real problem comes with the steering part. Unfortunately, it only accelerates a motor, meaning that in very tight turns we just lose the line.
Optimally, it should compensate a bit with the other motor (maybe going in the other direction?), but I am not sure how to calculate the speed of the motor, nor how to enforce this very strict line following policy.
Here is the code (I am also very grateful for any kind of tips on how to clean up the code! This is my first project in C :D ). I am not asking to read it all (it is pretty long), you could also just look at the steering function, and work your way back to rawFollowLine, this should hopefully shorten the code.
void rawFollowLine(int speed, float Kp, float Ki, float Kd){
_checkInit();
set_sensor_mode(sn_lx_color, "RGB-RAW");
set_sensor_mode(sn_rx_color, "RGB-RAW");
//printAllSensors();
int wasBlackCounter = 0;
int wasBlack = 0;
int lastBlack = 0;
for (int i = 0; i < 2000; i++)
{
if (isTerminating == 1)
{
killMotors(0);
break;
}
int greenCheck = rawGreenCheck(&wasBlack, &wasBlackCounter, &lastBlack);
if (wasBlack == 1){
wasBlackCounter++;
if (wasBlackCounter > 50){
wasBlackCounter = 0;
wasBlack = 0;
}
}
if (greenCheck == 1)
{
// lx is green
killMotors(1);
usleep(400 * 1000);
drive(200, 70);
waitIfMotorIsRunning();
killMotors(1);
pivotTurn(-90);
}
else if (greenCheck == 2)
{
// rx is green
killMotors(1);
usleep(400 * 1000);
drive(200, 70);
waitIfMotorIsRunning();
killMotors(1);
pivotTurn(90);
}
else if (greenCheck == 3)
{
// both rx and lx are green
killMotors(1);
turn(180);
}
else if (greenCheck == 5)
{
if(lastBlack == 2)
{
lastBlack = 0;
drive(100, -200);
//pivotTurn(50);
}
else if (lastBlack == 1)
{
lastBlack = 0;
drive(100, -200);
//pivotTurn(-50);
} else {
pidLineRaw(speed, Kp, Ki, Kd, &lastBlack);
}
}
else
{
pidLineRaw(speed, Kp, Ki, Kd, &lastBlack);
}
}
killMotors(1);
}
int rawGreenCheck(int *wasBlack, int *wasBlackCounter, int *lastBlack)
{
// Some documentation
// return nums:
// 3 = double green
// 2 = right green
// 1 = left green
// 0 = no green
int lx_red;
int lx_green;
int lx_blue;
int rx_red;
int rx_green;
int rx_blue;
get_sensor_value(0, sn_lx_color, &lx_red);
get_sensor_value(0, sn_rx_color, &rx_red);
get_sensor_value(1, sn_lx_color, &lx_green);
get_sensor_value(1, sn_rx_color, &rx_green);
get_sensor_value(2, sn_lx_color, &lx_blue);
get_sensor_value(2, sn_rx_color, &rx_blue);
//printf("rx_red %d\n", rx_red);
rx_red = (rx_red * rx_ratio_r);
rx_green = (rx_green * rx_ratio_g);
rx_blue = (rx_blue * rx_ratio_b);
//printf("rx_red (again) %d\n", rx_red);
if(
lx_red < 55 &&
lx_green > 90 &&
lx_blue < 55 &&
rx_red < 55 &&
rx_green > 90 &&
rx_blue < 55
)
{
// rx and lx see green
if (*wasBlack == 1)
{
// Apparently we crossed an intersection!
printf("Apparently we crossed an intersection!\n");
// We need to go straight.
*wasBlack = 0;
*wasBlackCounter = 0;
return 0;
}
else
{
return 3;
}
}
else if(lx_red < 55 && lx_green > 90 && lx_blue < 55)
{
// lx sees green
return 1;
}
else if(rx_red < 55 && rx_green > 90 && rx_blue < 55)
{
// rx sees green
return 2;
}
else if(rx_red < 50 && rx_green < 50 && rx_blue < 50 && lx_red < 50 && lx_green < 50 && lx_blue < 50)
{
// rx and lx see black
// this is needed if the intersection has the green tiles after the black line
printf("We are on the line? Is this an intersection?\n");
*wasBlack = 1;
return 0;
}
else if(lx_red < 55 && lx_green < 55 && lx_blue < 55)
{
// lx = right sees black
// this is needed if the intersection has the green tiles after the black line
//printf("We are on the line? Is this an intersection?\n");
killMotor(1, motor[R]);
rotateTillBlack(motor[L], sn_rx_color);
//printf("ASS2\n");
return 0;
}
else if(rx_red < 55 && rx_green < 55 && rx_blue < 55)
{
// rx = left sees black
killMotor(1, motor[L]);
rotateTillBlack(motor[R], sn_lx_color);
//printf("ASS1\n");
return 0;
}
//*lx_color_status = 0;
//*rx_color_status = 0;
*lastBlack = 0;
return 0;
}
void pidLineRaw(int speed, float Kp, float Ki, float Kd, int *lastBlack)
{
int red_lx_color;
int red_rx_color;
int green_lx_color;
int green_rx_color;
int blue_lx_color;
int blue_rx_color;
int lx_color;
int rx_color;
int last_error = 0;
int integral = 0;
int derivative = 0;
//float Kp = 0.1;
//float Ki = 0;
//float Kd = 0;
//set_sensor_mode(sn_lx_color, "COL-REFLECT");
//set_sensor_mode(sn_rx_color, "COL-REFLECT");
get_sensor_value(0, sn_lx_color, &red_lx_color);
get_sensor_value(0, sn_rx_color, &red_rx_color);
get_sensor_value(1, sn_lx_color, &green_lx_color);
get_sensor_value(1, sn_rx_color, &green_rx_color);
get_sensor_value(2, sn_lx_color, &blue_lx_color);
get_sensor_value(2, sn_rx_color, &blue_rx_color);
lx_color = (red_lx_color + green_lx_color+ blue_lx_color)/3;
rx_color = ( (red_rx_color*rx_ratio_r) + (green_rx_color*rx_ratio_g) + (blue_rx_color*rx_ratio_b))/3;
if(*lastBlack == 0)
{
int error = lx_color - rx_color;
integral = integral + error;
derivative = error - last_error;
last_error = error;
int steering_val = (error * Kp) + (integral * Ki) + (derivative * Kd);
// printf("error: %d\nsteering: %d\n",error, steering_val);
move_steering(-steering_val, speed, 1, 0);
} else if (*lastBlack == 1)
{
printf("lx_color_status\n");
move_steering(35, speed, 1, 0);
move_steering(-2, speed, 1, 0);
}
else if (*lastBlack == 2)
{
printf("rx_color_status\n");
move_steering(-35, speed, 1, 0);
move_steering(2, speed, 1, 0);
}
else
{
printf("HMMM: %d\n", *lastBlack);
exit(666);
}
}
static void _getSteeringSpeed(int speed, int *lx_speed, int *rx_speed, int steering)
{
if(steering > 100 || steering < -100)
{
printf("Yo wtf steering is %d\n", steering);
}
else
{
int speed_factor = (50 - abs(steering)) / 50;
*lx_speed = speed;
*rx_speed = speed;
if(steering >= 0)
{
*rx_speed = *rx_speed * speed_factor;
}
else
{
*lx_speed = *lx_speed * speed_factor;
}
}
}
Some parts are omitted, yes, they are not required to solve the problem.
I am also extremely sorry as there might be unused variables and such. I am working on refactoring the project, I'll update the post when I'm done.
So, summing everything up, I need to make sure that the steering part properly turns and follows the line. How do I do that? Is the code that I wrote even suitable? I'm guessing the steering itself might need some sort of feedback loop, to check if it's on the line?

How to get loop bounds in LLVM?

I want to get the loop bounds by using LLVM API. Here is the part of the code as follows. I don't know whether it is right to get the bounds. So, is there any other situation that I haven't thought about?
typedef std::vector<Loop*> LoopNest;
typedef std::vector<const Value*> LoopNestBounds;
...
void getLoopNestBounds(const LoopNest &Nest, LoopNestBounds &LBounds) {
ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
for (unsigned d = 0, n = Nestsize(); d != n; ++d) {
if (SE.hasLoopInvariantBackedgeTakenCount(Nest[d])) {
const SCEV *C = SE.getBackedgeTakenCount(Nest[d]);
const SCEVConstant *CC = dyn_cast<const SCEVConstant>(C);
LBounds.push_back(CC->getValue());
errs() << CC->getValue()->getValue() << " iterations\n";
}
else {
LBounds.push_back(0);
errs() << "---- 0 iterations for the nest ----" << "\n";
}
}
}
Note: the version of LLVM is 3.0.

Resources