Go to the documentation of this file.00001
00002
00003 #include "osl/eval/ml/openMidEndingEval.h"
00004 #include "osl/progress/ml/newProgress.h"
00005 #include "osl/record/csaRecord.h"
00006 #include "osl/container/pieceValues.h"
00007 #include "osl/state/numEffectState.h"
00008 #include "osl/oslConfig.h"
00009 #include "osl/pieceStand.h"
00010 #include "osl/record/kanjiPrint.h"
00011 #include <iostream>
00012 #include <iomanip>
00013 #include <cstdlib>
00014 #include <cstdio>
00015 #include <unistd.h>
00016
00017 using namespace osl;
00018 using namespace osl::eval;
00019
00020 void usage(const char *prog)
00021 {
00022 using namespace std;
00023 cerr << "Usage: " << prog << " csa-filename"
00024 << endl;
00025 exit(1);
00026 }
00027
00028 void show(const char *filename);
00029 int verbose = 0;
00030 int piece_estimate_level = 2;
00031
00032 int main(int argc, char **argv)
00033 {
00034 const char *program_name = argv[0];
00035 bool error_flag = false;
00036
00037 extern char *optarg;
00038 extern int optind;
00039 char c;
00040 while ((c = getopt(argc, argv, "e:vh")) != EOF)
00041 {
00042 switch(c)
00043 {
00044 case 'e':
00045 if (atoi(optarg) > 0)
00046 piece_estimate_level = atoi(optarg);
00047 break;
00048 default: error_flag = true;
00049 }
00050 }
00051 argc -= optind;
00052 argv += optind;
00053
00054 if (error_flag)
00055 usage(program_name);
00056
00057 eval::ml::OpenMidEndingEval::setUp();
00058 progress::ml::NewProgress::setUp();
00059
00060 for (int i=0; i<argc; ++i)
00061 {
00062 show(argv[i]);
00063 }
00064 }
00065
00066 void make1(const NumEffectState& state, const eval::ml::OpenMidEndingEval& eval, PieceValues& values)
00067 {
00068 for (int i=0; i<Piece::SIZE; ++i) {
00069 const Piece piece = state.pieceOf(i);
00070 if (! piece.isOnBoard() || unpromote(piece.ptype()) == KING)
00071 continue;
00072 const NumEffectState removed(state.emulateCapture(piece, piece.owner()));
00073 const eval::ml::OpenMidEndingEval eval_removed(removed);
00074 values[piece.number()] = eval.value() - eval_removed.value();
00075 }
00076 }
00077
00078 void make2(const NumEffectState& state, const eval::ml::OpenMidEndingEval& eval, PieceValues& values)
00079 {
00080 CArray<int,40> count = {{ 0 }};
00081 for (int i=0; i<Piece::SIZE; ++i) {
00082 values[i] = 0;
00083 const Piece piece = state.pieceOf(i);
00084 if (! piece.isOnBoard() || unpromote(piece.ptype()) == KING)
00085 continue;
00086 const NumEffectState removed(state.emulateCapture(piece, piece.owner()));
00087 const eval::ml::OpenMidEndingEval eval_removed(removed);
00088 for (int j=0; j<Piece::SIZE; ++j) {
00089 const Piece piece2 = state.pieceOf(j);
00090 if (! piece2.isOnBoard() || unpromote(piece2.ptype()) == KING || i == j)
00091 continue;
00092 const NumEffectState removed2(removed.emulateCapture(piece2, piece2.owner()));
00093 const eval::ml::OpenMidEndingEval eval_removed2(removed2);
00094 values[j] += eval_removed.value() - eval_removed2.value();
00095 count[j]++;
00096 }
00097 }
00098 for (int i=0; i<Piece::SIZE; ++i)
00099 if (count[i])
00100 values[i] /= count[i];
00101 }
00102
00103 void show(const NumEffectState& state)
00104 {
00105 static const boost::shared_ptr<osl::record::KIFCharacters> characters(new osl::record::KIFCharacters());
00106 static osl::record::KanjiPrint printer(std::cout, characters);
00107 static const double scale = 200.0
00108 / eval::ml::OpenMidEndingEval::captureValue(newPtypeO(WHITE,PAWN));
00109 const eval::ml::OpenMidEndingEval eval(state);
00110 PieceValues values;
00111 if (piece_estimate_level == 1)
00112 make1(state, eval, values);
00113 else
00114 make2(state, eval, values);
00115 printer.print(state);
00116 for (int z=0; z<2; ++z) {
00117 for (size_t t=0; t<PieceStand::order.size(); ++t) {
00118 const Ptype ptype = PieceStand::order[t];
00119 bool shown = false;
00120 for (int i=Ptype_Table.getIndexMin(ptype); i<Ptype_Table.getIndexLimit(ptype); ++i) {
00121 const Piece piece = state.pieceOf(i);
00122 if (! piece.isOnBoard() || unpromote(piece.ptype()) != ptype
00123 || piece.owner() != indexToPlayer(z))
00124 continue;
00125 if (! shown)
00126 std::cout << Ptype_Table.getCsaName(ptype);
00127 shown = true;
00128 std::cout << " (" << piece.square().x() << "," << piece.square().y()
00129 << ") " << (int)(values[piece.number()]*scale);
00130 }
00131 if (shown)
00132 std::cout << "\n";
00133 }
00134 }
00135 std::cout << "total " << (int)(eval.value()*scale)
00136 << " " << eval.value()
00137 << " (progress: " << eval.progress16().value()
00138 << " " << progress::ml::NewProgress(state).progress()
00139 << ", open-mid-mid2-endgame: " << eval.openingValue()
00140 << " " << eval.midgameValue()
00141 << " " << eval.midgame2Value()
00142 << " " << eval.endgameValue()
00143 << " )\n";
00144 }
00145
00146 void show(const char *filename)
00147 {
00148 CsaFile file(filename);
00149 const vector<osl::Move> moves = file.getRecord().getMoves();
00150 NumEffectState state(file.getInitialState());
00151 for (unsigned int i=0; i<moves.size(); i++)
00152 {
00153 show(state);
00154 const Move m = moves[i];
00155 state.makeMove(m);
00156 }
00157 show(state);
00158 }
00159
00160
00161
00162
00163
00164