Skip to content

Commit c87c309

Browse files
some fixes
1 parent 5060a78 commit c87c309

File tree

6 files changed

+147
-59
lines changed

6 files changed

+147
-59
lines changed

include/Metric.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,44 @@ class Metric
55
{
66
protected:
77
uint64_t a, b, c, d;
8+
void hardResolver(cellState state, cellState other_state);
9+
void softResolver(cellState state, cellState other_state);
810

911
public:
1012
Metric(/* args */);
11-
Metric(const Metric& m) = default;
13+
// Metric(const Metric* other);
14+
Metric(const Metric &m) = default;
1215
~Metric();
13-
void calculateVariables(const CellStorage& cellStorage, std::vector<std::pair<int, int>>& realFirePoints, std::vector<std::pair<int, int>>& burntPoints);
14-
void calculateVariablesFrom2Storages(const CellStorage& cellStorage, const CellStorage& other);
16+
void printConfusionMatrix();
17+
void calculateVariables(const CellStorage &cellStorage, std::vector<std::pair<int, int>> &realFirePoints, std::vector<std::pair<int, int>> &burntPoints);
18+
void calculateVariablesFrom2Storages(const CellStorage &cellStorage, const CellStorage &other);
1519
virtual double compute() const = 0;
20+
virtual const char* metricName() const = 0;
1621
};
1722

1823
class SimpsonMetric final : public Metric
1924
{
2025
public:
2126
double compute() const override;
27+
SimpsonMetric(const Metric &m);
28+
SimpsonMetric() = default;
29+
const char* metricName() const;
2230
};
2331

2432
class JaccardMetric final : public Metric
2533
{
2634
public:
2735
double compute() const override;
36+
JaccardMetric(const Metric &m);
37+
JaccardMetric() = default;
38+
const char* metricName() const;
2839
};
2940

3041
class SneathMetric final : public Metric
3142
{
3243
public:
3344
double compute() const override;
45+
SneathMetric(const Metric &m);
46+
SneathMetric() = default;
47+
const char* metricName() const;
3448
};

src/CellStorage.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ void CellStorage::iterateCell(int i, int j)
9595
continue;
9696
};
9797
double fireKoeff = 0;
98-
if (nearestCell->getState() == cellState::Tree)
98+
auto state_nearest_cell = nearestCell->getState();
99+
if (state_nearest_cell == cellState::Tree)
99100
{
100101
if (formula_->willSpread(iteratedCell, analyzedDirection, iteratedCell->getAltitude() - nearestCell->getAltitude()))
101102
{
@@ -107,7 +108,8 @@ void CellStorage::iterateCell(int i, int j)
107108
{
108109
continue;
109110
};
110-
if ((throughCell->getState() == cellState::Tree) && (nearestCell->getState() != cellState::Tree) && ((nearestCell->getState() != cellState::Fire)))
111+
auto state_through_cell = throughCell->getState();
112+
if ((state_through_cell == cellState::Tree) && (state_nearest_cell != cellState::Tree) && ((state_nearest_cell != cellState::Fire)))
111113
{
112114
if (formula_->willSpreadThroughOne(iteratedCell, analyzedDirection, iteratedCell->getAltitude() - throughCell->getAltitude()))
113115
{
@@ -215,7 +217,11 @@ void CellStorage::uploadFromTxt()
215217
while (std::getline(txt_file, line)) {
216218
for (size_t i = 0; i < line.size(); i++)
217219
{
218-
terrain_[currentLine][i].get()->setState(static_cast<cellState>(line[i]-'0'));
220+
cellState new_state = static_cast<cellState>(line[i]-'0');
221+
if (new_state == cellState::Fire){
222+
setNewState(cellState::Tree, currentLine, i);
223+
}
224+
setNewState(new_state, currentLine, i);
219225
}
220226
currentLine++;
221227

src/Math.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ double Math1::calculateGroundSlopeKoef(directions InvestigatedDirection, int alt
8282
double result = std::exp(0.5 * coef);
8383
// SlopeMetaData meta = {bool(static_cast<int>(InvestigatedDirection) / int(2)), altitudeDifference};
8484
slope_counter_[meta]++;
85-
// slope_result_[meta] = result;
85+
slope_result_[meta] = result;
8686

8787
clock_t per_iteration = clock() - start;
8888
slope_timer = per_iteration+slope_timer;
@@ -204,9 +204,9 @@ Math1::Math1()
204204

205205
Math1::~Math1()
206206
{
207-
printf("total time %f, overall counts %d\n", (double)(slope_timer), slope_counter);
208-
printf("total time %f, overall counts %d\n", (double)(wind_timer_), wind_counter_);
209-
printf("total time %f, overall counts %d\n", (double)(moisture_timer_), moisture_counter_);
207+
printf("total slope time %f, overall counts %d\n", (double)(slope_timer), slope_counter);
208+
printf("total wind time %f, overall counts %d\n", (double)(wind_timer_), wind_counter_);
209+
printf("total moisture time %f, overall counts %d\n", (double)(moisture_timer_), moisture_counter_);
210210
// printf("slope profiling\n");
211211
// std::map<int, int> slope_distribution;
212212

src/Metric.cpp

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,67 @@
22

33
#include "../include/Metric.h"
44
#include "../include/Properties.h"
5+
#include "Metric.h"
6+
7+
void Metric::hardResolver(cellState state, cellState other_state)
8+
{
9+
if ((state == cellState::Fire and other_state == cellState::Fire) or (state == cellState::Burnt and other_state == cellState::Burnt))
10+
{
11+
++a;
12+
}
13+
else if (state == cellState::Fire or state == cellState::Burnt)
14+
{
15+
++b;
16+
}
17+
else if (other_state == cellState::Fire or other_state == cellState::Burnt)
18+
{
19+
++c;
20+
}
21+
else
22+
{
23+
++d;
24+
}
25+
}
26+
27+
void Metric::softResolver(cellState state, cellState other_state)
28+
{
29+
if ((state == cellState::Fire or state == cellState::Burnt) and (other_state == cellState::Fire or other_state == cellState::Burnt))
30+
{
31+
++a;
32+
}
33+
else if (state == cellState::Fire or state == cellState::Burnt)
34+
{
35+
++b;
36+
}
37+
else if (other_state == cellState::Fire or other_state == cellState::Burnt)
38+
{
39+
++c;
40+
}
41+
else
42+
{
43+
++d;
44+
}
45+
}
546

647
Metric::Metric(/* args */)
748
{
849
}
950

51+
// Metric::Metric(const Metric *other)
52+
// :a(other->a), b(other->b), c(other->c), d(other->d)
53+
// {
54+
55+
// }
56+
1057
Metric::~Metric()
1158
{
1259
}
1360

61+
void Metric::printConfusionMatrix()
62+
{
63+
printf("TP: %ld, FP: %ld, FN: %ld, TN: %ld\n", a, b, c, d);
64+
}
65+
1466
void Metric::calculateVariables(const CellStorage &cellStorage, std::vector<std::pair<int, int>> &realFirePoints, std::vector<std::pair<int, int>> &burntPoints)
1567
{
1668
int64_t allArea = getXArea() * getYArea();
@@ -44,24 +96,8 @@ void Metric::calculateVariablesFrom2Storages(const CellStorage &cellStorage, con
4496
{
4597
for (size_t j = 0; j < getYArea(); j++)
4698
{
47-
auto state = cellStorage.getState(i, j);
48-
auto other_state = other.getState(i, j);
49-
if ((state == cellState::Fire and other_state == cellState::Fire) or (state == cellState::Burnt and other_state == cellState::Burnt))
50-
{
51-
++a;
52-
}
53-
else if (state == cellState::Fire or state == cellState::Burnt)
54-
{
55-
++b;
56-
}
57-
else if (other_state == cellState::Fire or other_state == cellState::Burnt)
58-
{
59-
++c;
60-
}
61-
else
62-
{
63-
++d;
64-
}
99+
this->softResolver(cellStorage.getState(i, j), other.getState(i, j));
100+
// this->hardResolver(cellStorage.getState(i, j), other.getState(i, j));
65101
}
66102
}
67103
}
@@ -71,12 +107,36 @@ double SimpsonMetric::compute() const
71107
return double(a) / double(std::min(a + b, a + c));
72108
}
73109

110+
SimpsonMetric::SimpsonMetric(const Metric &m)
111+
: Metric(m) {}
112+
113+
const char *SimpsonMetric::metricName() const
114+
{
115+
return "Simpson metric";
116+
}
117+
74118
double JaccardMetric::compute() const
75119
{
76120
return double(a) / double(a + b + c);
77121
}
78122

123+
JaccardMetric::JaccardMetric(const Metric &m)
124+
: Metric(m) {}
125+
126+
const char *JaccardMetric::metricName() const
127+
{
128+
return "Jaccard metric";
129+
}
130+
79131
double SneathMetric::compute() const
80132
{
81133
return double(a) / double(a + 2 * b + 2 * c);
134+
}
135+
136+
SneathMetric::SneathMetric(const Metric &m)
137+
: Metric(m) {}
138+
139+
const char *SneathMetric::metricName() const
140+
{
141+
return "Sneath metric";
82142
}

src/ProfilingDecorator.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,29 @@ ProfilingDecorator::~ProfilingDecorator()
3838
std::cout << "Overall: " << this->counter_ << " Positive: " << this->positive_counter_ << std::endl;
3939

4040
std::map<int,int> result_distribution;
41-
printf("Results: altitude_difference_, wind_direction_, wind_speed_, investigated_direction_, number_of_calculations\n");
42-
for (const auto &pair : results_)
43-
{
44-
printf("%d,%d,%2.2f,%d;%d \n",
45-
pair.first.altitude_difference_,
46-
static_cast<int>(pair.first.wind_direction_),
47-
pair.first.wind_speed_,
48-
static_cast<int>(pair.first.investigated_direction_),
49-
pair.second);
50-
result_distribution[pair.second]++;
51-
};
52-
printf("Distribution\n");
53-
for (auto &&i : result_distribution)
54-
{
55-
printf("%d;%d\n", i.first, i.second);
56-
};
57-
printf("Distribution direction\n");
58-
for (auto &&i : results_directions_)
59-
{
60-
printf("%d;%d\n", static_cast<int>(i.first), i.second);
61-
};
41+
// printf("Results: altitude_difference_, wind_direction_, wind_speed_, investigated_direction_, number_of_calculations\n");
42+
// for (const auto &pair : results_)
43+
// {
44+
// printf("%d,%d,%2.2f,%d;%d \n",
45+
// pair.first.altitude_difference_,
46+
// static_cast<int>(pair.first.wind_direction_),
47+
// pair.first.wind_speed_,
48+
// static_cast<int>(pair.first.investigated_direction_),
49+
// pair.second);
50+
// result_distribution[pair.second]++;
51+
// };
52+
printf("Overall timer: %ld\n", overall_timer_);
53+
54+
// printf("Distribution\n");
55+
// for (auto &&i : result_distribution)
56+
// {
57+
// printf("%d;%d\n", i.first, i.second);
58+
// };
59+
// printf("Distribution direction\n");
60+
// for (auto &&i : results_directions_)
61+
// {
62+
// printf("%d;%d\n", static_cast<int>(i.first), i.second);
63+
// };
6264

6365
primary_class_->~Math();
6466
}

src/main.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,34 @@ int main()
2020
{
2121

2222
printf("Size of cell from %.2ld to %.2ld bytes \n", sizeof(cell), sizeof(cell) + sizeof(Fire) + sizeof(Wind));
23-
2423
Connection c = Connection();
2524
Math1 formula = Math1();
2625
auto formulaDecorator = ProfilingDecorator(&formula);
2726
CellStorage s = CellStorage(&formulaDecorator);
2827
auto xRange = std::make_pair<int, int>(0, getXArea());
2928
auto yRange = std::make_pair<int, int>(0, getYArea()/2);
30-
auto w = std::make_shared<const Wind>(directions::SouthWest, float(5.0), 80); //Surgut
29+
auto w = std::make_shared<const Wind>(directions::SouthWest, float(2.0), 20); //Surgut
3130
// auto w = std::make_shared<const Wind>(directions::NorthEast, float(2.0)); // Vanderhoof
3231

3332
s.setWindToArea(xRange, yRange, w);
3433

3534
yRange = std::make_pair<int, int>(getYArea()/2, getYArea());
36-
w = std::make_shared<const Wind>(directions::SouthEast, float(2.0), 80);
35+
w = std::make_shared<const Wind>(directions::SouthWest, float(2.0), 20);
3736
s.setWindToArea(xRange, yRange, w);
3837

39-
for (size_t i = 0; i < 10; i++)
40-
{
38+
// for (size_t i = 0; i < 10; i++)
39+
// {
4140

4241
c.setStatesToStorage(s);
4342

4443
clock_t tStart = clock();
4544
for (size_t i = 0; i < numberOfSimulations(); i++)
4645
{
4746
s.iterate();
48-
if ((i % 10 == 0) && (i != 0))
49-
{
50-
std::cout << "we have calculated " << i << " iterations" << std::endl;
51-
}
47+
// if ((i % 10 == 0) && (i != 0))
48+
// {
49+
// std::cout << "we have calculated " << i << " iterations" << std::endl;
50+
// }
5251
// s.printCurrentStates();
5352
}
5453
// PROCESS_MEMORY_COUNTERS memCounter;
@@ -57,7 +56,7 @@ int main()
5756

5857
printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
5958

60-
};
59+
// };
6160
s.printCurrentStates();
6261
// s.saveFiresToJson();
6362

@@ -71,11 +70,18 @@ int main()
7170
// fired.push_back(std::make_pair<int, int>(9, 10));
7271
CellStorage other_s = CellStorage(&formulaDecorator);
7372
other_s.uploadFromTxt();
73+
other_s.iterate();
74+
// other_s.printCurrentStates();
7475
clock_t tStartMetrics = clock();
7576
// m->calculateVariables(s, fired, burnt);
7677

7778
m->calculateVariablesFrom2Storages(s, other_s);
7879
printf("Jaccard metric is equal to: %f\n", m->compute());
80+
auto simpson_metric = new SimpsonMetric(*m);
81+
printf("%s metric is equal to: %f\n", simpson_metric->metricName(), simpson_metric->compute());
82+
auto sneath_metric = new SneathMetric(*m);
83+
printf("%s metric is equal to: %f\n", sneath_metric->metricName(), sneath_metric->compute());
84+
m->printConfusionMatrix();
7985
printf("Time taken: %.2fs\n", (double)(clock() - tStartMetrics) / CLOCKS_PER_SEC);
8086
return 0;
8187
}

0 commit comments

Comments
 (0)