-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixProblemCreator.h
More file actions
160 lines (113 loc) · 3.79 KB
/
MatrixProblemCreator.h
File metadata and controls
160 lines (113 loc) · 3.79 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
//
// Created by ori on 14/01/2020.
//
#ifndef EX4_MATRIXPROBLEMCREATOR_H
#define EX4_MATRIXPROBLEMCREATOR_H
#include "SearchableCreator.h"
#include "State.h"
#include "MatrixProblem.h"
#include <string>
#include <vector>
class MatrixProblemCreator : public SearchableCreator<square *> {
public:
/*
* this method has the responsibility to the creation of the matrix.
* it wil parse the vector of strings that we got as input and from all the lines
* except for the last two it will put in a matrix, and the last two will determine
* as the initial state, and the goal state. at the end we call the constuctor
* of the matrix and return it.
*/
virtual Searchable<square *> *create(vector<string> input1) override {
string::size_type sz;
vector<vector<string>> input;
vector<string>::iterator it = input1.begin();
for (; it < input1.end(); it++) {
input.push_back(fromBufferToString(*it));
}
vector<string> line;
State<square *> ***finalOutput;
int i = 0;
int j = 0;
int rowNum = input.size() - 2;
int colNum;
finalOutput = new State<square *> **[rowNum];
vector<vector<string>>::iterator itRow = input.begin();
for (i = 0; i < rowNum; i++) {
line = *itRow;
vector<string>::iterator itCol = line.begin();
colNum = line.size();
State<square *> **outLine = new State<square *> *[colNum];
finalOutput[i] = outLine;
for (j = 0; j < colNum; j++) {
square *square1 = new square(i, j);
double cost;
string str = *itCol;
cost = stod(str, &sz);
//set up for h cost
int rows1 = getNumOfRows(input1);
int cols1 = getNumOfCols(input1);
double h_cost = calculateHCostOfNode(square1->getRow(), square1->getColumn(), rows1, cols1);
State<square *> *state1 = new State<square *>(square1, cost, h_cost);
//TODO put h cost
outLine[j] = state1;
itCol++;
}
itRow++;
}
//get the values of the initial state and the` goal state
line = *itRow;
vector<string>::iterator itCol = line.begin();
i = stod(*itCol, &sz);
itCol++;
j = stod(*itCol, &sz);
itRow++;
line = *itRow;
itCol = line.begin();
int k, l;
k = stod(*itCol, &sz);
itCol++;
l = stod(*itCol, &sz);
MatrixProblem *matrix = new MatrixProblem(finalOutput, i, j, k, l, rowNum, colNum);
return matrix;
}
vector<string> fromBufferToString(string string1) {
string s;
vector<string> line;
int i = 0;
while (string1[i] != '\0') {
while (string1[i] != ',') {
if (string1[i] == '\0') {
line.push_back(s);
return line;
}
s.push_back(string1[i]);
i++;
}
line.push_back(s);
s = "";
i++;
}
return line;
}
double calculateHCostOfNode(double row, double col, double rows, double cols) {
double x = rows - row - 1;
double y = cols - col - 1;
if (x < 0) {
x *= -1;
}
if (y < 0) {
y *= -1;
}
return x + y;
}
double getNumOfRows(vector<string> input1) {
vector<string>::iterator it = input1.begin();
string first_row = *it;
double length = (first_row.length() / 2) + 1;
return length;
}
double getNumOfCols(vector<string> input1) {
return input1.size() - 2;
}
};
#endif //EX4_MATRIXPROBLEMCREATOR_H