Cleanup and non-class print solutions
This commit is contained in:
parent
928d02d947
commit
4622be5121
Binary file not shown.
Binary file not shown.
BIN
build/libHLlib.a
BIN
build/libHLlib.a
Binary file not shown.
BIN
build/main
BIN
build/main
Binary file not shown.
@ -42,6 +42,34 @@ void HL::print_sol()
|
|||||||
std::cout << "Line ID " << std::get<0>(*itr) << " visible from x=" <<
|
std::cout << "Line ID " << std::get<0>(*itr) << " visible from x=" <<
|
||||||
start_print << " to x=" << stop_print << std::endl;
|
start_print << " to x=" << stop_print << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
auto lines = get_lines();
|
||||||
|
for(Line line : lines){
|
||||||
|
std::cout << "Line ID " << line.get_id() << " visible from x=" <<
|
||||||
|
line.get_vis_start() << " to x=" << line.get_vis_end() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_sol(std::vector<Line>& lines)
|
||||||
|
{
|
||||||
|
for(auto itr = lines.begin(); itr != lines.end(); itr++){
|
||||||
|
Line line = *itr;
|
||||||
|
double start_range = line.get_vis_start();
|
||||||
|
std::string start_print = std::to_string(start_range);
|
||||||
|
if(start_range == std::numeric_limits<double>::lowest())
|
||||||
|
start_print = "-inf";
|
||||||
|
|
||||||
|
double stop_range = line.get_vis_end();
|
||||||
|
std::string stop_print = std::to_string(stop_range);
|
||||||
|
if(stop_range == std::numeric_limits<double>::max())
|
||||||
|
stop_print = "inf";
|
||||||
|
|
||||||
|
if (start_range == stop_range)
|
||||||
|
continue;
|
||||||
|
std::cout << "Line ID " << line.get_id() << " visible from x=" <<
|
||||||
|
start_print << " to x=" << stop_print << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Divide and Conquer Strategy using recursive call to divide ls and merge using merge()
|
//Divide and Conquer Strategy using recursive call to divide ls and merge using merge()
|
||||||
@ -59,92 +87,53 @@ std::vector<Line> HL::gen_sol(std::vector<Line>& ls){
|
|||||||
auto end_itr = ls.begin();
|
auto end_itr = ls.begin();
|
||||||
std::advance(end_itr, split);
|
std::advance(end_itr, split);
|
||||||
|
|
||||||
for(auto itr = ls.begin(); itr != end_itr; itr++){
|
for(std::vector<Line>::iterator itr = ls.begin(); itr != end_itr; itr++){
|
||||||
lh.push_back(*itr);
|
lh.push_back(*itr);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto itr = end_itr; itr != ls.end(); itr++){
|
for(std::vector<Line>::iterator itr = end_itr; itr != ls.end(); itr++){
|
||||||
rh.push_back(*itr);
|
rh.push_back(*itr);
|
||||||
}
|
}
|
||||||
std::cout << "Left half is size " << lh.size() << " and right half is size " << rh.size() << std::endl;
|
|
||||||
|
|
||||||
//Recursive call
|
//Recursive call
|
||||||
auto merged = merge(inst.gen_sol(lh), inst.gen_sol(rh));
|
auto merged = merge(inst.gen_sol(lh), inst.gen_sol(rh));
|
||||||
|
|
||||||
for (auto itr = merged.begin(); itr != merged.end(); itr++){
|
for (std::vector<Line>::iterator itr = merged.begin(); itr != merged.end(); itr++){
|
||||||
Line l1 = *itr;
|
Line l1 = *itr;
|
||||||
sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end())));
|
|
||||||
|
std::vector<std::pair<int, std::pair<double, double>>>::iterator it = std::find_if(sol.begin(), sol.end(),
|
||||||
|
[l1](const std::pair<int, std::pair<double, double>>& p )
|
||||||
|
{ return (std::get<0>(p) == l1.get_id()); });
|
||||||
|
|
||||||
|
if(it != sol.end())
|
||||||
|
*it = std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end()));
|
||||||
|
else
|
||||||
|
sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end())));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Cleanup Solution
|
|
||||||
/*
|
|
||||||
std::set<std::pair<int, std::pair<double, double>>> new_sol;
|
|
||||||
for(std::set<std::pair<int, std::pair<double, double>>>::iterator itr = sol.begin(); itr != sol.end(); itr++){
|
|
||||||
auto start_range = std::get<0>(std::get<1>(*itr));
|
|
||||||
auto stop_range = std::get<1>(std::get<1>(*itr));
|
|
||||||
double my_slope = std::get<1>(*line_holder.find(std::get<0>(*itr))).get_slope();
|
|
||||||
int my_id = std::get<0>(*itr);
|
|
||||||
|
|
||||||
if(start_range == stop_range)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
//Handle overlaps
|
|
||||||
|
|
||||||
std::set<std::pair<int, std::pair<double, double>>>::iterator it = std::find_if(sol.begin(), sol.end(),
|
|
||||||
[start_range, my_id](const std::pair<int, std::pair<double, double>>& p )
|
|
||||||
{ return (std::get<0>(std::get<1>(p)) == start_range && std::get<0>(p) != my_id); });
|
|
||||||
|
|
||||||
if (it != sol.end()){
|
|
||||||
double stored_stop = std::get<1>(std::get<1>(*it));
|
|
||||||
double stored_slope = std::get<1>(*line_holder.find(std::get<0>(*it))).get_slope();
|
|
||||||
int stored_id = std::get<0>(*it);
|
|
||||||
|
|
||||||
std::cout << "Matched" << std::endl;
|
|
||||||
//Change lines' vis starts and ends
|
|
||||||
if (my_slope < stored_slope){
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_start(start_range);
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_end(stored_stop);
|
|
||||||
new_sol.insert(std::make_pair(my_id, std::make_pair(start_range, stored_stop)));
|
|
||||||
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_start(stored_stop);
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_end(stop_range);
|
|
||||||
new_sol.insert(std::make_pair(stored_id, std::make_pair(stored_stop, stop_range)));
|
|
||||||
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_start(start_range);
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*it))).set_vis_end(stored_stop);
|
|
||||||
new_sol.insert(std::make_pair(stored_id, std::make_pair(start_range, stored_stop)));
|
|
||||||
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_start(stored_stop);
|
|
||||||
std::get<1>(*line_holder.find(std::get<0>(*itr))).set_vis_end(stop_range);
|
|
||||||
new_sol.insert(std::make_pair(my_id, std::make_pair(stored_stop, stop_range)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else{
|
|
||||||
new_sol.insert(*itr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
inst.set_sol(new_sol);
|
|
||||||
*/
|
|
||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set with 2 lines is a base case: both are visible at +-inf respectively, and intersection is where they change
|
//Set with 2 lines is a base case: both are visible at +-inf respectively, and intersection is where they change
|
||||||
//visibility
|
//visibility
|
||||||
else if (ls.size() == 2){
|
else if (ls.size() == 2){
|
||||||
Line l1 = *(ls.begin());
|
std::vector<Line>::iterator bit = ls.begin();
|
||||||
Line l2 = *(ls.end());
|
std::vector<Line>::iterator eit = std::prev(ls.end());
|
||||||
|
Line l1 = *(bit);
|
||||||
|
Line l2 = *(eit);
|
||||||
double isec = (l2.get_ycept() - l1.get_ycept())/(l1.get_slope() - l2.get_slope());
|
double isec = (l2.get_ycept() - l1.get_ycept())/(l1.get_slope() - l2.get_slope());
|
||||||
|
|
||||||
if (l1.get_slope() < l2.get_slope()){
|
if (l1.get_slope() < l2.get_slope()){
|
||||||
l1.set_vis_end(isec);
|
l1.set_vis_end(isec);
|
||||||
|
*bit = l1;
|
||||||
l2.set_vis_start(isec);
|
l2.set_vis_start(isec);
|
||||||
|
*eit = l2;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
l2.set_vis_end(isec);
|
l2.set_vis_end(isec);
|
||||||
l1.set_vis_start(isec);
|
l1.set_vis_start(isec);
|
||||||
|
*bit = l1;
|
||||||
|
*eit = l2;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Insert partial solutions
|
//Insert partial solutions
|
||||||
@ -216,12 +205,10 @@ std::vector<Line> HL::construct_HWprob(){
|
|||||||
<< " and a y-intercept of " << line.get_ycept() << std::endl;
|
<< " and a y-intercept of " << line.get_ycept() << std::endl;
|
||||||
if (it == lines.begin()){
|
if (it == lines.begin()){
|
||||||
line.set_vis_start(std::numeric_limits<double>::lowest());
|
line.set_vis_start(std::numeric_limits<double>::lowest());
|
||||||
std::cout << "Changed Line " << line.get_id() << " start vis to " << line.get_vis_start() << std::endl;
|
|
||||||
*it = line;
|
*it = line;
|
||||||
}
|
}
|
||||||
if (it == (std::prev(lines.end()))){
|
if (it == (std::prev(lines.end()))){
|
||||||
line.set_vis_end(std::numeric_limits<double>::max());
|
line.set_vis_end(std::numeric_limits<double>::max());
|
||||||
std::cout << "Changed Line " << line.get_id() << " stop vis to " << line.get_vis_end() << std::endl;
|
|
||||||
*it = line;
|
*it = line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -233,6 +220,7 @@ std::vector<Line> HL::construct_HWprob(){
|
|||||||
std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
|
std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
|
||||||
std::vector<Line> merged;
|
std::vector<Line> merged;
|
||||||
|
|
||||||
|
std::cout << "Merging a left half of size " << lh.size() << " and a right half of size " << rh.size() << std::endl;
|
||||||
auto litr = lh.begin();
|
auto litr = lh.begin();
|
||||||
auto ritr = rh.begin();
|
auto ritr = rh.begin();
|
||||||
|
|
||||||
@ -249,7 +237,7 @@ std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
|
|||||||
l2.set_vis_start(isec);
|
l2.set_vis_start(isec);
|
||||||
}
|
}
|
||||||
merged.push_back(l1);
|
merged.push_back(l1);
|
||||||
merged.push_back(l2);
|
//merged.push_back(l2);
|
||||||
litr++;
|
litr++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +265,7 @@ std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
|
|||||||
l2.set_vis_end(isec);
|
l2.set_vis_end(isec);
|
||||||
}
|
}
|
||||||
|
|
||||||
merged.push_back(l1);
|
//merged.push_back(l1);
|
||||||
merged.push_back(l2);
|
merged.push_back(l2);
|
||||||
ritr++;
|
ritr++;
|
||||||
}
|
}
|
||||||
@ -286,7 +274,7 @@ std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
|
|||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Line> HL::get_lines() const{
|
std::vector<Line> HL::get_lines(){
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,10 +11,11 @@ class HL
|
|||||||
HL();
|
HL();
|
||||||
|
|
||||||
void print_sol();
|
void print_sol();
|
||||||
|
|
||||||
std::vector<Line> gen_sol(std::vector<Line>& ls);
|
std::vector<Line> gen_sol(std::vector<Line>& ls);
|
||||||
std::vector<Line> construct_HWprob();
|
std::vector<Line> construct_HWprob();
|
||||||
|
|
||||||
std::vector<Line> get_lines() const;
|
std::vector<Line> get_lines();
|
||||||
std::vector<std::pair<int, std::pair<double, double>>> get_sol() const;
|
std::vector<std::pair<int, std::pair<double, double>>> get_sol() const;
|
||||||
void set_sol(std::vector<std::pair<int, std::pair<double, double>>> &new_sol);
|
void set_sol(std::vector<std::pair<int, std::pair<double, double>>> &new_sol);
|
||||||
|
|
||||||
@ -28,3 +29,5 @@ class HL
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh);
|
std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh);
|
||||||
|
void print_sol(std::vector<Line>& lines);
|
||||||
|
|
||||||
|
|||||||
@ -15,9 +15,9 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
std::cout << "Generating Solution.\n" << std::endl;
|
std::cout << "Generating Solution.\n" << std::endl;
|
||||||
auto lines = inst.get_lines();
|
auto lines = inst.get_lines();
|
||||||
inst.gen_sol(lines);
|
auto sol = inst.gen_sol(lines);
|
||||||
|
|
||||||
std::cout << "Solution is: " << std::endl;
|
std::cout << "Solution is: " << std::endl;
|
||||||
inst.print_sol();
|
print_sol(sol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user