Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <queue>
- #include <stdlib.h>
- using namespace std;
- class State{
- private:
- vector<vector<int>> cState;
- vector<int> loc;
- public:
- int cost;
- int heuristic;
- int total;
- State(vector<vector<int>> sState,int costval){
- cState = sState;
- loc = findLoc(cState);
- cost = costval;
- cout<<"in constructor "<<cost<<endl;
- }
- State(vector<vector<int>> sState){
- cState = sState;
- loc = findLoc(cState);
- }
- void setHeuristic(State goal){
- int i,j;
- int h=0;
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- if(this->cState[i][j]!=goal.cState[i][j]) h++;
- }
- }
- this->heuristic = h;
- }
- void setfN(State state){
- int total = state.cost + state.heuristic;
- this->total = total;
- }
- vector<int> findLoc(vector<vector<int>> state){
- int i,j;
- vector<int> loc;
- loc.resize(2);
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- if(state[i][j]==0){loc[0] =i; loc[1] =j; return loc;}
- }
- }
- loc[0] = 999;
- loc[1] = 999;
- return loc;
- }
- bool isGoal(State gState){
- int i,j;
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- if(cState[i][j] != gState.cState[i][j]){return false;}
- }
- }
- return true;
- }
- void printState(State state){
- int i,j;
- for(i =0; i<3; i++){
- for(j=0; j<3; j++){
- cout<<state.cState[i][j];
- }
- cout<<endl;
- }
- cout<<"cost= "<<state.cost;
- cout<<"heuristic= "<<state.heuristic;
- cout<<"total= "<<state.total<<endl;
- }
- vector<State> nextState(State astate){
- int row = astate.loc[0];
- int col = astate.loc[1];
- int cost = astate.cost;
- cout<<"printing astate"<<endl;
- astate.printState(astate);
- cout<<"in next state"<<cost<<endl;
- vector<vector<int>> nState;
- vector<State> states;
- int temp;
- if(row==0){
- if(col==0){
- nState = astate.cState;
- temp = nState[0][1];
- nState[0][0] = temp;
- nState[0][1] = 0;
- State state(nState,cost+2);
- state.printState(state);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[1][0];
- nState[0][0] = temp;
- nState[1][0] = 0;
- State state1(nState,cost+2);
- states.push_back(state1);
- return states;
- }
- else if(col==2){
- nState = astate.cState;
- temp = nState[0][1];
- nState[0][2] = temp;
- nState[0][1] = 0;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[1][2];
- nState[0][2] = temp;
- nState[1][2] = 0;
- State state1(nState,cost+2);
- states.push_back(state1);
- return states;
- }
- else{
- nState = astate.cState;
- temp = nState[0][0];
- nState[0][0] = 0;
- nState[0][1] = temp;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[0][2];
- nState[0][2] = 0;
- nState[0][1] = temp;
- State state1(nState,cost+2);
- states.push_back(state1);
- nState = astate.cState;
- temp = nState[1][1];
- nState[1][1] = 0;
- nState[0][1] = temp;
- State state2(nState,cost+2);
- states.push_back(state2);
- return states;
- }
- }
- else if(row==2){
- if(col==0){
- nState = astate.cState;
- temp = nState[1][0];
- nState[1][0] = 0;
- nState[2][0] = temp;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[2][1];
- nState[2][0] = temp;
- nState[2][1] = 0;
- State state1(nState,cost+2);
- states.push_back(state1);
- return states;
- }
- else if(col==2){
- nState = astate.cState;
- temp = nState[1][2];
- nState[2][2] = temp;
- nState[1][2] = 0;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[2][1];
- nState[2][2] = temp;
- nState[2][1] = 0;
- State state1(nState,cost+2);
- states.push_back(state1);
- return states;
- }
- else{
- nState = astate.cState;
- temp = nState[2][0];
- nState[2][1] = temp;
- nState[2][0] = 0;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[2][2];
- nState[2][2] = 0;
- nState[2][1] = temp;
- State state1(nState,cost+2);
- states.push_back(state1);
- nState = astate.cState;
- temp = nState[1][1];
- nState[1][1] = 0;
- nState[2][1] = temp;
- State state2(nState,cost+2);
- states.push_back(state2);
- return states;
- }
- }
- else{
- if(col==0){
- nState = astate.cState;
- temp = nState[0][0];
- nState[1][0] = temp;
- nState[0][0] = 0;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[2][0];
- nState[2][0] = 0;
- nState[1][0] = temp;
- State state1(nState,cost+2);
- states.push_back(state1);
- nState = astate.cState;
- temp = nState[1][1];
- nState[1][1] = 0;
- nState[1][0] = temp;
- State state2(nState,cost+2);
- states.push_back(state2);
- return states;
- }
- else if(col==2){
- nState = astate.cState;
- temp = nState[0][2];
- nState[1][2] = temp;
- nState[0][2] = 0;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[2][2];
- nState[2][2] = 0;
- nState[1][2] = temp;
- State state1(nState,cost+2);
- states.push_back(state1);
- nState = astate.cState;
- temp = nState[1][1];
- nState[1][1] = 0;
- nState[1][2] = temp;
- State state2(nState,cost+2);
- states.push_back(state2);
- return states;
- }
- else{
- nState = astate.cState;
- temp = nState[0][1];
- nState[1][1] = temp;
- nState[0][1] = 0;
- State state(nState,cost+2);
- states.push_back(state);
- nState = astate.cState;
- temp = nState[2][1];
- nState[2][1] = 0;
- nState[1][1] = temp;
- State state1(nState,cost+2);
- states.push_back(state1);
- nState = astate.cState;
- temp = nState[1][0];
- nState[1][1] = temp;
- nState[1][0] = 0;
- State state2(nState,cost+2);
- states.push_back(state2);
- nState = astate.cState;
- temp = nState[1][2];
- nState[1][2] = 0;
- nState[1][1] = temp;
- State state3(nState,cost+2);
- states.push_back(state3);
- return states;
- }
- }
- }
- };
- //
- // Compare.h
- // assignment1
- //
- // Created by Josyula Gopala Krishna on 09/02/14.
- // Copyright (c) 2014 Josyula Gopala Krishna. All rights reserved.
- //
- #include <iostream>
- #include <string>
- #include <vector>
- #include <queue>
- using namespace std;
- class Compare{
- public:
- bool operator()(State s1, State s2){
- if(s1.total>s2.total) return true;
- if(s1.total == s2.total) return true;
- if(s1.total<s2.total)return false;
- return false;
- }
- };
- int main(){
- int i,j;
- priority_queue<State,vector<State>,Compare> fringe;
- vector<State> tempHolder;
- vector<State> actions;
- vector<vector<int>> intial = {{0,1,2},{ 3, 4, 5}, {6, 7, 8}};
- //vector<vector<int>> gol = {{1,6,4} ,{8,7,0} ,{3,2,5}};
- vector<vector<int>> gol = {{3,1,2} ,{4,5,0} ,{6,7,8}};
- State goal(gol);
- State initial(intial,0);
- initial.setHeuristic(goal);
- initial.setfN(initial);
- vector<vector<int>> fakeState = {{0,0,0},{0,0,0},{0,0,0}};
- State node(fakeState);
- fringe.push(initial);
- initial.printState(initial);
- tempHolder = initial.nextState(intial);
- /* while(true){
- if(fringe.size() > 0){
- node = fringe.top();
- fringe.pop();
- actions.push_back(node);
- if(node.isGoal(goal)){
- for(i=0;i<actions.size();i++)
- actions[i].printState(actions[i]);
- cout<<actions.size();
- exit(0);
- }
- tempHolder = node.nextState(initial);
- for(i=0;i<tempHolder.size();i++){
- tempHolder[i].setHeuristic(goal);
- tempHolder[i].setfN(tempHolder[i]);
- }
- for(i=0;i<tempHolder.size();i++)
- fringe.push(tempHolder[i]);
- }
- else{
- cout<<"reached the end of the states";
- exit(EXIT_FAILURE);
- }
- }
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment