コード例 #1
0
int QProtobufModel::rowCount(const QModelIndex &_index) const
{
    PBMessage* msg = getMessage(_index);

    if (msg == m_rootItem)
        return 1;

    PBMessage* parent = getMessage(_index.parent());

    auto descr = parent->GetDescriptor();

    auto field = descr->FindFieldByName(msg->GetTypeName());

    if (field->is_repeated())
    {
        int count = parent->GetReflection()->FieldSize(*parent, field);
        return count;
    }
    else
        return 1;
}
コード例 #2
0
// Evaluate the move by performing a search.
void evaluateMove(searchNode *node, move_t mv, move_t killer_a,
                  move_t killer_b, searchType_t type,
                  uint64_t *node_count_serial, moveEvaluationResult* result) {
  int ext = 0;  // extensions
  bool blunder = false;  // shoot our own piece

  result->next_node.subpv[0] = 0;
  result->next_node.parent = node;

  // Make the move, and get any victim pieces.
  bool isko = make_move(&(node->position), &(result->next_node.position), mv);

  // Check whether this move changes the board state.
  //   such moves are not legal.
  if (isko) {
    result->type = MOVE_ILLEGAL;
    return;
  }

  victims_t* victims = &result->next_node.position.victims;

  // Check whether the game is over.
  if (is_game_over(victims, node->pov, node->ply)) {
    // Compute the end-game score.
    result->type = MOVE_GAMEOVER;
    result->score = get_game_over_score(victims, node->pov, node->ply);
    return;
  }

  // Ignore noncapture moves when in quiescence.
  if (zero_victims(victims) && node->quiescence) {
    result->type = MOVE_IGNORE;
    return;
  }

  // Check whether the board state has been repeated, this results in a draw.
  if (is_repeated(&(result->next_node.position), node->ply)) {
    result->type = MOVE_GAMEOVER;
    result->score = get_draw_score(&(result->next_node.position), node->ply);
    return;
  }

  tbassert(victims->stomped == 0
           || color_of(victims->stomped) != node->fake_color_to_move,
           "stomped = %d, color = %d, fake_color_to_move = %d\n",
           victims->stomped, color_of(victims->stomped),
           node->fake_color_to_move);


  // Check whether we caused our own piece to be zapped. This isn't considered
  //   a blunder if we also managed to stomp an enemy piece in the process.
  if (victims->stomped == 0 &&
      victims->zapped > 0 &&
      color_of(victims->zapped) == node->fake_color_to_move) {
    blunder = true;
  }

  // Do not consider moves that are blunders while in quiescence.
  if (node->quiescence && blunder) {
    result->type = MOVE_IGNORE;
    return;
  }

  // Extend the search-depth by 1 if we captured a piece, since that means the
  //   move was interesting.
  if (victim_exists(victims) && !blunder) {
    ext = 1;
  }

  // Late move reductions - or LMR. Only done in scout search.
  int next_reduction = 0;
  if (type == SEARCH_SCOUT && node->legal_move_count + 1 >= LMR_R1 && node->depth > 2 &&
      zero_victims(victims) && mv != killer_a && mv != killer_b) {
    if (node->legal_move_count + 1 >= LMR_R2) {
      next_reduction = 2;
    } else {
      next_reduction = 1;
    }
  }

  result->type = MOVE_EVALUATED;
  int search_depth = ext + node->depth - 1;

  // Check if we need to perform a reduced-depth search.
  //
  // After a reduced-depth search, a full-depth search will be performed if the
  //  reduced-depth search did not trigger a cut-off.
  if (next_reduction > 0) {
    search_depth -= next_reduction;
    int reduced_depth_score = -scout_search(&(result->next_node), search_depth,
                                            node_count_serial);
    if (reduced_depth_score < node->beta) {
      result->score = reduced_depth_score;
      return;
    }
    search_depth += next_reduction;
  }

  // Check if we should abort due to time control.
  if (abortf) {
    result->score = 0;
    result->type = MOVE_IGNORE;
    return;
  }


  if (type == SEARCH_SCOUT) {
    result->score = -scout_search(&(result->next_node), search_depth,
                                 node_count_serial);
  } else {
    if (node->legal_move_count == 0 || node->quiescence) {
      result->score = -searchPV(&(result->next_node), search_depth, node_count_serial);
    } else {
      result->score = -scout_search(&(result->next_node), search_depth,
                            node_count_serial);
      if (result->score > node->alpha) {
        result->score = -searchPV(&(result->next_node), node->depth + ext - 1, node_count_serial);
      }
    }
  }
}
コード例 #3
0
ファイル: pb2json.cpp プロジェクト: HaustWang/pb2json
bool Pb2Json::Json2Message(const Json& json, ProtobufMsg& message, bool str2enum) {
    auto descriptor = message.GetDescriptor();
    auto reflection = message.GetReflection();
    if (nullptr == descriptor || nullptr == reflection) return false;

    auto count = descriptor->field_count();
    for (auto i = 0; i < count; ++i) {
        const auto field = descriptor->field(i);
        if (nullptr == field) continue;

        auto& value = json[field->name()];
        if (value.is_null()) continue;

        if (field->is_repeated()) {
            if (!value.is_array()) {
                return false;
            } else {
                Json2RepeatedMessage(value, message, field, reflection, str2enum);
                continue;
            }
        }

        switch (field->type()) {
            case ProtobufFieldDescriptor::TYPE_BOOL: {
                if (value.is_boolean())
                    reflection->SetBool(&message, field, value.get<bool>());
                else if (value.is_number_integer())
                    reflection->SetBool(&message, field, value.get<uint32_t>() != 0);
                else if (value.is_string()) {
                    if (value.get<std::string>() == "true")
                        reflection->SetBool(&message, field, true);
                    else if (value.get<std::string>() == "false")
                        reflection->SetBool(&message, field, false);
                }
            } break;

            case ProtobufFieldDescriptor::TYPE_ENUM: {
                auto const* pedesc = field->enum_type();
                const ::google::protobuf::EnumValueDescriptor* pevdesc = nullptr;

                if (str2enum) {
                    pevdesc = pedesc->FindValueByName(value.get<std::string>());
                } else {
                    pevdesc = pedesc->FindValueByNumber(value.get<int>());
                }

                if (nullptr != pevdesc) {
                    reflection->SetEnum(&message, field, pevdesc);
                }
            } break;

            case ProtobufFieldDescriptor::TYPE_INT32:
            case ProtobufFieldDescriptor::TYPE_SINT32:
            case ProtobufFieldDescriptor::TYPE_SFIXED32: {
                if (value.is_number()) reflection->SetInt32(&message, field, value.get<int32_t>());
            } break;

            case ProtobufFieldDescriptor::TYPE_UINT32:
            case ProtobufFieldDescriptor::TYPE_FIXED32: {
                if (value.is_number()) reflection->SetUInt32(&message, field, value.get<uint32_t>());
            } break;

            case ProtobufFieldDescriptor::TYPE_INT64:
            case ProtobufFieldDescriptor::TYPE_SINT64:
            case ProtobufFieldDescriptor::TYPE_SFIXED64: {
                if (value.is_number()) reflection->SetInt64(&message, field, value.get<int64_t>());
            } break;
            case ProtobufFieldDescriptor::TYPE_UINT64:
            case ProtobufFieldDescriptor::TYPE_FIXED64: {
                if (value.is_number()) reflection->SetUInt64(&message, field, value.get<uint64_t>());
            } break;

            case ProtobufFieldDescriptor::TYPE_FLOAT: {
                if (value.is_number()) reflection->SetFloat(&message, field, value.get<float>());
            } break;

            case ProtobufFieldDescriptor::TYPE_DOUBLE: {
                if (value.is_number()) reflection->SetDouble(&message, field, value.get<double>());
            } break;

            case ProtobufFieldDescriptor::TYPE_STRING:
            case ProtobufFieldDescriptor::TYPE_BYTES: {
                if (value.is_string()) reflection->SetString(&message, field, value.get<std::string>());
            } break;

            case ProtobufFieldDescriptor::TYPE_MESSAGE: {
                if (value.is_object()) Json2Message(value, *reflection->MutableMessage(&message, field));
            } break;

            default:
                break;
        }
    }
    return true;
}
コード例 #4
0
ファイル: pb2json.cpp プロジェクト: HaustWang/pb2json
void Pb2Json::Message2Json(const ProtobufMsg& message, Json& json, bool enum2str) {
    auto descriptor = message.GetDescriptor();
    auto reflection = message.GetReflection();
    if (nullptr == descriptor || nullptr == descriptor) return;

    auto count = descriptor->field_count();

    for (auto i = 0; i < count; ++i) {
        const auto field = descriptor->field(i);

        if (field->is_repeated()) {
            if (reflection->FieldSize(message, field) > 0)
                RepeatedMessage2Json(message, field, reflection, json[field->name()], enum2str);

            continue;
        }

        if (!reflection->HasField(message, field)) {
            continue;
        }

        switch (field->type()) {
            case ProtobufFieldDescriptor::TYPE_MESSAGE: {
                const ProtobufMsg& tmp_message = reflection->GetMessage(message, field);
                if (0 != tmp_message.ByteSize()) Message2Json(tmp_message, json[field->name()]);
            } break;

            case ProtobufFieldDescriptor::TYPE_BOOL:
                json[field->name()] = reflection->GetBool(message, field) ? true : false;
                break;

            case ProtobufFieldDescriptor::TYPE_ENUM: {
                auto* enum_value_desc = reflection->GetEnum(message, field);
                if (enum2str) {
                    json[field->name()] = enum_value_desc->name();
                } else {
                    json[field->name()] = enum_value_desc->number();
                }
            } break;

            case ProtobufFieldDescriptor::TYPE_INT32:
            case ProtobufFieldDescriptor::TYPE_SINT32:
            case ProtobufFieldDescriptor::TYPE_SFIXED32:
                json[field->name()] = reflection->GetInt32(message, field);
                break;

            case ProtobufFieldDescriptor::TYPE_UINT32:
            case ProtobufFieldDescriptor::TYPE_FIXED32:
                json[field->name()] = reflection->GetUInt32(message, field);
                break;

            case ProtobufFieldDescriptor::TYPE_INT64:
            case ProtobufFieldDescriptor::TYPE_SINT64:
            case ProtobufFieldDescriptor::TYPE_SFIXED64:
                json[field->name()] = reflection->GetInt64(message, field);
                break;

            case ProtobufFieldDescriptor::TYPE_UINT64:
            case ProtobufFieldDescriptor::TYPE_FIXED64:
                json[field->name()] = reflection->GetUInt64(message, field);
                break;

            case ProtobufFieldDescriptor::TYPE_FLOAT:
                json[field->name()] = reflection->GetFloat(message, field);
                break;

            case ProtobufFieldDescriptor::TYPE_STRING:
            case ProtobufFieldDescriptor::TYPE_BYTES:
                json[field->name()] = reflection->GetString(message, field);
                break;

            default:
                break;
        }
    }
}
コード例 #5
0
ファイル: search.c プロジェクト: darkmatter08/6172_project4
score_t searchRoot(position_t *p, score_t alpha, score_t beta, int depth,
                   int ply, move_t *pv, uint64_t *node_count_serial,
                   FILE *OUT) {
  static int num_of_moves = 0;  // number of moves in list
  // hopefully, more than we will need
  static sortable_move_t move_list[MAX_NUM_MOVES];

  if (depth == 1) {
    // we are at depth 1; generate all possible moves
    num_of_moves = generate_all_opt(p, move_list, false);
    // shuffle the list of moves
    for (int i = 0; i < num_of_moves; i++) {
      int r = myrand() % num_of_moves;
      sortable_move_t tmp = move_list[i];
      move_list[i] = move_list[r];
      move_list[r] = tmp;
    }
  }

  searchNode rootNode;
  rootNode.parent = NULL;
  initialize_root_node(&rootNode, alpha, beta, depth, ply, p);


  assert(rootNode.best_score == alpha);  // initial conditions

  searchNode next_node;
  next_node.subpv[0] = 0;
  next_node.parent = &rootNode;

  score_t score;

  for (int mv_index = 0; mv_index < num_of_moves; mv_index++) {
    move_t mv = get_move(move_list[mv_index]);

    if (TRACE_MOVES) {
      print_move_info(mv, ply);
    }

    (*node_count_serial)++;

    // make the move.
    victims_t x = make_move(&(rootNode.position), &(next_node.position), mv);

    if (is_KO(x)) {
      continue;  // not a legal move
    }

    if (is_game_over(x, rootNode.pov, rootNode.ply)) {
      score = get_game_over_score(x, rootNode.pov, rootNode.ply);
      next_node.subpv[0] = 0;
      goto scored;
    }

    if (is_repeated(&(next_node.position), rootNode.ply)) {
      score = get_draw_score(&(next_node.position), rootNode.ply);
      next_node.subpv[0] = 0;
      goto scored;
    }

    if (mv_index == 0 || rootNode.depth == 1) {
      // We guess that the first move is the principle variation
      score = -searchPV(&next_node, rootNode.depth-1, node_count_serial);

      // Check if we should abort due to time control.
      if (abortf) {
        return 0;
      }
    } else {
      score = -scout_search(&next_node, rootNode.depth-1, node_count_serial);

      // Check if we should abort due to time control.
      if (abortf) {
        return 0;
      }

      // If its score exceeds the current best score,
      if (score > rootNode.alpha) {
        score = -searchPV(&next_node, rootNode.depth-1, node_count_serial);
        // Check if we should abort due to time control.
        if (abortf) {
          return 0;
        }
      }
    }

  scored:
    // only valid for the root node:
    tbassert((score > rootNode.best_score) == (score > rootNode.alpha),
             "score = %d, best = %d, alpha = %d\n", score, rootNode.best_score, rootNode.alpha);

    if (score > rootNode.best_score) {
      tbassert(score > rootNode.alpha, "score: %d, alpha: %d\n", score, rootNode.alpha);

      rootNode.best_score = score;
      pv[0] = mv;
      memcpy(pv+1, next_node.subpv, sizeof(move_t) * (MAX_PLY_IN_SEARCH - 1));
      pv[MAX_PLY_IN_SEARCH - 1] = 0;

      // Print out based on UCI (universal chess interface)
      double et = elapsed_time();
      char   pvbuf[MAX_PLY_IN_SEARCH * MAX_CHARS_IN_MOVE];
      getPV(pv, pvbuf, MAX_PLY_IN_SEARCH * MAX_CHARS_IN_MOVE);
      if (et < 0.00001) {
        et = 0.00001;  // hack so that we don't divide by 0
      }

      uint64_t nps = 1000 * *node_count_serial / et;
      fprintf(OUT, "info depth %d move_no %d time (microsec) %d nodes %" PRIu64
              " nps %" PRIu64 "\n",
              depth, mv_index + 1, (int) (et * 1000), *node_count_serial, nps);
      fprintf(OUT, "info score cp %d pv %s\n", score, pvbuf);

      // Slide this move to the front of the move list
      for (int j = mv_index; j > 0; j--) {
        move_list[j] = move_list[j - 1];
      }
      move_list[0] = mv;
    }

    // Normal alpha-beta logic: if the current score is better than what the
    // maximizer has been able to get so far, take that new value.  Likewise,
    // score >= beta is the beta cutoff condition
    if (score > rootNode.alpha) {
      rootNode.alpha = score;
    }
    if (score >= rootNode.beta) {
      tbassert(0, "score: %d, beta: %d\n", score, rootNode.beta);
      break;
    }
  }

  return rootNode.best_score;
}