<commit_before>#include <fstream>
#include <sstream>


#include "../../include/reduced/ReducedState.h"

#include "../../include/solvers/FFReducedModelSolver.h"


using namespace mlcore;
using namespace mlppddl;
using namespace std;


namespace mlsolvers
{


void FFReducedModelSolver::storeRemovedAtoms()
{
    mlreduced::ReducedModel* reducedModel =
        dynamic_cast<mlreduced::ReducedModel*> (problem_);
    assert(reducedModel);
    mlppddl::PPDDLProblem* originalProblem =
        dynamic_cast<mlppddl::PPDDLProblem*> (reducedModel->originalProblem());
    assert(originalProblem);

    // Storing all atoms in the initial state
    ifstream problemTemplateFile_;
    problemTemplateFile_.open(templateProblemFilename_, ifstream::in);
    string line;
    unordered_set<string> initAtoms;
    if (problemTemplateFile_.is_open()) {
        while (getline(problemTemplateFile_, line)) {
            size_t idx = line.find("init");
            if (idx != string::npos) {
                for (int i = idx + 4; i < line.size(); i++) {
                    if (line[i] == '(') {
                        string atom = "";
                        do {
                            atom += line[i];
                        } while (line[i++] != ')');
                        initAtoms.insert(atom);
                    }
                }
            }
        }
        problemTemplateFile_.close();
    }

    // Figuring out which atoms were removed from the PPDDL parser
    problem_t* pProblem = originalProblem->pProblem();
    Domain dom = pProblem->domain();
    PredicateTable& preds = dom.predicates();
    TermTable& terms = pProblem->terms();
    for (auto const & atom : problem_t::atom_hash()) {
        ostringstream oss;
        atom.first->print(oss, preds, dom.functions(), terms);
        if (initAtoms.find(oss.str()) != initAtoms.end())
            initAtoms.erase(oss.str());
    }

    // Storing the removed atoms
    removedAtoms_ = "";
    for (string atom : initAtoms)
        removedAtoms_ += atom + " ";
}

string FFReducedModelSolver::extractStateAtoms(PPDDLState* state)
{
    ostringstream oss;
    oss << state;
    string stateStr = oss.str();
    string atomsCurrentState = "";
    for (int i = 0; i < stateStr.size(); i++) {
        // The format of a PPDDLState "tostring" conversion is
        // [ atom_1_Id:(atom_1), atom_2_Id:(atom_2), ..., atom_N_Id:(atom_N) ]
        if (stateStr[i] == ':') {
            i++;
            do {
                atomsCurrentState += stateStr[i];
            } while (stateStr[i++] != ')');
            atomsCurrentState += " ";
        }
    }
    return atomsCurrentState;
}


void FFReducedModelSolver::replaceInitStateInProblemFile(
    string atomsCurrentState)
{
    ifstream problemTemplateFile_;
    problemTemplateFile_.open(templateProblemFilename_, ifstream::in);
    string line;
    string newProblemText = "";
    if (problemTemplateFile_.is_open()) {
        while (getline(problemTemplateFile_, line)) {
            if (line.find("init") != string::npos) {
                line = "  (:init " + removedAtoms_ + atomsCurrentState + ")";
            }
            newProblemText += line + "\n";
        }
        problemTemplateFile_.close();
    }
    ofstream newProblemFile;
    newProblemFile.open(currentProblemFilename_);
    newProblemFile << newProblemText;
    newProblemFile.close();
}


pair<string, int> FFReducedModelSolver::getActionNameAndCostFromFF()
{
    string ffDomain = "-o " + determinizedDomainFilename_;
    string ffProblem = "-f " + currentProblemFilename_;
    string ffCommand = ffExecFilename_ + " " + ffDomain + " " + ffProblem + " -i 101 -i 105 -i 114 -i 118";
    string actionName = "__mdplib-dead-end__";
    FILE *ff = popen(ffCommand.c_str(), "r");
    int costFF = floor(mdplib::dead_end_cost);
    if (ff) {
        char lineBuffer[1024];
        int currentLineAction = -1;
        while (fgets(lineBuffer, 1024, ff)) {
                                                                                  cerr << lineBuffer;
            if (strstr(lineBuffer, "goal can be simplified to FALSE.") !=
                    nullptr) {
                break;
            }

            if (strstr(lineBuffer, "step") != nullptr) {
                actionName = "";
                char *pch = strstr(lineBuffer, "0:");
                if (pch == nullptr)
                    continue;
                pch += 3;
                actionName += pch;
                actionName = actionName.substr(0, actionName.size() - 1);
                currentLineAction = 0;
            } else if (currentLineAction != -1) {
                currentLineAction++;
                ostringstream oss("");
                oss << currentLineAction << ":";
                char *pch = strstr(lineBuffer, oss.str().c_str());
                if (pch == nullptr) {
                    costFF = currentLineAction;
                    currentLineAction = -1;<commit_msg>fixed a bug in the code to store the removed atoms<commit_after>#include <fstream>
#include <sstream>


#include "../../include/reduced/ReducedState.h"

#include "../../include/solvers/FFReducedModelSolver.h"


using namespace mlcore;
using namespace mlppddl;
using namespace std;


namespace mlsolvers
{


void FFReducedModelSolver::storeRemovedAtoms()
{
    mlreduced::ReducedModel* reducedModel =
        dynamic_cast<mlreduced::ReducedModel*> (problem_);
    assert(reducedModel);
    mlppddl::PPDDLProblem* originalProblem =
        dynamic_cast<mlppddl::PPDDLProblem*> (reducedModel->originalProblem());
    assert(originalProblem);

    // Storing all atoms in the initial state
    ifstream problemTemplateFile_;
    problemTemplateFile_.open(templateProblemFilename_, ifstream::in);
    string line;
    unordered_set<string> initAtoms;
    if (problemTemplateFile_.is_open()) {
        while (getline(problemTemplateFile_, line)) {
            size_t idx = line.find("init");
            if (idx != string::npos) {
                for (int i = idx + 4; i < line.size(); i++) {
                    if (line[i] == '(') {
                        string atom = "";
                        do {
                            atom += line[i];
                        } while (line[i++] != ')');
                        initAtoms.insert(atom);
                        i--;
                    }
                }
            }
        }
        problemTemplateFile_.close();
    }

    // Figuring out which atoms were removed from the PPDDL parser
    problem_t* pProblem = originalProblem->pProblem();
    Domain dom = pProblem->domain();
    PredicateTable& preds = dom.predicates();
    TermTable& terms = pProblem->terms();
    for (auto const & atom : problem_t::atom_hash()) {
        ostringstream oss;
        atom.first->print(oss, preds, dom.functions(), terms);
        if (initAtoms.find(oss.str()) != initAtoms.end())
            initAtoms.erase(oss.str());
    }

    // Storing the removed atoms
    removedAtoms_ = "";
    for (string atom : initAtoms)
        removedAtoms_ += atom + " ";
}

string FFReducedModelSolver::extractStateAtoms(PPDDLState* state)
{
    ostringstream oss;
    oss << state;
    string stateStr = oss.str();
    string atomsCurrentState = "";
    for (int i = 0; i < stateStr.size(); i++) {
        // The format of a PPDDLState "tostring" conversion is
        // [ atom_1_Id:(atom_1), atom_2_Id:(atom_2), ..., atom_N_Id:(atom_N) ]
        if (stateStr[i] == ':') {
            i++;
            do {
                atomsCurrentState += stateStr[i];
            } while (stateStr[i++] != ')');
            atomsCurrentState += " ";
        }
    }
    return atomsCurrentState;
}


void FFReducedModelSolver::replaceInitStateInProblemFile(
    string atomsCurrentState)
{
    ifstream problemTemplateFile_;
    problemTemplateFile_.open(templateProblemFilename_, ifstream::in);
    string line;
    string newProblemText = "";
    if (problemTemplateFile_.is_open()) {
        while (getline(problemTemplateFile_, line)) {
            if (line.find("init") != string::npos) {
                line = "  (:init " + removedAtoms_ + atomsCurrentState + ")";
            }
            newProblemText += line + "\n";
        }
        problemTemplateFile_.close();
    }
    ofstream newProblemFile;
    newProblemFile.open(currentProblemFilename_);
    newProblemFile << newProblemText;
    newProblemFile.close();
}


pair<string, int> FFReducedModelSolver::getActionNameAndCostFromFF()
{
    string ffDomain = "-o " + determinizedDomainFilename_;
    string ffProblem = "-f " + currentProblemFilename_;
    string ffCommand = ffExecFilename_ + " " + ffDomain + " " + ffProblem + " -i 101 -i 105 -i 114 -i 118";
    dprint1(ffCommand);
    string actionName = "__mdplib-dead-end__";
    FILE *ff = popen(ffCommand.c_str(), "r");
    int costFF = floor(mdplib::dead_end_cost);
    if (ff) {
        char lineBuffer[1024];
        int currentLineAction = -1;
        while (fgets(lineBuffer, 1024, ff)) {
                                                                                  cerr << lineBuffer;
            if (strstr(lineBuffer, "goal can be simplified to FALSE.") !=
                    nullptr) {
                break;
            }

            if (strstr(lineBuffer, "step") != nullptr) {
                actionName = "";
                char *pch = strstr(lineBuffer, "0:");
                if (pch == nullptr)
                    continue;
                pch += 3;
                actionName += pch;
                actionName = actionName.substr(0, actionName.size() - 1);
                currentLineAction = 0;
            } else if (currentLineAction != -1) {
                currentLineAction++;
                ostringstream oss("");
                oss << currentLineAction << ":";
                char *pch = strstr(lineBuffer, oss.str().c_str());
                if (pch == nullptr) {
                    costFF = currentLineAction;
                    currentLineAction = -1;
{'fraction_non_alphanumeric': 0.1248, 'fraction_numerical': 0.0069, 'mean_word_length': 1.9844822441062369, 'pattern_counts': {'":': 2, '<': 39, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7133402', 'n_tokens_mistral': 3009, 'n_tokens_neox': 2788, 'n_words': 609}