-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlrta.cpp
More file actions
202 lines (147 loc) · 3.61 KB
/
Copy pathlrta.cpp
File metadata and controls
202 lines (147 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <cstdint> //or <stdint.h>
#include <limits>
#include <string>
#include <vector>
#include <set>
#include <boost/any.hpp>
#include <random>
#include <chrono>
#include <boost/unordered_map.hpp>
#include <boost/any.hpp>
boost::unordered_map<string, int> h_map;
int generated_nodes;
int expanded_nodes;
vector<int> goal_list;
struct State{
int id
vector<int> list;
float g;
float h;
float f;
boost::any handler_open;
int n = list.size();
State father;
State(int _id, vector<int> _list, double j, double h_, double k, State _f) : id(_id) ,list(_list),g(j),h(h_),f(k) father(_f) {}
~State(){}
State & operator=(const State & _state){
if(this == &_state) return *this;
this->id = _state.id;
this->list = _state.list;
this->g = _state.g;
this->h = _state.h;
this->f = _state.f;
this->father = _state.father;
};
bool operator==(const State & _state) const{
return (this==&_state);
};
bool operator<( const State & a, const State & b) {
return(a->f > b->f);
};
};
string id_gen(vector<int> _v){
string _id = "";
for(unsigned i = 0; i <_v.size() ; ++i) {
_id+= to_string(_v[i]);
}
return _id;
}
void printList(state* currentState ){
for (int& x: currentState->list) cout << ' ' << x;
std::cout << '\n';
}
vector<int> slice(vector<int>::iterator first, vector<int>::iterator last){
vector<int> aux;
for (vector<int>::iterator cur = first; cur != last; ++cur) {
aux.push_back(*cur);
}
return aux;
}
vector<int> flip(vector<int> s1, int i) {
int back = s1.back();
s1.pop_back();
for(unsigned i = 0; i < s1.size()-1; ++i) {
}
vector<int> previous = slice(s1.begin(),s1.begin()+i);
vector<int> aux_append = slice(s1.begin()+i,s1.end());
reverse(previous.begin(), previous.end());
previous.insert( previous.end(), aux_append.begin(), aux_append.end() );
previous.push_back(back);
return previous;
}
int hgap(vector<int> numbers){
int gap = 0;
for(size_t i = 0; i<numbers.size()-1; ++i){
if (abs(numbers[i]-numbers[i+1]) != 1){
gap++;
}
}
return gap;
}
bool checkForGoal(vector<int> s_candidate) {
int old_size = goal_set.size();
goal_set.insert(s_candidate);
int new_size = goal_set.size();
if(old_size == new_size) {
return 0;
}
goal_set.clear();
goal_set.insert(goal_list);
return 1;
}
State* lookahead(State* current){
State* min = new State(,current->list,0,0,0) ;
for(size_t i = 2; i < _current->list.size(); ++i) {
State* succ = new State(id_gen(),flip(_current->list,i),0,0,0,current);
generated_nodes++;
//Already visited ?
auto s_find = h_map.find(succ);
if(s_find != h_map.end()){
succ->g = succ->g+1;
succ->h = s_find->h;
succ->f = succ->f = succ->h + succ->g;
} else {
succ->h = hgap(succ->list);
succ->f = succ->h + 1;
h_map.emplace(succ->id,succ->h);
}
//get the min
if(min < succ) {
min = succ;
}
}
return min;
}
void update(State* _curr&, State* _succ&) {
if(_curr->h < _succ->f){
_curr->h = _succ->f;
auto it = map.find()
if(it != map.end(_curr->id))
it->second = _curr->h;
}
}
State LRTA(State start){
State *current = start //reset to start
while(checkForGoal(current->list)){
State *succ = lookahead(current);
update(current, succ);
current = state;
expanded_nodes++;
}
return current;
}
void driver(vector<int> s0){
State solution =
State * start = new state(id_gen(s0),s0,0,hgap(s0),0)
h_map.emplace(succ->id,succ->h);
while( solution->g != start->h ){
solution = LRTA(start);
}
}
void clean(){
}
int main(int argc, char const *argv[])
{
return 0;
}