Ejemplo n.º 1
0
void TestIterativeValue(int depth) {
	// setup book and cache
	CCache acache(2);
	cache = &acache;
	const int nEmpty = 22;
	CBook* oldBook = book;
	book = NULL;
	InitializeCache();
	SetBookHeights(nEmpty);
	mpcs = CMPCStats::GetMPCStats('J','A',5);

	// testing
	const COsGame osGame = LoadTestGames().at(0);
	const CQPosition testPosition = PositionFromEmpties(osGame, nEmpty);
	std::vector<CMoveValue> mvs = createMoves(testPosition);
	Pos2 pos2;
	pos2.Initialize(testPosition.BitBoard(), testPosition.BlackMove());
	u4 nValued=0;
	std::vector<CMoveValue> mvsEvaluated;
	ValueMulti(pos2, depth, -kInfinity, kInfinity, 4, 1, mvs, false, false, mvsEvaluated, nValued);
//	cout << " nValued = " << nValued << "\n";
//	cout << " mvsEvaluated = \n";
//	for (int i=0; i<mvsEvaluated.size(); i++) {
//		CMoveValue mv = mvsEvaluated.at(i);
//		cout << "new CMoveValue(" << mv.move << "," << mv.value << "),\n";
//	}
//	cout << "\n";

	// tear down book and cache
	book = oldBook;
	cache = NULL;
}
Ejemplo n.º 2
0
static void TestMakeMove() {
	CQPosition pos;
	char sBoard[65];
	pos.Initialize();

	pos.MakeMove(CMove("F5"));
	assertStringEquals("---------------------------O*------***--------------------------", pos.GetSBoard(sBoard));
	assertFalse(pos.BlackMove());
	assertEquals(59, pos.NEmpty());
	assertEquals(1, pos.NMover());
}
Ejemplo n.º 3
0
void AnalyzePosition(CPlayerComputer* computer, CQPosition& pos) {
	CMVK mvk;

	std::cout << pos.NEmpty();
	computer->Clear();

	u4 fNeed=CSearchInfo::kNeedMove+CSearchInfo::kNeedValue+CSearchInfo::kNeedMPCStats;
	CSearchInfo si = computer->DefaultSearchInfo(pos.BlackMove(), fNeed, INFINITE_TIME, 0);
	computer->GetChosen(si, pos, mvk, true);
	printf("\n");
	std::cerr << ".";
}
Ejemplo n.º 4
0
// save values of positions in a file
//	 format:
//	bitboard (white just moved),  value (to black), pass flag (0,1,2), best move (for mover)
// precondition: no book (unless you want to add these positions to a book)
void CalcPosValues(CPlayerComputer* computer, bool fAppend) {
	FILE* fpCapture, *fpPV;
	CQPosition pos;
	CMVK mvk;
	CMoves moves;
	int i, quantities[60], pass, nCalced, iAppend;
	CBitBoard bb;
	time_t tStart;

	if (fAppend) {
		// figure out what number to start on
		std::string fn(fnBaseDir);
		fn+=sPVFile;
		fpPV=fopen(fn.c_str(), "rb");
		int result = fseek(fpPV, 0, SEEK_END);
		int nFileSize=ftell(fpPV);
		int nRecordSize=sizeof(bb.mover)+sizeof(bb.empty)+
			sizeof(mvk.value)+sizeof(pass)+sizeof(mvk.move);
		if (nFileSize%nRecordSize) {
			iAppend=0;
			std::cerr << "Previous .pv file is corrupt, starting again: file size = "
				<< nFileSize << ", record size = " << nRecordSize
				<< ", file size % record size = " << nFileSize%nRecordSize << "\n";
		}
		else {
			iAppend=nFileSize/nRecordSize;
			std::cerr << "Appending to previous file with " << iAppend << " elements\n";
		}
		fclose(fpPV);
		fpPV=NULL;
	}
	else
		iAppend=0;

	// clear count
	for (i=0;i<60; i++)
		quantities[i]=0;

	std::string fn(fnBaseDir);
	fn+=fnCapture;
	fpCapture=fopen(fn.c_str(), "rb");
	if (fpCapture) {
		std::string fn(fnBaseDir);
		fn+=sPVFile;
		fpPV=fopen(fn.c_str(), iAppend?"a+bc":"wbc");
		if (fpPV) {
			for (nCalced=0; bb.Read(fpCapture); ) {
				if (bb.NEmpty()>60)
					continue;

				quantities[bb.NEmpty()]++;

				// print progress
				if (nCalced%200 == 0) {
					fflush(fpPV);
					if (nCalced%10000 == 0)
						fprintf(stderr, "\nPosition %dk ",nCalced/1000);
					else
						fprintf(stderr, ".");
				}
				//printf("%d", bb.NEmpty());

				if (nCalced>=iAppend) {
					pos.Initialize(bb,true);
					pass=pos.CalcMovesAndPass(moves);

					// if terminal position, calc value
					if (pass==2) {
						mvk.value=-pos.TerminalValue();
						mvk.move.Set(-1);
					}

					// nonterminal position, get value from computer
					else {
						tStart=time(0);

						CSearchInfo si = computer->DefaultSearchInfo(pos.BlackMove(),
																	 CSearchInfo::kNeedMove + CSearchInfo::kNeedValue,
																	 INFINITE_TIME, 0);
						computer->GetChosen(si, pos, mvk, true);
						if (pass==1)
							mvk.value=-mvk.value;
						//printf("NEmpty = %2d; value = %4d; move = ",pos.NEmpty(),mvk.value);
						//mvk.move.Print();
						//printf("; pass = %d; time = %2d\n",pass, time(0)-tStart);
					}

					// write to file
					if (!bb.Write(fpPV)
						|| !fwrite(&mvk.value, sizeof(mvk.value), 1, fpPV)
						|| !fwrite(&pass, sizeof(pass), 1, fpPV)
						|| !fwrite(&mvk.move, sizeof(mvk.move), 1, fpPV)) {
						std::string fn(fnBaseDir);
						fn+=sPVFile;
						std::cerr << "can't write to file " << fn << "\n";
						assert(0);
						_exit(1);
					}
				}
				nCalced++;
			}
			fclose(fpPV);
		}
		fclose(fpCapture);
	}
	// print count
	int sum=0;
	for (i=0;i<60; i++) {
		sum+=quantities[i];
		printf("%d:%d\n",i,quantities[i]);
	}
	printf("%d total positions\n",sum);
}