//============================================================================== // // main // //============================================================================== int main(int argc, char **argv) { int numDisks = GetNextInt(); int numPegs = GetNextInt(); std::vector<int> startState; std::vector<int> endState; for (int i = 0; i < numDisks; i++) { startState.push_back(GetNextInt()-1); } for (int i = 0; i < numDisks; i++) { endState.push_back(GetNextInt()-1); } Graph *graph = new Graph(numDisks, numPegs); int numMoves = graph->BuildAndExplore(startState, endState); std::cout << "num moves = " << numMoves << std::endl; Graph::Vertex *vtx = graph->GetVertex(endState); std::list<Graph::Vertex *> forwardList; for (int i = 0; i < numMoves; i++) { forwardList.push_front(vtx); vtx = vtx->predecessor; } std::cout << numMoves << std::endl; std::list<Graph::Vertex *>::iterator iter; for (iter = forwardList.begin(); iter != forwardList.end(); iter++) { PrintMove(*iter); } delete graph; return 0; }
void Viterbi(const Graph& graph, const SparseVector& weights, float bleuWeight, const ReferenceSet& references , size_t sentenceId, const std::vector<FeatureStatsType>& backgroundBleu, HgHypothesis* bestHypo) { BackPointer init(NULL,kMinScore); vector<BackPointer> backPointers(graph.VertexSize(),init); HgBleuScorer bleuScorer(references, graph, sentenceId, backgroundBleu); vector<FeatureStatsType> winnerStats(kBleuNgramOrder*2+1); for (size_t vi = 0; vi < graph.VertexSize(); ++vi) { // cerr << "vertex id " << vi << endl; FeatureStatsType winnerScore = kMinScore; const Vertex& vertex = graph.GetVertex(vi); const vector<const Edge*>& incoming = vertex.GetIncoming(); if (!incoming.size()) { //UTIL_THROW(HypergraphException, "Vertex " << vi << " has no incoming edges"); //If no incoming edges, vertex is a dead end backPointers[vi].first = NULL; backPointers[vi].second = kMinScore; } else { //cerr << "\nVertex: " << vi << endl; for (size_t ei = 0; ei < incoming.size(); ++ei) { //cerr << "edge id " << ei << endl; FeatureStatsType incomingScore = incoming[ei]->GetScore(weights); for (size_t i = 0; i < incoming[ei]->Children().size(); ++i) { size_t childId = incoming[ei]->Children()[i]; //UTIL_THROW_IF(backPointers[childId].second == kMinScore, // HypergraphException, "Graph was not topologically sorted. curr=" << vi << " prev=" << childId); incomingScore = max(incomingScore + backPointers[childId].second, kMinScore); } vector<FeatureStatsType> bleuStats(kBleuNgramOrder*2+1); // cerr << "Score: " << incomingScore << " Bleu: "; // if (incomingScore > nonbleuscore) {nonbleuscore = incomingScore; nonbleuid = ei;} FeatureStatsType totalScore = incomingScore; if (bleuWeight) { FeatureStatsType bleuScore = bleuScorer.Score(*(incoming[ei]), vertex, bleuStats); if (isnan(bleuScore)) { cerr << "WARN: bleu score undefined" << endl; cerr << "\tVertex id : " << vi << endl; cerr << "\tBleu stats : "; for (size_t i = 0; i < bleuStats.size(); ++i) { cerr << bleuStats[i] << ","; } cerr << endl; bleuScore = 0; } //UTIL_THROW_IF(isnan(bleuScore), util::Exception, "Bleu score undefined, smoothing problem?"); totalScore += bleuWeight * bleuScore; // cerr << bleuScore << " Total: " << incomingScore << endl << endl; //cerr << "is " << incomingScore << " bs " << bleuScore << endl; } if (totalScore >= winnerScore) { //We only store the feature score (not the bleu score) with the vertex, //since the bleu score is always cumulative, ie from counts for the whole span. winnerScore = totalScore; backPointers[vi].first = incoming[ei]; backPointers[vi].second = incomingScore; winnerStats = bleuStats; } } //update with winner //if (bleuWeight) { //TODO: Not sure if we need this when computing max-model solution if (backPointers[vi].first) { bleuScorer.UpdateState(*(backPointers[vi].first), vi, winnerStats); } } // cerr << "backpointer[" << vi << "] = (" << backPointers[vi].first << "," << backPointers[vi].second << ")" << endl; } //expand back pointers GetBestHypothesis(graph.VertexSize()-1, graph, backPointers, bestHypo); //bleu stats and fv //Need the actual (clipped) stats //TODO: This repeats code in bleu scorer - factor out bestHypo->bleuStats.resize(kBleuNgramOrder*2+1); NgramCounter counts; list<WordVec> openNgrams; for (size_t i = 0; i < bestHypo->text.size(); ++i) { const Vocab::Entry* entry = bestHypo->text[i]; if (graph.IsBoundary(entry)) continue; openNgrams.push_front(WordVec()); for (list<WordVec>::iterator k = openNgrams.begin(); k != openNgrams.end(); ++k) { k->push_back(entry); ++counts[*k]; } if (openNgrams.size() >= kBleuNgramOrder) openNgrams.pop_back(); } for (NgramCounter::const_iterator ngi = counts.begin(); ngi != counts.end(); ++ngi) { size_t order = ngi->first.size(); size_t count = ngi->second; bestHypo->bleuStats[(order-1)*2 + 1] += count; bestHypo->bleuStats[(order-1) * 2] += min(count, references.NgramMatches(sentenceId,ngi->first,true)); } bestHypo->bleuStats[kBleuNgramOrder*2] = references.Length(sentenceId); }