...
 
Commits (6)
......@@ -64,18 +64,99 @@ void save_plan(const vector<const Operator *> &plan, int iter) {
out << g_plan_filename << "." << iter;
outfile.open(out.str().c_str(), ios::out);
}
for (int i = 0; i < plan.size(); i++) {
cout << plan[i]->get_name() << " (" << plan[i]->get_cost() << ")" << endl;
outfile << "(" << plan[i]->get_name() << ")" << endl;
string line;
ifstream myfile ("/sumi/savePlan.txt");
if (myfile.is_open())
{
//cout << "reading file"<< endl;;
std::vector<std::vector<std::string>> strVec;
int notFound = 0;
int row = 20, width = 50, i = 0, j = 0;
strVec.resize( row );
for( auto &i : strVec ) {
i.resize( width );
}
while ( getline (myfile,line) ) {
strVec[i][j] = line ;
if (line != "END") {
//cout << "strVec..." << strVec[i][j] << endl;
j++;
} else {
i++;
j = 0;
}
}
for (int x = 0; x < i; x++) {
//cout << "value of row" << i<< endl;;
int k = 0;
line.clear();
line = plan[k]->get_name() + " (" + std::to_string(plan[k]->get_cost()) + ")";
//cout << "first for loop" << "line"<<line << "strVec" << strVec[x][1];
if (strVec[x][0] == line) {
//cout << "first line match "<< endl;;
for (int y = 1; y <= sizeof( strVec); y++) {
k++;
if (k < plan.size()) {
line.clear();
line = plan[k]->get_name() + " (" + std::to_string(plan[k]->get_cost()) + ")";
//cout << line;
}
if (strVec[x][y] == line && k < plan.size()) {
notFound = 1; //match 1=false
//cout << y <<"line match"<< endl;
continue;
} else if ( strVec[x][y] == "END") {
break;
} else {
notFound = 0; //not match 0=true
break;
} //if end
} //for end
} //if end
if (notFound == 1 ) {
break;
}//if end
}
if (notFound == 0) {
//cout << "adding data"<< endl;;
ofstream writeFile;
writeFile.open("/sumi/savePlan.txt", std::ios_base::app) ;
for (int i = 0; i < plan.size(); i++) {
writeFile << plan[i]->get_name() << " (" << std::to_string(plan[i]->get_cost()) << ")" << endl;
cout << plan[i]->get_name() << " (" << plan[i]->get_cost() << ")" << endl;
outfile << "(" << plan[i]->get_name() << ")" << endl;
}
writeFile << "END" << endl;
writeFile.close();
outfile.close();
int plan_cost = calculate_plan_cost(plan);
cout << "Plan length: " << plan.size() << " step(s)." << endl;
cout << "Plan cost: " << plan_cost << endl;
}
} else {
ofstream collection ("/sumi/savePlan.txt");
if (collection.is_open()) {
for (int i = 0; i < plan.size(); i++) {
collection << plan[i]->get_name() << " (" << std::to_string(plan[i]->get_cost()) << ")" << endl;
cout << plan[i]->get_name() << " (" << plan[i]->get_cost() << ")" << endl;
outfile << "(" << plan[i]->get_name() << ")" << endl;
}
collection << "END" << endl;
collection.close();
outfile.close();
int plan_cost = calculate_plan_cost(plan);
cout << "Plan length: " << plan.size() << " step(s)." << endl;
cout << "Plan cost: " << plan_cost << endl;
}
}
outfile.close();
int plan_cost = calculate_plan_cost(plan);
ofstream statusfile;
statusfile.open("plan_numbers_and_cost", ios::out | ios::app);
statusfile << iter << " " << plan_cost << endl;
statusfile.close();
cout << "Plan length: " << plan.size() << " step(s)." << endl;
cout << "Plan cost: " << plan_cost << endl;
int plan_cost = calculate_plan_cost(plan);
ofstream statusfile;
statusfile.open("plan_numbers_and_cost", ios::out | ios::app);
statusfile << iter << " " << plan_cost << endl;
statusfile.close();
}
bool peek_magic(istream &in, string magic) {
......
......@@ -4,7 +4,10 @@
#include "aStarBwd.h"
#include "../image.h"
#include <algorithm>
#include <vector>
#include <random>
#include <iterator>
#include <chrono>
void AStarBwd::image(MEDDLY::dd_edge &stateSet, MEDDLY::dd_edge &transOp,
MEDDLY::dd_edge &res) {
MEDDLY::dd_edge primedStateSet(forest);
......@@ -64,9 +67,21 @@ Operator *AStarBwd::reconstructPlanStep(MEDDLY::dd_edge &targetState) {
MEDDLY::FILE_output out(stdout);
int costAfter = selectAnyState(targetState);
// targetState.show(out, 3);
for (int op_i = 0; op_i < g_operators.size(); op_i++) {
//std::random_device rd;
typedef std::chrono::high_resolution_clock myclock;
myclock::time_point beginning = myclock::now();
myclock::duration d = myclock::now() - beginning;
unsigned seed = d.count();
std::mt19937 g(seed);
std::vector<int> vector;
for (int op_i = 0; op_i < g_operators.size(); op_i++) {
vector.push_back(op_i);
}
std::shuffle(vector.begin(), vector.end(), g);
for (std::vector<int>::iterator it=vector.begin(); it!=vector.end(); ++it)
{
MEDDLY::dd_edge successors(forest);
imageInv(targetState, *g_operators[op_i].get_op_evmdd(), successors);
imageInv(targetState, *g_operators[*it].get_op_evmdd(), successors);
for (int c_i = 0; c_i < curClosedList; c_i++) {
MEDDLY::dd_edge tmp(forest);
......@@ -74,16 +89,13 @@ Operator *AStarBwd::reconstructPlanStep(MEDDLY::dd_edge &targetState) {
// We need to add the cost to target and then check if it fits
MEDDLY::dd_edge costDummy(forest);
MEDDLY::apply(MEDDLY::PLUS, *g_operators[op_i].get_SDAC_cost(),
targetState, costDummy);
MEDDLY::apply(MEDDLY::PLUS, *g_operators[*it].get_SDAC_cost(),
targetState, costDummy);
int cost_tmp = minVal(costDummy);
costDummy.set(-1, cost_tmp);
MEDDLY::apply(MEDDLY::PLUS, tmp, costDummy, costDummy);
// std::cout << minVal(costDummy) << ", " << costAfter << std::endl;
// Cost Fits
// std::cout << "Cost after: " << costAfter << ", minval" << minVal(tmp)
// << std::endl;
if (minVal(costDummy) == costAfter) {
// tmp.show(out, 3);
MEDDLY::apply(MINSTATES, tmp, tmp);
......@@ -92,9 +104,9 @@ Operator *AStarBwd::reconstructPlanStep(MEDDLY::dd_edge &targetState) {
MEDDLY::apply(INTERSECTIONMAX, tmp, *closedLists[c_i], targetState);
// create dummy op with correct cost
Operator *dummy = new Operator(g_operators[op_i].get_name(),
costAfter - minVal(targetState));
dummy->set_cost_function(g_operators[op_i].get_SDAC_cost_function());
Operator *dummy = new Operator(g_operators[*it].get_name(),
costAfter - minVal(targetState));
dummy->set_cost_function(g_operators[*it].get_SDAC_cost_function());
// targetState.show(out, 3);
// std::cout << "-----------------------" << std::endl;
// std::cout << "\n" << curClosedList << std::endl;
......
......@@ -4,7 +4,10 @@
#include "aStarFwd.h"
#include "../image.h"
#include <algorithm>
#include <vector>
#include <random>
#include <iterator>
#include <chrono>
void AStarFwd::image(MEDDLY::dd_edge &stateSet, MEDDLY::dd_edge &transOp,
MEDDLY::dd_edge &res) {
MEDDLY::apply(MEDDLY::PLUS, stateSet, transOp, res);
......@@ -47,12 +50,22 @@ int AStarFwd::selectAnyState(MEDDLY::dd_edge &targetState) {
}
Operator *AStarFwd::reconstructPlanStep(MEDDLY::dd_edge &targetState) {
MEDDLY::FILE_output out(stdout);
MEDDLY::FILE_output out(stdout);
int costAfter = selectAnyState(targetState);
// targetState.show(out, 3);
for (int op_i = 0; op_i < g_operators.size(); op_i++) {
typedef std::chrono::high_resolution_clock myclock;
myclock::time_point beginning = myclock::now();
myclock::duration d = myclock::now() - beginning;
unsigned seed = d.count();
std::mt19937 g(seed);
std::vector<int> vector;
for (int op_i = 0; op_i < g_operators.size(); op_i++) {
vector.push_back(op_i);
}
std::shuffle(vector.begin(), vector.end(), g);
for (std::vector<int>::iterator it=vector.begin(); it!=vector.end(); ++it)
{
MEDDLY::dd_edge predecessors(forest);
imageInv(targetState, *g_operators[op_i].get_op_evmdd(), predecessors);
imageInv(targetState, *g_operators[*it].get_op_evmdd(), predecessors);
/*if (minVal(predecessors) == std::numeric_limits<int>::max())
continue;*/
// predecessors.show(out, 3);
......@@ -62,10 +75,8 @@ Operator *AStarFwd::reconstructPlanStep(MEDDLY::dd_edge &targetState) {
for (int c_i = 0; c_i < curClosedList; c_i++) {
MEDDLY::dd_edge tmp(forest);
MEDDLY::apply(INTERSECTIONMAX, predecessors, *closedLists[c_i], tmp);
MEDDLY::apply(MEDDLY::PLUS, tmp, *g_operators[op_i].get_SDAC_cost(), tmp);
MEDDLY::apply(MEDDLY::PLUS, tmp, *g_operators[*it].get_SDAC_cost(), tmp);
// Fits!
// std::cout << "Cost after: " << costAfter << ", minval" << minVal(tmp)
// << std::endl;
if (minVal(tmp) == costAfter) {
// tmp.show(out, 3);
MEDDLY::apply(MINSTATES, tmp, tmp);
......@@ -74,11 +85,11 @@ Operator *AStarFwd::reconstructPlanStep(MEDDLY::dd_edge &targetState) {
MEDDLY::apply(INTERSECTIONMAX, tmp, *closedLists[c_i], targetState);
// create dummy op with correct cost
Operator *dummy = new Operator(g_operators[op_i].get_name(),
Operator *dummy = new Operator(g_operators[*it].get_name(),
costAfter - minVal(targetState));
dummy->set_cost_function(g_operators[op_i].get_SDAC_cost_function());
dummy->set_cost_function(g_operators[*it].get_SDAC_cost_function());
// targetState.show(out, 3);
// std::cout << "-----------------------"
// std::cout << "----------------------"
// << std::endl;
curClosedList = c_i;
return dummy;
......
......@@ -197,26 +197,33 @@ int symple::step() {
<< std::endl;
// Reconstruct plan
Plan plan;
double startRec = g_timer();
if (!astarFwd.reconstructSolution(collisionFound, plan)) {
return FAILED;
}
if (!astarBwd.reconstructSolution(collisionFound, plan)) {
return FAILED;
}
if (!validatePlan(plan)) {
std::cerr << "Plan not valid!" << std::endl;
return FAILED;
}
for (int i = 1; i <= 20; i++) {
// Reconstruct plan
Plan plan;
double startRec = g_timer();
if (!astarFwd.reconstructSolution(collisionFound, plan)) {
return FAILED;
}
if (!astarBwd.reconstructSolution(collisionFound, plan)) {
return FAILED;
}
if (!validatePlan(plan)) {
std::cerr << "Plan not valid!" << std::endl;
return FAILED;
}
// if (sat_planning)
// save_plan(plan, plan_id);
this->set_plan(plan);
std::cout << "Plan reconstruction took: " << g_timer() - startRec << "sec"
<< std::endl;
// if (sat_planning)
// save_plan(plan, plan_id);
//this->set_plan(plan);
std::cout << "***************Opt plan : " << i << "***************" << std::endl;
save_plan(plan, plan_id);
plan_id++;
std::cout << "Plan reconstruction took: " << g_timer() - startRec << "sec"
<< std::endl;
}
// Clean up
remove( "/sumi/savePlan.txt" );
printMemoryState(forest);
cleanUpMeddly();
std::cout << "NOT RELEASED MEMORY: " << std::flush;
......