Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
halfplane_intersection_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
45# include <geom_algorithms.H>
46
47# include <cassert>
48# include <fstream>
49# include <iomanip>
50# include <iostream>
51
52using namespace Aleph;
53using namespace std;
54
55static void print_banner(const char * title)
56{
57 cout << "[Aleph Geometry Example] " << title << "\n";
58 cout << "============================================================\n";
59}
60
61static Geom_Number objective(const Point & p)
62{
63 // Maximize z = 3x + 2y
64 return 3 * p.get_x() + 2 * p.get_y();
65}
66
67static void write_polygon_wkt(ostream & out, const Polygon & poly)
68{
69 out << "POLYGON((";
70
71 bool first = true;
73 for (Polygon::Vertex_Iterator it(poly); it.has_curr(); it.next_ne())
74 {
75 const Point & p = it.get_current_vertex();
76 if (first)
77 {
78 first_point = p;
79 first = false;
80 }
81 else
82 out << ", ";
83
84 out << geom_number_to_double(p.get_x()) << " "
86 }
87
88 if (not first)
89 out << ", " << geom_number_to_double(first_point.get_x()) << " "
91
92 out << "))";
93}
94
95static bool export_csv(const char * path,
96 const Polygon & feasible,
97 const Point & best_point,
98 const Geom_Number & best_value)
99{
100 ofstream file(path);
101 if (not file)
102 return false;
103
104 file << fixed << setprecision(8);
105 file << "type,index,x,y,objective,wkt\n";
106
107 size_t idx = 0;
108 for (Polygon::Vertex_Iterator it(feasible); it.has_curr(); it.next_ne(), ++idx)
109 {
110 const Vertex & v = it.get_current_vertex();
111 file << "vertex," << idx << ","
112 << geom_number_to_double(v.get_x()) << ","
113 << geom_number_to_double(v.get_y()) << ","
114 << objective(v) << ",\n";
115 }
116
117 file << "optimum,-1,"
118 << geom_number_to_double(best_point.get_x()) << ","
119 << geom_number_to_double(best_point.get_y()) << ","
120 << best_value << ",\n";
121
122 file << "polygon,-1,,,,\"";
123 write_polygon_wkt(file, feasible);
124 file << "\"\n";
125 return true;
126}
127
128int main(int argc, char ** argv)
129{
131
132 print_banner("Half-Plane Intersection / 2D LP");
133 cout << "Feasible region of constraints:" << endl;
134 cout << " x >= 0, y >= 0, x <= 4, y <= 4, x + y <= 6" << endl;
135 cout << "Objective: maximize z = 3x + 2y" << endl;
136
138 hps.append(HP(Point(0, 1), Point(0, 0))); // x >= 0
139 hps.append(HP(Point(0, 0), Point(1, 0))); // y >= 0
140 hps.append(HP(Point(4, 0), Point(4, 1))); // x <= 4
141 hps.append(HP(Point(1, 4), Point(0, 4))); // y <= 4
142 hps.append(HP(Point(6, 0), Point(0, 6))); // x + y <= 6
143
145 Polygon feasible = hpi(hps);
146
147 assert(feasible.is_closed());
148 assert(feasible.size() >= 3);
149
150 cout << "\nFeasible polygon vertices:" << endl;
152 Geom_Number best_value = 0;
153 bool initialized = false;
154
155 for (Polygon::Vertex_Iterator it(feasible); it.has_curr(); it.next_ne())
156 {
157 const Vertex & v = it.get_current_vertex();
158 const Geom_Number z = objective(v);
159 cout << " (" << geom_number_to_double(v.get_x()) << ", "
160 << geom_number_to_double(v.get_y()) << "), z=" << z << endl;
161
162 if (not initialized or z > best_value)
163 {
164 best_value = z;
165 best_point = v;
166 initialized = true;
167 }
168 }
169
171 cout << "\nOptimal vertex for z=3x+2y: ("
172 << geom_number_to_double(best_point.get_x()) << ", "
173 << geom_number_to_double(best_point.get_y()) << ")" << endl;
174 cout << "Optimal value z* = " << best_value << endl;
175
176 const char * csv_path =
177 (argc > 1) ? argv[1] : "halfplane_intersection_output.csv";
178 if (export_csv(csv_path, feasible, best_point, best_value))
179 cout << "CSV/WKT exported to: " << csv_path << endl;
180 else
181 cout << "Warning: cannot export CSV to: " << csv_path << endl;
182
183 cout << "STATUS: OK" << endl;
184
185 return 0;
186}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
Exact bounded intersection of half-planes.
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:457
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:466
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:246
const bool & is_closed() const
Check if the polygon is closed.
Definition polygon.H:473
const size_t & size() const
Get the number of vertices.
Definition polygon.H:477
A vertex in a polygon's doubly linked vertex list.
Definition polygon.H:119
Computational geometry algorithms.
static Geom_Number objective(const Point &p)
static void write_polygon_wkt(ostream &out, const Polygon &poly)
static bool export_csv(const char *path, const Polygon &feasible, const Point &best_point, const Geom_Number &best_value)
static void print_banner(const char *title)
static int initialized
Definition mpfr_mul_d.c:4
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
Definition point.H:122
STL namespace.
Iterator over the vertices of a polygon.
Definition polygon.H:489
ofstream file
Definition writeRb.C:46