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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
|
/* Copyright (c) 1997-2024
Ewgenij Gawrilow, Michael Joswig, and the polymake team
Technische Universität Berlin, Germany
https://polymake.org
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
--------------------------------------------------------------------------------
*/
#pragma once
#include "polymake/client.h"
#include "polymake/linalg.h"
#include "polymake/Vector.h"
#include "polymake/Matrix.h"
#include "polymake/ListMatrix.h"
#include "polymake/IncidenceMatrix.h"
#include "polymake/Graph.h"
#include "polymake/Bitset.h"
#include "polymake/Set.h"
#include "polymake/Array.h"
#include "polymake/list"
#include <deque>
namespace polymake { namespace polytope {
/** Encapsulating the beneath-beyond algorithm.
One instance is used to solve exactly one convex-hull problem.
@tmplparam E numerical type of the coordinates
*/
template <typename E>
class beneath_beyond_algo {
public:
beneath_beyond_algo()
: expect_redundant(false)
, make_triangulation(true)
, is_cone(false)
, compute_vertices(false)
{
dual_graph.attach(facets);
dual_graph.attach(ridges);
}
/// flag==true: input points may contain redundancies (INPUT_RAYS, INEQUALITIES)
/// flag==false: inpit points are free from redundancies (RAYS, FACETS)
beneath_beyond_algo& expecting_redundant(bool flag)
{
expect_redundant = flag;
return *this;
}
/// flag==true: input points are cone rays/facets
/// flag==false: input points are polytope vertices/facets
beneath_beyond_algo& for_cone(bool flag)
{
is_cone = flag;
return *this;
}
/// flag==true: compute the triangulation
/// flag==false: don't compute the triangulation
beneath_beyond_algo& making_triangulation(bool flag)
{
make_triangulation = flag;
return *this;
}
/// flag==true: input vectors are hyperplanes, result is vertices
/// flag==false: input vectors are rays, result is facets
beneath_beyond_algo& computing_vertices(bool flag)
{
compute_vertices = flag;
return *this;
}
void compute(const Matrix<E>& rays, const Matrix<E>& lins)
{
#if POLYMAKE_DEBUG
enable_debug_output();
#endif
compute(rays, lins, entire(sequence(0, rays.rows())));
}
// TODO: bundle all results in a structure, move all numbers into it
template <typename Iterator>
void compute(const Matrix<E>& rays, const Matrix<E>& lins, Iterator perm);
Matrix<E> getFacets() const;
IncidenceMatrix<> getVertexFacetIncidence() const;
Matrix<E> getAffineHull() const;
Matrix<E> getVertices() const;
Bitset getNonRedundantPoints() const
{
Bitset result(sequence(0, source_points->rows()));
result -= interior_points;
return result;
}
Set<Int> getNonRedundantLinealities() const
{
const Int n_points = source_points->rows();
Set<Int> result = points_in_lineality_basis;
for (const Int p : source_lineality_basis)
result += p + n_points;
return result;
}
Matrix<E> getLinealities() const
{
return linealities_so_far;
}
Graph<> getDualGraph() const
{
return dual_graph;
}
Array<Set<Int>> getTriangulation() const
{
return Array<Set<Int>>(triang_size, triangulation.rbegin());
}
bool getGenericPosition() const
{
return generic_position;
}
protected:
// connects a facet with a triangulation simplex
struct incident_simplex {
const Set<Int>* simplex; // an element of triangulation
Int opposite_vertex; // the only vertex NOT belonging to the facet
incident_simplex(const Set<Int>& simplex_arg, Int vertex_arg)
: simplex(&simplex_arg), opposite_vertex(vertex_arg) { }
};
// description of a facet; stored as a node attribute of the dual graph
struct facet_info {
Vector<E> normal; // normal vector, directed inside the polyhedron
E sqr_normal; // sqr(normal)
Int orientation; // sign(normal * current-point-to-be-added) during one algo step
// =0 : facet is incident, <0 : facet is violated (and will die)
Set<Int> vertices; // ... incident to the facet
// simplices from the polytope triangulation contributing to the triangulation of this facet
using simplex_list = std::list<incident_simplex>;
simplex_list simplices;
// compute the normal vector etc. assuming the full-dimensional case (Affine Hull is empty)
void coord_full_dim(const beneath_beyond_algo&);
// compute the normal vector etc. in the low-dimensional case
void coord_low_dim(const beneath_beyond_algo&);
// check the intersection of triangulation simplices from [s, s_end) with the facet,
// include those with a (d-1)-face ompletely belonging to the facet in its simplex_list
template <typename Iterator>
void add_incident_simplices(Iterator s, Iterator s_end);
// enables efficient memory management in the dual graph
friend void relocate(facet_info* from, facet_info* to)
{
relocate(&from->normal, &to->normal);
relocate(&from->sqr_normal, &to->sqr_normal);
to->orientation = from->orientation;
relocate(&from->vertices, &to->vertices);
pm::relocate(&from->simplices, &to->simplices);
}
#if POLYMAKE_DEBUG
template <typename Output> friend
Output& operator<< (GenericOutput<Output>& os, const facet_info& me)
{
return os.top() << me.normal << ' ' << me.vertices;
}
#endif
};
const Matrix<E>* source_points;
Matrix<E> transformed_points;
const Matrix<E>* points;
const Matrix<E>* source_linealities;
Matrix<E> linealities_so_far;
const Matrix<E>* linealities;
Matrix<E> lineality_transform;
bool expect_redundant;
bool make_triangulation;
bool is_cone;
bool compute_vertices;
enum class compute_state { zero, one, low_dim, full_dim };
compute_state state;
Graph<> dual_graph;
using facets_t = NodeMap<Undirected,facet_info>;
using ridges_t = EdgeMap<Undirected, Set<Int>>;
facets_t facets;
ridges_t ridges;
ListMatrix<SparseVector<E>> AH; // affine hull
ListMatrix<SparseVector<E>> facet_nullspace; // its affine nullspace - is not computed as long as
// the consumed input vertices comprise a simplex
Bitset interior_points; // indices of points which are not vertices
Set<Int> source_lineality_basis, points_in_lineality_basis;
using Triangulation = std::list<Set<Int>>;
Triangulation triangulation;
// These are working variables valid within one algo step.
// We define them as instance variables nevertheless to avoid the repeating allocation and deallocation.
Bitset vertices_this_step, // points proved to be non-redundant
interior_points_this_step, // points that could be redundant
visited_facets; // facets seen
std::deque<Int> facet_queue; // BFS queue for update_facets()
// accumulates the non-redundant points; is filled until the polytope turns out to be full-dimensional
Set<Int> vertices_so_far;
Int triang_size; // = triangulation.size();
Int valid_facet; // a facet where to start the visibility border search
bool generic_position;
bool facet_normals_valid;
void process_point(Int p);
void add_second_point(Int p);
// add the next point, given by the row index.
// Recalculates the affine hull. If its dimension did not decrease, delegates the rest work to add_point_full_dim()
void add_point_low_dim(Int p);
// The first phase of the step: looking for a facet violated by point p. If found, calls update_facets()
void add_point_full_dim(Int p);
// helper function for add_point_full_dim
Int descend_to_violated_facet(Int f, Int p);
// helper functions
void facet_normals_low_dim();
bool reduce_nullspace(ListMatrix<SparseVector<E>>& M, Int p) const;
// The main phase of the step: detect all facets to be deleted, create new facets and simplices
// @param f the index of the first facet violated by or incident with point p
void update_facets(Int f, Int p);
void process_new_lineality(Int p, const std::list<Int>& incident_facets);
void transform_points();
template <typename ISet>
void add_linealities(const ISet& point_set);
class stop_calculation {};
void complain_redundant(Int p);
#if POLYMAKE_DEBUG
void dump() const;
void dump_p(Int p) const;
void check_f(Int f, Int last_p) const;
void check_p(Int p) const;
void check_fp(Int f_index, const facet_info& f, Int p, std::ostringstream& errors) const;
enum debug_kind { do_nothing, do_check, do_dump, do_full_dump };
debug_kind debug;
void enable_debug_output()
{
switch (get_debug_level()) {
case 0:
debug = do_nothing;
break;
case 1:
debug = do_check;
break;
case 2:
debug = do_dump;
break;
default:
debug = do_full_dump;
break;
}
}
#endif
};
template <typename E>
Matrix<E> beneath_beyond_algo<E>::getFacets() const
{
const auto extract_normals = [this](){
return Matrix<E>(dual_graph.nodes(), points->cols(),
entire(attach_member_accessor(facets, ptr2type<facet_info, Vector<E>, &facet_info::normal>())));
};
if (dual_graph.nodes() != 0 && linealities->rows() != 0) {
return (extract_normals() | zero_matrix<E>(0, linealities->rows())) * T(lineality_transform);
} else if (dual_graph.nodes() != 0) {
return extract_normals();
} else {
// no facets but fix ambient dimension
return Matrix<E>(0,source_points->cols());
}
}
template <typename E>
Matrix<E> beneath_beyond_algo<E>::getAffineHull() const
{
if (AH.rows() != 0 && linealities->rows() != 0) {
return (AH | zero_matrix<E>(0, linealities->rows())) * T(lineality_transform);
} else if (AH.rows() != 0) {
return AH;
} else {
// no affine hull but fix ambient dimension
return Matrix<E>(0,source_points->cols());
}
}
template <typename E>
Matrix<E> beneath_beyond_algo<E>::getVertices() const
{
Matrix<E> V = source_points->minor(~interior_points, All);
if (linealities->rows() != 0 && !compute_vertices) {
// canonicalize vertices: project them on the orthogonal complement of linealities
Matrix<E> Lnorm = *linealities;
for (auto l = entire(rows(Lnorm)); !l.at_end(); ++l)
*l /= sqr(*l);
V -= V * (T(Lnorm) * (*linealities));
}
return V;
}
template <typename E>
IncidenceMatrix<> beneath_beyond_algo<E>::getVertexFacetIncidence() const
{
IncidenceMatrix<> VIF(dual_graph.nodes(), points->rows(),
attach_member_accessor(facets, ptr2type<facet_info, Set<Int>, &facet_info::vertices>()).begin());
if (!expect_redundant) return VIF;
return VIF.minor(All, ~interior_points);
}
template <typename E>
void beneath_beyond_algo<E>::transform_points()
{
const auto lin_compl = null_space(*linealities);
if (expect_redundant && lin_compl.rows() == 0)
// solution space is empty
throw stop_calculation();
lineality_transform = inv(lin_compl / *linealities);
transformed_points = (*source_points * lineality_transform).minor(All, sequence(0, source_points->cols() - linealities->rows()));
points = &transformed_points;
}
template <typename E>
bool beneath_beyond_algo<E>::reduce_nullspace(ListMatrix<SparseVector<E>>& M, Int p) const
{
return basis_of_rowspan_intersect_orthogonal_complement(M, points->row(p), black_hole<Int>(), black_hole<Int>());
}
template <typename E>
template <typename Iterator>
void beneath_beyond_algo<E>::compute(const Matrix<E>& rays, const Matrix<E>& lins, Iterator perm)
{
source_points = &rays;
source_linealities = &lins;
linealities_so_far.resize(0,rays.cols());
try {
if (lins.rows() != 0) {
if (expect_redundant) {
source_lineality_basis = basis_rows(lins);
linealities_so_far = lins.minor(source_lineality_basis, All);
linealities = &linealities_so_far;
} else {
linealities = source_linealities;
}
transform_points();
} else {
points = source_points;
linealities = expect_redundant ? &linealities_so_far : source_linealities;
}
generic_position = !expect_redundant;
triang_size = 0;
AH = unit_matrix<E>(points->cols());
if (expect_redundant) {
interior_points.resize(points->rows());
vertices_this_step.resize(points->rows());
interior_points_this_step.resize(points->rows());
}
for (state = compute_state::zero; !perm.at_end(); ++perm)
process_point(*perm);
if (state == compute_state::low_dim && !facet_normals_valid)
facet_normals_low_dim();
}
catch (const stop_calculation&) {
#if POLYMAKE_DEBUG
if (debug >= do_dump) cout << "stop: degenerated to full linear space" << endl;
#endif
state = compute_state::zero;
dual_graph.clear();
vertices_so_far.clear();
points = source_points;
interior_points = sequence(0, source_points->rows());
if (make_triangulation) {
triangulation.clear();
triang_size = 0;
}
}
switch (state) {
case compute_state::zero:
if (!is_cone) {
// empty polyhedron
AH.resize(0, source_points->cols());
linealities_so_far.resize(0, source_points->cols());
}
break;
case compute_state::one:
// There is one empty facet in this case and the point is also a facet normal
facets[dual_graph.add_node()].normal = points->row(vertices_so_far.front());
if (make_triangulation) {
triang_size=1;
triangulation.push_back(vertices_so_far);
}
break;
case compute_state::low_dim:
case compute_state::full_dim:
dual_graph.squeeze();
break;
}
#if POLYMAKE_DEBUG
if (debug >= do_dump) {
cout << "final ";
dump();
}
#endif
}
template <typename E>
void beneath_beyond_algo<E>::process_point(const Int p)
{
if (expect_redundant && is_zero(points->row(p))) {
interior_points += p;
return;
}
switch (state) {
case compute_state::zero: {
// the first point
reduce_nullspace(AH, p);
vertices_so_far = scalar2set(p);
state = compute_state::one;
break;
}
case compute_state::one: {
add_second_point(p);
break;
}
case compute_state::low_dim:
add_point_low_dim(p);
#if POLYMAKE_DEBUG
if (debug == do_check) check_p(p);
#endif
break;
case compute_state::full_dim:
add_point_full_dim(p);
#if POLYMAKE_DEBUG
if (debug == do_check) check_p(p);
#endif
break;
}
}
template <typename E>
void beneath_beyond_algo<E>::add_second_point(Int p)
{
const Int p0 = vertices_so_far.front();
if (reduce_nullspace(AH, p)) {
// two different points found: initialize the polytope
const Int f0 = dual_graph.add_node();
facets[f0].vertices = vertices_so_far;
const Int f1 = dual_graph.add_node();
facets[f1].vertices = scalar2set(p);
dual_graph.edge(f0, f1);
vertices_so_far += p;
if (make_triangulation) {
triangulation.push_back(vertices_so_far);
triang_size = 1;
facets[f0].simplices.push_back(incident_simplex(triangulation.front(), p));
facets[f1].simplices.push_back(incident_simplex(triangulation.front(), p0));
}
valid_facet = 0;
#if POLYMAKE_DEBUG
if (debug >= do_dump)
cout << "starting points: " << vertices_so_far << "\n" << points->minor(vertices_so_far, All) << "\nAH:\n" << AH << endl;
#endif
if ((facet_normals_valid = (AH.rows() == 0))) {
// dimension==1, will need the facet normals immediately
facets[f0].coord_full_dim(*this);
facets[f1].coord_full_dim(*this);
state = compute_state::full_dim;
} else {
state = compute_state::low_dim;
}
} else if (expect_redundant) {
// p and p0 must be collinear; if the signs are different, they build a linearity
const auto sign_of = [](const auto& v) {
Int s = 0;
for (const auto& x : v) {
if ((s = sign(x)) != 0) break;
}
return s;
};
if (sign_of(points->row(p0)) != sign_of(points->row(p))) {
interior_points += p0;
vertices_so_far.clear();
add_linealities(scalar2set(p0));
#if POLYMAKE_DEBUG
if (debug >= do_dump)
cout << "linealities:\n" << linealities_so_far << endl;
#endif
state = compute_state::zero;
}
interior_points += p;
} else {
complain_redundant(p);
}
}
template <typename E>
template <typename ISet>
void beneath_beyond_algo<E>::add_linealities(const ISet& point_set)
{
const Int lin_rows = linealities_so_far.rows();
linealities_so_far /= source_points->minor(point_set, All);
const Set<Int> lin_basis = basis_rows(linealities_so_far);
linealities_so_far = linealities_so_far.minor(lin_basis, All);
// if new rays have been added to the lineality matrix, their indexes must be greater than those of already known linealities
if (lin_basis.size() > lin_rows) {
// TODO: introduce shifted_set or something similar
const Set<Int> new_points_in_basis(attach_operation(lin_basis - sequence(0, lin_rows), operations::fix2<Int, operations::sub>(lin_rows)));
points_in_lineality_basis += select(point_set, new_points_in_basis);
}
transform_points();
AH = unit_matrix<E>(points->cols());
}
template <typename E>
void beneath_beyond_algo<E>::complain_redundant(Int p)
{
throw std::runtime_error("beneath_beyond_algo: found redundant point " + std::to_string(p) + " while none was expected");
}
/** @param p the new point
@param f the facet index to start from
@retval index of the violated/incident facet or -1 if nothing found
*/
template <typename E>
Int beneath_beyond_algo<E>::descend_to_violated_facet(Int f, Int p)
{
visited_facets += f;
E fxp = facets[f].normal * points->row(p);
if ((facets[f].orientation = sign(fxp)) <= 0) return f;
// starting facet stays valid in this step: let's look for another one violated by p.
// The search is performed in the dual graph, following the steepest descend of the
// (square of the) distance between p and the facets
if (expect_redundant) vertices_this_step += facets[f].vertices;
fxp = fxp * fxp / facets[f].sqr_normal; // square of the distance from p to the facet
Int nextf;
do {
#if POLYMAKE_DEBUG
if (debug >= do_full_dump)
cout << " *" << f << '(' << fxp << ')';
#endif
nextf = -1;
for (auto neighbor = entire(dual_graph.adjacent_nodes(f)); !neighbor.at_end(); ++neighbor) {
const Int f2 = *neighbor;
if (visited_facets.contains(f2)) continue;
visited_facets += f2;
E f2xp = facets[f2].normal * points->row(p);
if ((facets[f2].orientation = sign(f2xp)) <= 0) return f2;
if (expect_redundant) vertices_this_step += facets[f2].vertices;
f2xp = f2xp * f2xp / facets[f2].sqr_normal;
#if POLYMAKE_DEBUG
if (debug >= do_full_dump)
cout << ' ' << f2 << '(' << f2xp << ')';
#endif
if (f2xp <= fxp) {
nextf = f2;
fxp = f2xp;
}
}
} while ((f = nextf) >= 0);
return f; // -1 : local minimum of sqr(distance) reached
}
namespace {
template <typename TSet>
Int first_or_none(const TSet& set)
{
auto s = entire(set);
return s.at_end() ? -1 : *s;
}
}
template <typename E>
void beneath_beyond_algo<E>::add_point_full_dim(Int p)
{
#if POLYMAKE_DEBUG
if (debug >= do_dump)
cout << "point " << p << "=[ " << points->row(p) << " ] : valid facets";
#endif
// reset the working variables
visited_facets.clear();
if (expect_redundant) vertices_this_step.clear();
// first try the facet added last in the previous step
Int try_facet = valid_facet;
do {
if ((try_facet = descend_to_violated_facet(try_facet, p)) >= 0) {
update_facets(try_facet, p);
return;
}
for (auto f=entire(nodes(dual_graph)); !f.at_end(); ++f) {
if (!visited_facets.contains(f.index())) {
try_facet = f.index();
break;
}
}
} while (try_facet >= 0);
// no violated facet found: p must be a redundant point
if (expect_redundant) {
interior_points += p;
#if POLYMAKE_DEBUG
if (debug >= do_dump)
cout << "\ninterior points: " << interior_points
<< "\n=======================================" << endl;
#endif
} else {
complain_redundant(p);
}
}
template <typename E>
void beneath_beyond_algo<E>::facet_normals_low_dim()
{
// facets must be orthogonal to the affine hull
const Int d = points->cols();
facet_nullspace = unit_matrix<E>(d);
if (is_cone) {
null_space(entire(rows(AH)), black_hole<Int>(), black_hole<Int>(), facet_nullspace);
} else {
SparseMatrix<E> AHaff=AH;
// make all hyperplanes going thru the origin, but leave the far hyperplane untouched
const auto far_hyperplane = unit_vector<E>(d, 0);
for (auto r=entire(rows(AHaff)); !r.at_end(); ++r)
if (*r != far_hyperplane)
r->erase(0);
null_space(entire(rows(AHaff)), black_hole<Int>(), black_hole<Int>(), facet_nullspace);
}
for (auto f=entire(facets); !f.at_end(); ++f) {
f->coord_low_dim(*this);
#if POLYMAKE_DEBUG
if (debug >= do_dump) cout << f.index() << ": " << f->vertices << "=[ " << f->normal << " ]\n";
#endif
}
}
template <typename E>
void beneath_beyond_algo<E>::add_point_low_dim(Int p)
{
// update the affine hull
if (reduce_nullspace(AH, p)) {
// point set dimension increased
if (facet_nullspace.rows() != 0) {
generic_position = false; // the base facet is more than a simplex
facet_nullspace.clear();
}
// build a pyramid with the former polytope as a base and the point as an apex
const Int nf_index = dual_graph.add_node();
facet_info& nf = facets[nf_index];
nf.vertices = vertices_so_far;
if (expect_redundant) nf.vertices -= interior_points;
// triangulation simplices are 'pyramidized' too
if (make_triangulation) {
for (auto simplex = entire(triangulation); !simplex.at_end(); ++simplex) {
*simplex += p;
nf.simplices.push_back(incident_simplex(*simplex, p));
}
}
vertices_so_far += p;
if ((facet_normals_valid = AH.rows() == 0))
state = compute_state::full_dim;
for (auto r=entire(ridges); !r.at_end(); ++r)
*r += p;
for (auto f=entire(nodes(dual_graph)); !f.at_end(); ++f) {
// for all facets, except the new one
if (f.index() != nf_index) {
ridges(f.index(), nf_index) = facets[*f].vertices;
facets[*f].vertices += p;
}
if (facet_normals_valid) {
// the polytope became full-dimensional: will need the facet coordinates the whole rest of the time
facets[*f].coord_full_dim(*this);
#if POLYMAKE_DEBUG
if (debug >= do_dump) cout << f.index() << ": " << facets[*f].vertices << "=[ " << facets[*f].normal << " ]\n";
#endif
}
}
#if POLYMAKE_DEBUG
if (debug >= do_dump) cout << "point " << p << "=[" << (*points)[p] << "] : dim increased\nAH:\n" << AH << endl;
#endif
} else {
// point set dimension not increased
if (!facet_normals_valid) {
// the polytope was a simplex, the facet coordinates are still not computed;
// now we need them for the visibility region search.
facet_normals_low_dim();
facet_normals_valid = true;
}
add_point_full_dim(p);
}
}
template <typename E>
void beneath_beyond_algo<E>::update_facets(Int f, const Int p)
{
#if POLYMAKE_DEBUG
if (debug >= do_dump) cout << "\nupdating:";
#endif
facet_queue.clear();
facet_queue.push_back(f);
if (expect_redundant) interior_points_this_step.clear();
std::list<Int> incident_facets;
if (facets[f].orientation == 0) {
facets[f].vertices += p;
generic_position = false;
incident_facets.push_back(f);
}
/* BFS in the visible hemisphere.
We visit all facets violated by or incident with p.
Incident facets are important since they can contain redundant points not discovered before this iteration.
*/
while (!facet_queue.empty()) {
f = facet_queue.front(); facet_queue.pop_front();
const Int f_orientation = facets[f].orientation;
// remember the position where the new simplices will end
auto new_simplex_end = triangulation.begin();
if (f_orientation < 0) {
// the facet is violated
#if POLYMAKE_DEBUG
if (debug >= do_dump) cout << " -" << f;
#endif
if (expect_redundant) interior_points_this_step += facets[f].vertices;
// build new triangulation simplices using the triangulation of the facet
if (make_triangulation) {
for (auto is = entire(facets[f].simplices); !is.at_end(); ++is) {
triangulation.push_front(*is->simplex);
++triang_size;
// just take the existing simplex and replace the vertex behind the facet by the new point
(triangulation.front() -= is->opposite_vertex) += p;
}
}
#if POLYMAKE_DEBUG
} else {
if (debug >= do_dump) cout << " " << f;
#endif
}
// check the neighbor facets
for (auto e=entire(dual_graph.out_edges(f)); !e.at_end(); ++e) {
const Int f2 = e.to_node();
facet_info& nbf = facets[f2];
if (!visited_facets.contains(f2)) {
visited_facets += f2;
nbf.orientation = sign(nbf.normal * points->row(p));
if (nbf.orientation == 0) {
// incident facet
nbf.vertices += p;
generic_position = false;
incident_facets.push_back(f2);
}
if (nbf.orientation <= 0)
facet_queue.push_back(f2);
else if (expect_redundant)
vertices_this_step += nbf.vertices;
}
if (f_orientation < 0) {
if (nbf.orientation > 0) {
// found a ridge on the visibility border: create a new facet
const Int nf_index = dual_graph.add_node();
facet_info& nf = facets[nf_index];
nf.vertices = ridges[*e] + p;
if (AH.rows())
nf.coord_low_dim(*this);
else
nf.coord_full_dim(*this);
#if POLYMAKE_DEBUG
if (debug == do_check) check_f(nf_index, p);
#endif
ridges(nf_index, f2) = ridges[*e];
incident_facets.push_back(nf_index);
if (make_triangulation) {
nf.add_incident_simplices(triangulation.begin(), new_simplex_end);
}
} else if (nbf.orientation == 0) {
if (make_triangulation) {
nbf.add_incident_simplices(triangulation.begin(), new_simplex_end);
}
}
} else if (nbf.orientation == 0) {
ridges[*e] += p; // include the point into the edge, since it's incident to both facets
}
}
if (f_orientation < 0) dual_graph.delete_node(f);
}
if (expect_redundant) {
if (interior_points_this_step.empty()) { // = no violated facets visited
interior_points += p;
#if POLYMAKE_DEBUG
if (debug >= do_full_dump)
cout << "\ninterior points: " << interior_points
<< "\n=======================================";
if (debug >= do_dump) cout << endl;
#endif
return;
}
if (vertices_this_step.empty()) {
process_new_lineality(p, incident_facets);
return;
}
interior_points_this_step -= vertices_this_step;
interior_points += interior_points_this_step;
}
/// The final phase of the step: create new edges in the dual graph
Int min_ridge = points->cols() - AH.rows()-2;
for (auto f_it = entire(incident_facets); !f_it.at_end(); ++f_it) {
f = *f_it;
const bool vis = visited_facets.contains(f);
auto f2_it = f_it;
for (++f2_it; !f2_it.at_end(); ++f2_it) {
const Int f2 = *f2_it;
// if both facets are incident to p, they could already have a connecting edge
if (vis && visited_facets.contains(f2) && dual_graph.edge_exists(f, f2)) continue;
const Set<Int> ridge = facets[f].vertices * facets[f2].vertices;
if (ridge.size() >= min_ridge) {
bool add = true;
auto e = entire(dual_graph.out_edges(f));
while (!e.at_end()) {
const Int inc = incl(ridges[*e], ridge);
if (inc == 2) {
++e;
} else {
if (inc <= 0)
dual_graph.out_edges(f).erase(e++);
if (inc >= 0) {
add = false;
break;
}
}
}
if (add) ridges(f,f2) = ridge;
}
}
}
if (AH.rows() != 0) {
vertices_so_far += p;
if (expect_redundant) vertices_so_far -= interior_points_this_step;
}
#if POLYMAKE_DEBUG
if (debug >= do_dump) {
cout << "\n";
dump();
if (expect_redundant) cout << "\ninterior points: " << interior_points << "\n";
cout << "=======================================" << endl;
}
#endif
valid_facet = f;
}
template <typename E>
void beneath_beyond_algo<E>::process_new_lineality(const Int p, const std::list<Int>& incident_facets)
{
Set<Int> rays_in_lineality, candidate_points;
if (incident_facets.empty()) {
// all rays visited so far belong to the new lineality
if (AH.rows() == 0)
// lineality fills the entire affine hull - no reason to continue calculations
throw stop_calculation();
rays_in_lineality = vertices_so_far - interior_points;
} else if (dual_graph.nodes() > 1) {
// the intersection of all incident_facets is the new lineality,
// other vertices are candidates for restart
auto fac_it = entire(incident_facets);
candidate_points = rays_in_lineality = facets[*fac_it].vertices;
while (!(++fac_it).at_end()) {
rays_in_lineality *= facets[*fac_it].vertices;
candidate_points += facets[*fac_it].vertices;
}
candidate_points -= rays_in_lineality;
rays_in_lineality -= p;
} else {
// in the special case of two points and two facets the only candidate point belongs to the "violated" facet
candidate_points = interior_points_this_step;
rays_in_lineality = facets[incident_facets.front()].vertices;
}
add_linealities(rays_in_lineality);
interior_points_this_step -= candidate_points;
interior_points += interior_points_this_step;
interior_points += p;
interior_points += rays_in_lineality;
vertices_so_far.clear();
dual_graph.clear();
if (make_triangulation) {
triangulation.clear();
triang_size = 0;
}
state = compute_state::zero;
#if POLYMAKE_DEBUG
if (debug >= do_dump)
cout << "restart\nlinealities:\n" << linealities_so_far << endl;
#endif
for (const Int cp : candidate_points) {
process_point(cp);
}
#if POLYMAKE_DEBUG
if (debug >= do_dump)
cout << "resume with remaining points" << endl;
#endif
}
template <typename E>
void beneath_beyond_algo<E>::facet_info::coord_full_dim(const beneath_beyond_algo<E>& A)
{
normal = rows(null_space(A.points->minor(vertices, All))).front();
if (normal * A.points->row((A.vertices_so_far - vertices).front()) < 0)
normal.negate();
sqr_normal = sqr(normal);
}
template <typename E>
void beneath_beyond_algo<E>::facet_info::coord_low_dim(const beneath_beyond_algo<E>& A)
{
ListMatrix<SparseVector<E>> Fn = A.facet_nullspace;
for (const Int v : vertices)
A.reduce_nullspace(Fn, v);
normal = rows(Fn).front();
if (normal * A.points->row((A.vertices_so_far - vertices).front()) < 0)
normal.negate();
sqr_normal = sqr(normal);
}
template <typename TSet>
Int single_or_nothing(const GenericSet<TSet, Int>& s)
{
Int x = -1;
auto e = entire(s.top());
if (!e.at_end()) {
x = *e; ++e;
if (!e.at_end()) x = -1;
}
return x;
}
template <typename E> template <typename Iterator>
void beneath_beyond_algo<E>::facet_info::add_incident_simplices(Iterator s, Iterator s_end)
{
for (; s != s_end; ++s) {
Int opv = single_or_nothing(*s - vertices);
if (opv >= 0)
simplices.push_back(incident_simplex(*s, opv));
}
}
#if POLYMAKE_DEBUG
template <typename E>
void beneath_beyond_algo<E>::dump() const
{
cout << "dual_graph:\n";
const bool show_normals = debug == do_full_dump && (AH.rows() == 0 || facet_nullspace.rows() != 0);
for (auto f=entire(nodes(dual_graph)); !f.at_end(); ++f) {
cout << f.index() << ": " << facets[*f].vertices;
if (show_normals) cout << "=[ " << facets[*f].normal << " ]";
if (debug == do_full_dump) {
for (auto e = entire(f.out_edges()); !e.at_end(); ++e)
cout << " (" << e.to_node() << ' ' << ridges[*e] << ')';
if (make_triangulation) {
cout << " <<";
for (auto s = entire(facets[*f].simplices); !s.at_end(); ++s)
cout << ' ' << *s->simplex << '-' << s->opposite_vertex;
cout << " >>";
}
cout << endl;
} else {
cout << ' ' << f.adjacent_nodes() << endl;
}
}
}
template <typename E>
void beneath_beyond_algo<E>::check_fp(Int f_index, const facet_info& f, Int p, std::ostringstream& errors) const
{
const E prod = points->row(p) * f.normal;
if (f.vertices.contains(p)) {
if (prod!=0)
wrap(errors) << "facet(" << f_index << ") * incident vertex(" << p << ")=" << prod << endl;
} else {
if (prod<=0)
wrap(errors) << "facet(" << f_index << ") * non-incident vertex(" << p << ")=" << prod << endl;
}
}
// various consistency checks
template <typename E>
void beneath_beyond_algo<E>::check_p(Int p) const
{
if (AH.rows() == 0 || facet_nullspace.rows() != 0) {
std::ostringstream errors;
for (auto f=entire(nodes(dual_graph)); !f.at_end(); ++f)
check_fp(f.index(), facets[*f], p, errors);
if (!errors.str().empty())
throw std::runtime_error("beneath_beyond_algo - consistency checks failed:\n" + errors.str());
}
}
template <typename E>
void beneath_beyond_algo<E>::check_f(Int f, Int last_p) const
{
std::ostringstream errors;
const facet_info& fi=facets[f];
for (auto p = entire(range(0, last_p)); !p.at_end(); ++p)
check_fp(f, fi, *p, errors);
if (!errors.str().empty())
throw std::runtime_error("beneath_beyond_algo - consistency checks failed:\n" + errors.str());
}
template <typename E>
void beneath_beyond_algo<E>::dump_p(Int p) const
{
if (!AH.rows() || facet_nullspace.rows()) {
for (auto f=entire(nodes(dual_graph)); !f.at_end(); ++f)
if (f.degree()) {
const E prod = points->row(p) * facets[*f].normal;
cout << "facet(" << f.index() << "): prod=" << prod << ", sqr_dist=" << double(prod*prod/facets[*f].sqr_normal) << '\n';
}
}
}
#endif // POLYMAKE_DEBUG
} }
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|