Skip to content

Commit 1c0b574

Browse files
make slope optimizations
1 parent d2b06ab commit 1c0b574

File tree

4 files changed

+180
-28
lines changed

4 files changed

+180
-28
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ ADD_LIBRARY(LibsModule
2727
target_link_libraries(LibsModule PRIVATE PostgreSQL::PostgreSQL)
2828

2929
ADD_EXECUTABLE(main src/main.cpp)
30+
ADD_CUSTOM_COMMAND(TARGET main POST_BUILD
31+
COMMAND ${CMAKE_COMMAND} -E copy_directory
32+
${CMAKE_SOURCE_DIR}/data $<TARGET_FILE_DIR:main>/data)
3033
target_link_libraries(main LibsModule)
3134

3235
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY

data/slope.csv

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
-20,0,0.675232
2+
17,0,1.422260
3+
-27,1,0.683119
4+
10,0,1.185208
5+
19,0,1.344144
6+
14,0,1.258360
7+
15,0,1.276099
8+
16,0,1.293566
9+
31,0,1.515261
10+
21,0,1.376205
11+
-21,0,0.726636
12+
-15,0,0.724879
13+
12,0,1.222165
14+
11,0,1.285852
15+
-18,0,0.693220
16+
9,0,1.235440
17+
-14,0,0.736856
18+
25,1,1.436084
19+
21,1,1.376205
20+
18,0,1.327604
21+
18,1,1.327604
22+
-19,1,0.743968
23+
13,0,1.334008
24+
-20,1,0.735107
25+
14,1,1.357118
26+
19,1,1.344144
27+
8,0,1.147771
28+
6,0,1.156882
29+
-25,0,0.696338
30+
-14,1,0.794685
31+
-16,1,0.773057
32+
-21,1,0.726636
33+
22,1,1.391711
34+
-10,1,0.793086
35+
2,0,1.035927
36+
31,1,1.515261
37+
10,1,1.185208
38+
-2,1,0.951387
39+
1,0,1.017827
40+
-11,0,0.777694
41+
-8,0,0.826750
42+
-5,0,0.916233
43+
-2,0,0.965319
44+
-1,1,0.975330
45+
0,1,1.000000
46+
1,1,1.025294
47+
-6,1,0.900760
48+
6,1,1.156882
49+
-7,1,0.885763
50+
5,1,1.091425
51+
3,1,1.077286
52+
-1,0,0.975330
53+
0,0,1.000000
54+
-3,1,0.948536
55+
2,1,1.051097
56+
9,1,1.235440
57+
3,0,1.077286
58+
-3,0,0.948536
59+
23,1,1.406861
60+
-9,1,0.857243
61+
7,1,1.128971
62+
-5,1,0.916233
63+
8,1,1.147771
64+
-4,1,0.932166
65+
15,1,1.276099
66+
-17,1,0.762928
67+
-7,0,0.845069
68+
-8,1,0.826750
69+
24,1,1.421652
70+
4,0,1.072771
71+
20,1,1.360347
72+
-12,1,0.763219
73+
5,0,1.091425
74+
-4,0,0.932166
75+
-9,0,0.809428
76+
7,0,1.128971
77+
4,1,1.103733
78+
-6,0,0.864392
79+
-10,0,0.843734
80+
17,1,1.310740
81+
-15,1,0.783638
82+
-16,0,0.713644
83+
-17,0,0.762928
84+
11,1,1.203765
85+
12,1,1.222165
86+
16,1,1.293566
87+
13,1,1.240373
88+
-11,1,0.830727
89+
-13,1,0.806209
90+
-13,0,0.749621
91+
-12,0,0.818220

include/Math.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#include <cmath>
33
#include "Cell.h"
44
#include <map>
5+
#include <time.h>
56
#include <unordered_map>
67

78
struct SlopeMetaData
89
{
9-
directions investigated_direction_;
10+
bool diagonal_direction_;
1011
int altitude_difference_;
1112
bool operator==(const SlopeMetaData &rhs) const
1213
{
13-
return investigated_direction_ == rhs.investigated_direction_ &&
14+
return diagonal_direction_ == rhs.diagonal_direction_ &&
1415
altitude_difference_ == rhs.altitude_difference_;
1516
}
1617
};
@@ -20,7 +21,7 @@ struct std::hash<SlopeMetaData>
2021
{
2122
std::size_t operator()(const SlopeMetaData &key) const
2223
{
23-
size_t h1 = std::hash<int>()(static_cast<int>(key.investigated_direction_));
24+
size_t h1 = std::hash<int>()(key.diagonal_direction_);
2425
size_t h2 = std::hash<int>()(key.altitude_difference_);
2526
return h1 ^ (h2 << 1);
2627
}
@@ -54,16 +55,19 @@ constexpr int throughPercentage(){
5455
class Math1 final: public Math
5556
{
5657
private:
57-
mutable std::unordered_map<SlopeMetaData, int> result_slope_;
58+
mutable std::unordered_map<SlopeMetaData, int> slope_counter_;
59+
mutable std::unordered_map<SlopeMetaData, double> slope_result_;
5860
mutable std::map<double, int> result_wind_;
5961
mutable std::map<int, int> result_overall;
62+
mutable clock_t slope_timer;
63+
mutable int slope_counter;
6064
double calculateKoef(float windSpeed, double slopeAngleRad) const;
6165
double calculateWindKoef(const cell* c, directions InvestigatedDirection) const;
6266
double calculateGroundSlopeKoef(directions InvestigatedDirection, int altitudeDifference) const;
6367
public:
6468
bool willSpread(const cell* c, directions InvestigatedDirection, int altitudeDifference) const override;
6569
bool willSpreadThroughOne(const cell* c, directions InvestigatedDirection, int altitudeDifference) const override;
66-
Math1() = default;
70+
Math1();
6771
~Math1();
6872
};
6973

src/Math.cpp

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "../include/Properties.h"
33
#include <cmath>
44
#include "Math.h"
5+
#include <fstream>
6+
#include <sstream>
7+
#include <vector>
58

69
Math::Math(/* args */)
710
{
@@ -33,6 +36,19 @@ double Math1::calculateWindKoef(const cell *c, directions InvestigatedDirection)
3336

3437
double Math1::calculateGroundSlopeKoef(directions InvestigatedDirection, int altitudeDifference) const
3538
{
39+
clock_t start = clock();
40+
41+
SlopeMetaData meta = {bool(static_cast<int>(InvestigatedDirection) / int(2)), altitudeDifference};
42+
43+
// auto a = slope_result_[meta];
44+
// if (a != 0)
45+
// {
46+
// clock_t per_iteration = clock() - start;
47+
// slope_timer = per_iteration+slope_timer;
48+
// slope_counter++;
49+
// return a;
50+
// }
51+
3652
double coef = 0;
3753
if (InvestigatedDirection == directions::North || InvestigatedDirection == directions::West ||
3854
InvestigatedDirection == directions::East || InvestigatedDirection == directions::South)
@@ -44,8 +60,13 @@ double Math1::calculateGroundSlopeKoef(directions InvestigatedDirection, int alt
4460
coef = atan2(static_cast<double>(altitudeDifference), static_cast<double>(cellSizeInMeters() * sqrt(2.0)));
4561
}
4662
double result = std::exp(0.5 * coef);
47-
SlopeMetaData meta = {InvestigatedDirection, altitudeDifference};
48-
result_slope_[meta]++;
63+
// SlopeMetaData meta = {bool(static_cast<int>(InvestigatedDirection) / int(2)), altitudeDifference};
64+
slope_counter_[meta]++;
65+
// slope_result_[meta] = result;
66+
67+
clock_t per_iteration = clock() - start;
68+
slope_timer = per_iteration+slope_timer;
69+
slope_counter++;
4970
return result;
5071
}
5172

@@ -65,28 +86,61 @@ bool Math1::willSpreadThroughOne(const cell *c, directions InvestigatedDirection
6586
return int(fireKoeff * slopeKoeff * 100) + (rand() % 30) > throughPercentage();
6687
}
6788

68-
Math1::~Math1()
89+
bool to_bool(std::string const &s)
6990
{
70-
printf("slope profiling\n");
71-
std::map<int, int> slope_distribution;
72-
for (auto &&i : result_slope_)
73-
{
74-
printf("%d,%d;%d\n", i.first.altitude_difference_, static_cast<int>(i.first.investigated_direction_), i.second);
75-
slope_distribution[i.second]++;
76-
}
77-
printf("slope distribution\n");
78-
for (auto &&i : slope_distribution)
79-
{
80-
printf("%d;%d\n", i.first, i.second);
81-
}
82-
printf("wind profiling\n");
83-
for (auto &&i : result_wind_)
84-
{
85-
printf("%2.2f;%d\n", i.first, i.second);
86-
}
87-
printf("overall profiling\n");
88-
for (auto &&i : result_overall)
91+
return s != "0";
92+
}
93+
94+
Math1::Math1()
95+
{
96+
std::ifstream csvFile{"data/slope.csv"};
97+
std::string row;
98+
while (std::getline(csvFile, row))
8999
{
90-
printf("%d;%d\n", i.first, i.second);
100+
std::stringstream rowStream(row);
101+
std::string col;
102+
std::vector<std::string> cols;
103+
while (std::getline(rowStream, col, ','))
104+
{
105+
cols.push_back(col);
106+
}
107+
auto a = atoi(cols[0].c_str());
108+
slope_result_[SlopeMetaData{to_bool(cols[0].c_str()), a}] = atof(cols[2].c_str());
91109
}
110+
csvFile.close();
111+
slope_counter = 0;
112+
slope_timer = 0;
113+
}
114+
115+
Math1::~Math1()
116+
{
117+
printf("total time %f, overall counts %d", (double)(slope_timer), slope_counter);
118+
// printf("slope profiling\n");
119+
// std::map<int, int> slope_distribution;
120+
121+
// for (auto &&i : slope_result_)
122+
// {
123+
// printf("%d,%d,%f\n", i.first.altitude_difference_, i.first.diagonal_direction_, i.second);
124+
// }
125+
126+
// for (auto &&i : slope_counter_)
127+
// {
128+
// printf("%d,%d;%d\n", i.first.altitude_difference_, i.first.diagonal_direction_, i.second);
129+
// slope_distribution[i.second]++;
130+
// }
131+
// printf("slope distribution\n");
132+
// for (auto &&i : slope_distribution)
133+
// {
134+
// printf("%d;%d\n", i.first, i.second);
135+
// }
136+
// printf("wind profiling\n");
137+
// for (auto &&i : result_wind_)
138+
// {
139+
// printf("%2.2f;%d\n", i.first, i.second);
140+
// }
141+
// printf("overall profiling\n");
142+
// for (auto &&i : result_overall)
143+
// {
144+
// printf("%d;%d\n", i.first, i.second);
145+
// }
92146
}

0 commit comments

Comments
 (0)