Growing and Shrinking Polygons for Random Testing of Computational - - PowerPoint PPT Presentation

growing and shrinking polygons for random testing of
SMART_READER_LITE
LIVE PREVIEW

Growing and Shrinking Polygons for Random Testing of Computational - - PowerPoint PPT Presentation

Growing and Shrinking Polygons for Random Testing of Computational Geometry Algorithms Experience Report Ilya Sergey ICFP 2016 September 20th, 2016 Polygons Polygons Polygons Polygons Polygons are Trees Polygons are Trees We can


slide-1
SLIDE 1

Growing and Shrinking Polygons 
 for Random Testing 


  • f Computational Geometry Algorithms

Ilya Sergey

Experience Report

ICFP 2016 September 20th, 2016

slide-2
SLIDE 2

Polygons

slide-3
SLIDE 3

Polygons

slide-4
SLIDE 4

Polygons

slide-5
SLIDE 5

Polygons

slide-6
SLIDE 6

Polygons are Trees

slide-7
SLIDE 7

Polygons are Trees

  • We can grow them;
slide-8
SLIDE 8

Polygons are Trees

  • We can grow them;
  • We can also trim them;
slide-9
SLIDE 9

Polygons are Trees

  • We can grow them;
  • We can also trim them;
  • We can use QuickCheck to test their properties.
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

Would you like to run a scenario project this year?

slide-14
SLIDE 14

Sure, 
 why not.

Would you like to run a scenario project this year?

slide-15
SLIDE 15

Scenario Project Requirements

slide-16
SLIDE 16

Scenario Project Requirements

  • For 2nd year undergrads, team work,
slide-17
SLIDE 17

Scenario Project Requirements

  • For 2nd year undergrads, team work,
  • One week-long,
slide-18
SLIDE 18

Scenario Project Requirements

  • For 2nd year undergrads, team work,
  • One week-long,
  • Should involve math and programming,
slide-19
SLIDE 19

Scenario Project Requirements

  • For 2nd year undergrads, team work,
  • One week-long,
  • Should involve math and programming,
  • Challenging for students, but easy to assess,
slide-20
SLIDE 20

Scenario Project Requirements

  • For 2nd year undergrads, team work,
  • One week-long,
  • Should involve math and programming,
  • Challenging for students, but easy to assess,
  • To be delivered in about a month.
slide-21
SLIDE 21

Scenario Project Requirements

  • For 2nd year undergrads, team work,
  • One week-long,
  • Should involve math and programming,
  • Challenging for students, but easy to assess,
  • To be delivered in about a month.
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26

How many guards do we really need?

slide-27
SLIDE 27

How many guards do we really need?

The answer depends on the shape of the gallery.

slide-28
SLIDE 28

How many guards do we really need?

The answer depends on the shape of the gallery.

slide-29
SLIDE 29

How many guards do we really need?

The answer depends on the shape of the gallery.

slide-30
SLIDE 30

How many guards do we really need?

slide-31
SLIDE 31

How many guards do we really need?

slide-32
SLIDE 32

How many guards do we really need?

slide-33
SLIDE 33

How many guards do we really need?

slide-34
SLIDE 34

For a given gallery (polygon), 
 find the minimal set of guards’ positions, 
 so together the guards can “see” the whole interior.

Art Gallery Problem

slide-35
SLIDE 35

For a given gallery (polygon), 
 find the minimal set of guards’ positions, 
 so together the guards can “see” the whole interior.

NP-hard

Art Gallery Problem

slide-36
SLIDE 36

Project: Art Gallery Competition

Find the best solutions for a collection of large polygons.

slide-37
SLIDE 37

Project: Art Gallery Competition

Find the best solutions for a collection of large polygons.

  • 58 vertices
  • 5 guards
slide-38
SLIDE 38

Organizers’ TODO

slide-39
SLIDE 39

Organizers’ TODO

  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
slide-40
SLIDE 40

Organizers’ TODO

  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
slide-41
SLIDE 41

Organizers’ TODO

  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
  • Make sure that it all works.
slide-42
SLIDE 42

Organizers’ TODO

  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
  • Make sure that it all works.
slide-43
SLIDE 43

Organizers’ TODO

  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
  • Make sure that it all works.
slide-44
SLIDE 44
  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
  • Make sure that it all works.

Organizers’ TODO

slide-45
SLIDE 45

Growing polygons

slide-46
SLIDE 46

Growing polygons

Primitive polygons with specific “features”

slide-47
SLIDE 47

Growing polygons

Primitive polygons with specific “features”

slide-48
SLIDE 48

Seed

Growing polygons

slide-49
SLIDE 49

Seed

Growing polygons

slide-50
SLIDE 50

Growing polygons

slide-51
SLIDE 51

Growing polygons

slide-52
SLIDE 52

Growing polygons

slide-53
SLIDE 53

Growing polygons

slide-54
SLIDE 54

Growing polygons

slide-55
SLIDE 55

Growing polygons

slide-56
SLIDE 56

Growing polygons

slide-57
SLIDE 57

Growing polygons

slide-58
SLIDE 58

Growing polygons

slide-59
SLIDE 59

Growing polygons

slide-60
SLIDE 60

Growing polygons

slide-61
SLIDE 61

Algorithm for growing polygons

1. Pick a primitive polygon; 2. Locate a segment on a host polygon edge; 3. Scale, rotate and attach the primitive;
 check for self-intersections

  • 4. Repeat
slide-62
SLIDE 62

Abstract polygon generator

trait PolygonGenerator extends GeneratorPrimitives {
 
 val seeds : List[Polygon] val primitives : List[(Int) => Polygon] val locate : Double => Option[(Double, Double)] val seedFreqs : List[Int] val primFreqs : List[Int] val generations : Int ... }

slide-63
SLIDE 63

Abstract polygon generator

trait PolygonGenerator extends GeneratorPrimitives {
 
 val seeds : List[Polygon] val primitives : List[(Int) => Polygon] val locate : Double => Option[(Double, Double)] val seedFreqs : List[Int] val primFreqs : List[Int] val generations : Int ... }

slide-64
SLIDE 64

Abstract polygon generator

trait PolygonGenerator extends GeneratorPrimitives {
 
 val seeds : List[Polygon] val primitives : List[(Int) => Polygon] val locate : Double => Option[(Double, Double)] val seedFreqs : List[Int] val primFreqs : List[Int] val generations : Int ... }

slide-65
SLIDE 65

Abstract polygon generator

trait PolygonGenerator extends GeneratorPrimitives {
 
 val seeds : List[Polygon] val primitives : List[(Int) => Polygon] val locate : Double => Option[(Double, Double)] val seedFreqs : List[Int] val primFreqs : List[Int] val generations : Int ... }

slide-66
SLIDE 66

Generating contest problems

Rectilinear

slide-67
SLIDE 67

Quasi-convex

Generating contest problems

slide-68
SLIDE 68

Crazy

Generating contest problems

slide-69
SLIDE 69
  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
  • Make sure that it all works.

Organizers’ TODO

slide-70
SLIDE 70
  • Problem generator;
  • Polygons with different “features” (convex, rectangular, etc.)
  • Solution checker with online feedback
  • geometric machinery (triangulation, visibility, …)
  • web-server
  • Make sure that it all works.

Organizers’ TODO

slide-71
SLIDE 71

Testing with ScalaCheck

slide-72
SLIDE 72

Testing with ScalaCheck

  • Triangulation of a polygon of size N:
  • centre of each triangle lies within a polygon;
  • triangulation generates N − 2 (possibly degenerate) triangles;
slide-73
SLIDE 73

Testing with ScalaCheck

  • Triangulation of a polygon of size N:
  • centre of each triangle lies within a polygon;
  • triangulation generates N − 2 (possibly degenerate) triangles;
  • Joe-Simpson algorithm for visibility polygons (VPs):
  • a vertex of a VP is also within the original polygon;
  • every VP’s edge is within the original polygon;
  • a random point within a VP is indeed visible from its origin;
slide-74
SLIDE 74

Testing with ScalaCheck

  • Triangulation of a polygon of size N:
  • centre of each triangle lies within a polygon;
  • triangulation generates N − 2 (possibly degenerate) triangles;
  • Joe-Simpson algorithm for visibility polygons (VPs):
  • a vertex of a VP is also within the original polygon;
  • every VP’s edge is within the original polygon;
  • a random point within a VP is indeed visible from its origin;
  • Solution visibility checker:
  • a refutation (if it exists) is within the original polygon;
  • a refutation for a set of guards is not within any of their VPs;
slide-75
SLIDE 75

Testing with ScalaCheck

  • Triangulation of a polygon of size N:
  • centre of each triangle lies within a polygon;
  • triangulation generates N − 2 (possibly degenerate) triangles;
  • Joe-Simpson algorithm for visibility polygons (VPs):
  • a vertex of a VP is also within the original polygon;
  • every VP’s edge is within the original polygon;
  • a random point within a VP is indeed visible from its origin;
  • Solution visibility checker:
  • a refutation (if it exists) is within the original polygon;
  • a refutation for a set of guards is not within any of their VPs;
  • Basic algorithm for solving AGP by Fisk:
  • delivers a solution of size within the boundary ⌊N/3⌋;
  • the solution checker finds no refutations for its result.
slide-76
SLIDE 76

How well did that work?

slide-77
SLIDE 77

How well did that work?

  • Multiple bugs (>20) due to inaccurate treatment of

floating points;

slide-78
SLIDE 78

How well did that work?

  • Multiple bugs (>20) due to inaccurate treatment of

floating points;

  • Several bugs (~10) due to misreading of the textbook

algorithms or simplifications in their descriptions;

slide-79
SLIDE 79

How well did that work?

  • Multiple bugs (>20) due to inaccurate treatment of

floating points;

  • Several bugs (~10) due to misreading of the textbook

algorithms or simplifications in their descriptions;

  • Two bugs in the Joe-Simpson algorithm itself;
slide-80
SLIDE 80

How well did that work?

  • Multiple bugs (>20) due to inaccurate treatment of

floating points;

  • Several bugs (~10) due to misreading of the textbook

algorithms or simplifications in their descriptions;

  • Two bugs in the Joe-Simpson algorithm itself;
  • Bonus: an undocumented behavior in the 


state-of-the-art CGAL library 
 (written in C++ using exact arithmetics).

slide-81
SLIDE 81

How well did that work?

  • Multiple bugs (>20) due to inaccurate treatment of

floating points;

  • Several bugs (~10) due to misreading of the textbook

algorithms or simplifications in their descriptions;

  • Two bugs in the Joe-Simpson algorithm itself;
  • Bonus: an undocumented behavior in the 


state-of-the-art CGAL library 
 (written in C++ using exact arithmetics).

slide-82
SLIDE 82

Testing basic visibility algorithm

  • Main component for checking arbitrary solutions;
  • Original description (1986) has a number of simplifications and

is given in pseudocode.

slide-83
SLIDE 83

import RandomRectilinearPolygonGenerator._
 
 property("All visibility polygons lie within the original polygon") =
 forAll { (p : Polygon) =>
 val guards = p.vertices
 val vps = guards.map(visibilityPolygon(p, _))
 "Every edge of a visibility polygon is within ${p}" |: {
 val edges = for (vp <- vps; e <- vp.edges) yield e
 edges.forall(p.containsSegment)
 }
 }

Testing basic visibility algorithm

slide-84
SLIDE 84

import RandomRectilinearPolygonGenerator._
 
 property("All visibility polygons lie within the original polygon") =
 forAll { (p : Polygon) =>
 val guards = p.vertices
 val vps = guards.map(visibilityPolygon(p, _))
 "Every edge of a visibility polygon is within ${p}" |: {
 val edges = for (vp <- vps; e <- vp.edges) yield e
 edges.forall(p.containsSegment)
 }
 }

Testing basic visibility algorithm

slide-85
SLIDE 85

import RandomRectilinearPolygonGenerator._
 
 property("All visibility polygons lie within the original polygon") =
 forAll { (p : Polygon) =>
 val guards = p.vertices
 val vps = guards.map(visibilityPolygon(p, _))
 "Every edge of a visibility polygon is within ${p}" |: {
 val edges = for (vp <- vps; e <- vp.edges) yield e
 edges.forall(p.containsSegment)
 }
 }

Testing basic visibility algorithm

slide-86
SLIDE 86

import RandomRectilinearPolygonGenerator._
 
 property("All visibility polygons lie within the original polygon") =
 forAll { (p : Polygon) =>
 val guards = p.vertices
 val vps = guards.map(visibilityPolygon(p, _))
 "Every edge of a visibility polygon is within ${p}" |: {
 val edges = for (vp <- vps; e <- vp.edges) yield e
 edges.forall(p.containsSegment)
 }
 }

Testing basic visibility algorithm

slide-87
SLIDE 87

Bug in Joe-Simpson algorithm

Randomly generated, 260 vertices, guards in every node

slide-88
SLIDE 88

Bug in Joe-Simpson algorithm

??! Randomly generated, 260 vertices, guards in every node

slide-89
SLIDE 89

Shrinking Polygons

slide-90
SLIDE 90

Shrinking Polygons Trimming

slide-91
SLIDE 91

Reconstructing polygon generation

slide-92
SLIDE 92

Reconstructing polygon generation

1 2 3 4 5 6 7 1 2 3 4 5 6 7

slide-93
SLIDE 93

Reconstructing polygon generation

1 2 3 4 5 6 7 1 2 3 4 5 6 7

“Attachment tree”

slide-94
SLIDE 94

Trimming polygons

1 3 4 5 6 7 1 2 4 5 6 7 3 2

slide-95
SLIDE 95

1 2 3 4 5 6 7 1 2 3 4 5 6 7

Trimming polygons

slide-96
SLIDE 96

Shrinking strategy

  • Build the attachment tree while constructing a

random polygon;

  • Construct all its subtrees;
  • “Render” the corresponding trimmed polygons.
slide-97
SLIDE 97

Bug in Joe-Simpson algorithm

Randomly generated, 260 vertices, guards in every node

slide-98
SLIDE 98

Bug in Joe-Simpson algorithm

Randomly generated, 260 vertices, guards in every node

slide-99
SLIDE 99

Bug in Joe-Simpson algorithm

After trimming, 20 vertices

slide-100
SLIDE 100

Bug in Joe-Simpson algorithm

Removed 
 irrelevant guards

slide-101
SLIDE 101

Bug in Joe-Simpson algorithm

slide-102
SLIDE 102

Bug in Joe-Simpson algorithm

slide-103
SLIDE 103

... intersectWithWindow(v(i), v(i + 1), s.top, windowEnd) match {
 
 case Some(p) =>
 s.push(p)
 advance(v, s, i)
 case _ =>
 scan(v, s, i, windowEnd, ccw)
 
 } ...

Bug in Joe-Simpson algorithm

slide-104
SLIDE 104

Bug in Joe-Simpson algorithm

... intersectWithWindow(v(i), v(i + 1), s.top, windowEnd) match {
 
 case Some(p) if !(windowEnd.isDefined && p =~= windowEnd) =>
 s.push(p)
 advance(v, s, i)
 case _ =>
 scan(v, s, i, windowEnd, ccw)
 
 } ...

slide-105
SLIDE 105

Implementation effort

  • Geometric primitives and procedures: 1450 LOC
  • Server infrastructure: 1500 LOC
  • Testing framework: 350 LOC
slide-106
SLIDE 106

Running the Art Gallery Competition

slide-107
SLIDE 107

Running the Art Gallery Competition

  • 94 participants, 24 teams, 2360 submissions in five days
slide-108
SLIDE 108

Running the Art Gallery Competition

  • 94 participants, 24 teams, 2360 submissions in five days
  • Server running 24/5
  • for different teams, solution processed concurrently
  • no crashes during the week
  • one non-critical bug in server logic (fixed without restarting)
slide-109
SLIDE 109

Running the Art Gallery Competition

  • 94 participants, 24 teams, 2360 submissions in five days
  • Server running 24/5
  • for different teams, solution processed concurrently
  • no crashes during the week
  • one non-critical bug in server logic (fixed without restarting)
  • Most of the solutions: textbook algorithm with optimizations;
slide-110
SLIDE 110

Running the Art Gallery Competition

  • 94 participants, 24 teams, 2360 submissions in five days
  • Server running 24/5
  • for different teams, solution processed concurrently
  • no crashes during the week
  • one non-critical bug in server logic (fixed without restarting)
  • Most of the solutions: textbook algorithm with optimizations;
  • The winning team implemented the state-of-the art algorithm

(2014) using linear programming;

slide-111
SLIDE 111

Mon Tue Wed The Fri

slide-112
SLIDE 112

To take away

slide-113
SLIDE 113

To take away

  • FP works great for rapid development of 


non-trivial and robust geometric applications;

slide-114
SLIDE 114

To take away

  • FP works great for rapid development of 


non-trivial and robust geometric applications;

  • QuickCheck-ing CG with polygons is 


feasible and efficient in practice;

slide-115
SLIDE 115

To take away

  • FP works great for rapid development of 


non-trivial and robust geometric applications;

  • QuickCheck-ing CG with polygons is 


feasible and efficient in practice;

  • Polygons can be grown and trimmed, 


just like trees.

slide-116
SLIDE 116

To take away

Thanks!

  • FP works great for rapid development of 


non-trivial and robust geometric applications;

  • QuickCheck-ing CG with polygons is 


feasible and efficient in practice;

  • Polygons can be grown and trimmed, 


just like trees.