-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraints.html.orig
More file actions
1445 lines (1272 loc) · 86.2 KB
/
Constraints.html.orig
File metadata and controls
1445 lines (1272 loc) · 86.2 KB
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
<!DOCTYPE html>
<html lang="en" data-content_root="./" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Shaping Inputs with Constraints — Fuzzing with Fandango</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="_static/styles/bootstrap.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link href="_static/vendor/fontawesome/6.5.2/css/all.min.css?digest=dfe6caa3a7d634c4db9b" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.5.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="_static/styles/sphinx-book-theme.css?v=eba8b062" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css?v=13237357" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css?v=76b2166b" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.8ecb98da25f57f5357bf6f572d296f466b2cfe2517ffebfabe82451661e28f02.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css?v=4fa983c6" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-design.min.css?v=95c83b7e" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=dfe6caa3a7d634c4db9b" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=dfe6caa3a7d634c4db9b" />
<script src="_static/vendor/fontawesome/6.5.2/js/all.min.js?digest=dfe6caa3a7d634c4db9b"></script>
<script src="_static/documentation_options.js?v=9eb32ce0"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/clipboard.min.js?v=a7894cd8"></script>
<script src="_static/copybutton.js?v=f281be69"></script>
<script src="_static/scripts/sphinx-book-theme.js?v=887ef09a"></script>
<script>let toggleHintShow = 'Click to show';</script>
<script>let toggleHintHide = 'Click to hide';</script>
<script>let toggleOpenOnPrint = 'true';</script>
<script src="_static/togglebutton.js?v=4a39c7ea"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script src="_static/design-tabs.js?v=f930bc37"></script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
<script async="async" src="_static/sphinx-thebe.js?v=c100c467"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown';</script>
<script>const THEBE_JS_URL = "https://unpkg.com/thebe@0.8.2/lib/index.js"; const thebe_selector = ".thebe,.cell"; const thebe_selector_input = "pre"; const thebe_selector_output = ".output, .cell_output"</script>
<script>DOCUMENTATION_OPTIONS.pagename = 'Constraints';</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="The Fandango Shell" href="Shell.html" />
<link rel="prev" title="Some Fuzzing Strategies" href="Strategies.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<input type="checkbox"
class="sidebar-toggle"
id="pst-primary-sidebar-checkbox"/>
<label class="overlay overlay-primary" for="pst-primary-sidebar-checkbox"></label>
<input type="checkbox"
class="sidebar-toggle"
id="pst-secondary-sidebar-checkbox"/>
<label class="overlay overlay-secondary" for="pst-secondary-sidebar-checkbox"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search this book..."
aria-label="Search this book..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<aside class="bd-header-announcement" aria-label="Announcement">
<div class="bd-header-announcement__content"><a href="https://pypi.org/project/fandango-fuzzer/1.1.1/" style="color:white;">Fandango 1.1.1</a> is now available! Check out the <a href="https://fandango-fuzzer.github.io/ReleaseNotes.html#" style="color:white;">release notes</a> for details.</div>
</aside>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="index.html">
<img src="_static/fandango.png" class="logo__image only-light" alt="Fuzzing with Fandango - Home"/>
<script>document.write(`<img src="_static/fandango.png" class="logo__image only-dark" alt="Fuzzing with Fandango - Home"/>`);</script>
</a></div>
<div class="sidebar-primary-item">
<script>
document.write(`
<button class="btn search-button-field search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
`);
</script></div>
<div class="sidebar-primary-item"><nav class="bd-links bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<ul class="nav bd-sidenav bd-sidenav__home-link">
<li class="toctree-l1">
<a class="reference internal" href="index.html">
Fuzzing with Fandango
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">About Fandango</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="About.html">About Fandango</a></li>
<li class="toctree-l1"><a class="reference internal" href="ReleaseNotes.html">Release Notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="FAQ.html">Fandango FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="Contributing.html">Contributing to Fandango</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">First Steps</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="Tutorial.html">Tutorial Conventions</a></li>
<li class="toctree-l1"><a class="reference internal" href="Setup.html">Installing Fandango</a></li>
<li class="toctree-l1"><a class="reference internal" href="FirstSpec.html">A First Fandango Spec</a></li>
<li class="toctree-l1"><a class="reference internal" href="Invoking.html">Invoking Fandango</a></li>
<li class="toctree-l1"><a class="reference internal" href="Fuzzing.html">Fuzzing with Fandango</a></li>
<li class="toctree-l1"><a class="reference internal" href="Strategies.html">Some Fuzzing Strategies</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Advanced Input Generation</span></p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Shaping Inputs with Constraints</a></li>
<li class="toctree-l1"><a class="reference internal" href="Shell.html">The Fandango Shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="Generators.html">Data Generators and Fakers</a></li>
<li class="toctree-l1"><a class="reference internal" href="Regexes.html">Regular Expressions</a></li>
<li class="toctree-l1"><a class="reference internal" href="Recursive.html">Complex Input Structures</a></li>
<li class="toctree-l1"><a class="reference internal" href="Paths.html">Accessing Input Elements</a></li>
<li class="toctree-l1"><a class="reference internal" href="Converters.html">Converting ANTLR and Other Input Specs</a></li>
<li class="toctree-l1"><a class="reference internal" href="Diversity.html">Covering Specs and Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="ISO8601.html">Case Study: ISO 8601 Date + Time</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Generating Binary Inputs</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="Binary.html">Generating Binary Inputs</a></li>
<li class="toctree-l1"><a class="reference internal" href="Bits.html">Bits and Bit Fields</a></li>
<li class="toctree-l1"><a class="reference internal" href="Conversion.html">Data Converters</a></li>
<li class="toctree-l1"><a class="reference internal" href="Gif.html">Case Study: The GIF Format</a></li>
<li class="toctree-l1"><a class="reference internal" href="Hatching.html">Hatching Specs</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Checking Responses</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="Parsing.html">Parsing Strings</a></li>
<li class="toctree-l1"><a class="reference internal" href="Outputs.html">Checking Outputs</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Testing Protocols</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="Protocols.html">Testing Protocols</a></li>
<li class="toctree-l1"><a class="reference internal" href="Parties.html">Customizing Party Communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="FTP.html">Case Study: FTP</a></li>
<li class="toctree-l1"><a class="reference internal" href="DNS.html">Case Study: DNS</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Fandango Reference</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="Reference.html">Fandango Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="Installing.html">Installing Fandango</a></li>
<li class="toctree-l1"><a class="reference internal" href="Commands.html">Fandango Command Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="Language.html">Fandango Language Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="Stdlib.html">Fandango Standard Library</a></li>
<li class="toctree-l1"><a class="reference internal" href="DerivationTree.html">Derivation Tree Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="PythonAPI.html">Python API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="FandangoParty.html">FandangoParty Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="Including.html">Fandango File Locations</a></li>
<li class="toctree-l1"><a class="reference internal" href="LanguageServer.html">Fandango Language Server</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</button></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<div class="dropdown dropdown-source-buttons">
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Source repositories">
<i class="fab fa-github"></i>
</button>
<ul class="dropdown-menu">
<li><a href="https://github.com/fandango-fuzzer/fandango" target="_blank"
class="btn btn-sm btn-source-repository-button dropdown-item"
title="Source repository"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fab fa-github"></i>
</span>
<span class="btn__text-container">Repository</span>
</a>
</li>
<li><a href="https://github.com/fandango-fuzzer/fandango/issues/new?title=Issue%20on%20page%20%2FConstraints.html&body=Your%20issue%20content%20here." target="_blank"
class="btn btn-sm btn-source-issues-button dropdown-item"
title="Open an issue"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-lightbulb"></i>
</span>
<span class="btn__text-container">Open issue</span>
</a>
</li>
</ul>
</div>
<div class="dropdown dropdown-download-buttons">
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
<i class="fas fa-download"></i>
</button>
<ul class="dropdown-menu">
<li><a href="_sources/Constraints.md" target="_blank"
class="btn btn-sm btn-download-source-button dropdown-item"
title="Download source file"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file"></i>
</span>
<span class="btn__text-container">.md</span>
</a>
</li>
<li>
<button onclick="window.print()"
class="btn btn-sm btn-download-pdf-button dropdown-item"
title="Print to PDF"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file-pdf"></i>
</span>
<span class="btn__text-container">.pdf</span>
</button>
</li>
</ul>
</div>
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<script>
document.write(`
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button" title="light/dark" aria-label="light/dark" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto"></i>
</button>
`);
</script>
<script>
document.write(`
<button class="btn btn-sm pst-navbar-icon search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button>
`);
</script>
<button class="sidebar-toggle secondary-toggle btn btn-sm" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</button>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>Shaping Inputs with Constraints</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-limits-of-grammars">The Limits of Grammars</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#specifying-constraints">Specifying Constraints</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#constraints-and-derivationtree-types">Constraints and <code class="docutils literal notranslate"><span class="pre">DerivationTree</span></code> types</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#constraints-on-the-command-line">Constraints on the Command Line</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#how-fandango-solves-constraints">How Fandango Solves Constraints</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#the-fandango-progress-bar">The Fandango Progress Bar</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#soft-constraints-and-optimization">Soft Constraints and Optimization</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#when-constraints-cannot-be-solved">When Constraints Cannot be Solved</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section class="tex2jax_ignore mathjax_ignore" id="shaping-inputs-with-constraints">
<span id="sec-constraints"></span><h1>Shaping Inputs with Constraints<a class="headerlink" href="#shaping-inputs-with-constraints" title="Link to this heading">#</a></h1>
<section id="the-limits-of-grammars">
<h2>The Limits of Grammars<a class="headerlink" href="#the-limits-of-grammars" title="Link to this heading">#</a></h2>
<p>So far, all the operations we have performed on our data were <em>syntax-oriented</em> - that is, we could shape the format and structure of our values, but not their <em>semantics</em> - that is, their actual meaning.
Consider our <a class="reference internal" href="Fuzzing.html#sec-fuzzing"><span class="std std-ref">Person/Age dataset from the previous section</span></a>.
What would we do if we want the “age” in a specific range?
Or we want it to be odd?
Or we want the age to be distributed in a certain way?</p>
<div class="margin docutils">
<p>All of this refers to <em>context-free grammars</em>, which are the ones Fandango uses.</p>
</div>
<p>Some of these can be obtained by altering the grammar - limiting the age to two digits at most, for instance, will keep the value below 100 - but others cannot.
Properties that cannot be expressed in a grammar are called <em>semantic properties</em> - in contrast to <em>syntactical properties</em>, which is precisely what grammars are good for.</p>
</section>
<section id="specifying-constraints">
<h2>Specifying Constraints<a class="headerlink" href="#specifying-constraints" title="Link to this heading">#</a></h2>
<p>Fandango solves this problem through a pretty unique feature: It allows users to specify <em>constraints</em> which inputs have to satisfy. These thus narrow down the set of possible inputs.</p>
<p>Constraints are <em>predicates over grammar symbols</em>.
Essentially, you write a Boolean expression, using grammar symbols (in <code class="docutils literal notranslate"><span class="pre"><...></span></code>) to refer to individual elements of the input.</p>
<p>As an example, consider this Fandango constraint:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span> <span class="o"><</span> <span class="mi">50</span>
</pre></div>
</div>
<p>This constraint takes the <code class="docutils literal notranslate"><span class="pre"><age></span></code> element from the input and converts it into an integer (all symbols are strings in the first place).
Inputs are produced only if the resulting value is less than 50.</p>
<p>We can add such constraints to any .fan file, say the <a class="reference download internal" download="" href="_downloads/eb75cca3f691010aefb22548a3d912ba/persons.fan"><span class="xref download myst"><code class="docutils literal notranslate"><span class="pre">persons.fan</span></code></span></a> file from the previous section.
Constraints are preceded by a keyword <code class="docutils literal notranslate"><span class="pre">where</span></code>.
So the line we add reads</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">where</span> <span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span> <span class="o"><</span> <span class="mi">50</span>
</pre></div>
</div>
<p>and the full <code class="docutils literal notranslate"><span class="pre">persons.fan</span></code> file reads</p>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span><start> ::= <person_name> "," <age>
<person_name> ::= <first_name> " " <last_name>
<first_name> ::= <name>
<last_name> ::= <name>
<name> ::= <ascii_uppercase_letter><ascii_lowercase_letter>+
<age> ::= <digit>+
where int(<age>) < 50
</pre></div>
</div>
</div>
</div>
<p>If we do this and run Fandango, we obtain a new set of inputs:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>-n<span class="w"> </span><span class="m">10</span>
</pre></div>
</div>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ezormdsjczfvvezpxya Ckkwqrx,7
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Vzworjfq Lmabwsdmigydyauqpfkkn,5
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Qiigk Xwx,9
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Lybvqgspyv Zsdlhpyt,8
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ychyvnvtsfjzqru Gkeldmqhywjghr,12
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ol Dasuuhhknuyqbtqps,02
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Cwdsmxxsnun Wa,3
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Cwdsmxxsnun Na,3
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ol Tasuuhhknuyqbtqps,02
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Vzworjfq Lmabwsdmigydyauqpfkkn,8
</pre></div>
</div>
</div>
</div>
<p>We see that all persons produced now indeed have an age of less than 50.
Even if an age begins with <code class="docutils literal notranslate"><span class="pre">0</span></code>, it still represents a number below 50.</p>
<p>The language Fandango uses to express constraints is Python, so you can make use of arbitrary Python expressions.
For instance, we can use Python Boolean operators (<code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code>, <code class="docutils literal notranslate"><span class="pre">not</span></code>) to request values in a range of 25-45:</p>
<div class="margin docutils">
<p>Interestingly, having symbols in <code class="docutils literal notranslate"><span class="pre"><...></span></code> does not conflict with the rest of Python syntax.
Be sure, though, to leave spaces around <code class="docutils literal notranslate"><span class="pre"><</span></code> and <code class="docutils literal notranslate"><span class="pre">></span></code> operators to avoid confusion.</p>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">25</span> <span class="o"><=</span> <span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span> <span class="o"><=</span> <span class="mi">45</span>
</pre></div>
</div>
<p>and we obtain these inputs:</p>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Uouxxxxjq Lbdz,27
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Kxkey Ijaployjymxvqrapitc,25
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Uouxxxxsq Lbdz,27
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ukuxxxxjq Lbdz,27
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Uouxxxxjq Koskcvneafibqtwm,27
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Uyqkksugixhpbk Aouyoedrtjynnky,25
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Uouxxxxjq Bpvylafemw,27
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Erszagaiztyhrwjin Bw,25
Aujnraixoricy Qahcobedunjobynyqy,25
Uohxxxxjq Lbdz,27
</pre></div>
</div>
</div>
</div>
<p>Start with <a class="reference download internal" download="" href="_downloads/eb75cca3f691010aefb22548a3d912ba/persons.fan"><span class="xref download myst"><code class="docutils literal notranslate"><span class="pre">persons.fan</span></code></span></a> and add a constraint such that we generate people whose age is a multiple of 7, as in</p>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Zakujyzsnciwoa Zxzzjjixjxgrck,42
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Qvmhiea Cinrtmnokm,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Bdxuxffpa Cainomotdhleouurcalg,365407
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Mcohvwqzmvzzzldjzwjgr Zvwkwqitoasou,92449
Hanaxsmrnxulnazykw Ryhreiviiu,77
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Fkrstupv Bjjgut,9017453297727
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ytgrvkrmlu Yevlescwefsmcagzwdgjj,7
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Deufgl Lxqjsifcbaafmmcmrswx,084793913085440608
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Pvfoe Ueihne,04256
Xwhnwunoqwueapbc Fxem,8162
</pre></div>
</div>
</div>
</div>
<p>(Hint: The modulo operator in Python is <code class="docutils literal notranslate"><span class="pre">%</span></code>).</p>
<div class="tip dropdown admonition">
<p class="admonition-title">Solution</p>
<p>This is not too hard. Simply add</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">where</span> <span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span> <span class="o">%</span> <span class="mi">7</span> <span class="o">==</span> <span class="mi">0</span>
</pre></div>
</div>
<p>as a constraint.</p>
</div>
</section>
<section id="constraints-and-derivationtree-types">
<span id="sec-derivationtree"></span><h2>Constraints and <code class="docutils literal notranslate"><span class="pre">DerivationTree</span></code> types<a class="headerlink" href="#constraints-and-derivationtree-types" title="Link to this heading">#</a></h2>
<p>Whenever Fandango evaluates a constraint, such as</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span> <span class="o">></span> <span class="mi">20</span>
</pre></div>
</div>
<p>the type of <code class="docutils literal notranslate"><span class="pre"><age></span></code> is actually not a string, but a <code class="docutils literal notranslate"><span class="pre">DerivationTree</span></code> object - <a class="reference internal" href="Paths.html#sec-paths"><span class="std std-ref">a tree representing the structure of the output.</span></a>. You can use <code class="docutils literal notranslate"><span class="pre">DerivationTree</span></code> objects as other basic python data types by converting them using (<code class="docutils literal notranslate"><span class="pre">int(<age>)</span></code>, <code class="docutils literal notranslate"><span class="pre">str(<age>)</span></code>, <code class="docutils literal notranslate"><span class="pre">bytes(<age>)</span></code>).</p>
<ul class="simple">
<li><p>You can then invoke most <em>string, bytes and int methods</em> on them (<code class="docutils literal notranslate"><span class="pre"><age>.startswith('0')</span></code>) (see <a class="reference internal" href="DerivationTree.html#sec-derivation-tree-direct-access-functions"><span class="std std-ref">Details</span></a>)</p></li>
<li><p>You can <em>compare</em> them against each other (<code class="docutils literal notranslate"><span class="pre"><age_1></span> <span class="pre">==</span> <span class="pre"><age_2></span></code>) as well as against other strings (<code class="docutils literal notranslate"><span class="pre"><age></span> <span class="pre">!=</span> <span class="pre">"19"</span></code>)</p></li>
</ul>
<p>One thing you <em>cannot</em> do, though, is <em>passing them directly as arguments to functions</em> that do not expect a <code class="docutils literal notranslate"><span class="pre">DerivationTree</span></code> type. This applies to the vast majority of Python functions.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>If you want to pass a symbol as a function argument, convert it to the proper type (<code class="docutils literal notranslate"><span class="pre">int(<age>)</span></code>, <code class="docutils literal notranslate"><span class="pre">float(<age>)</span></code>, <code class="docutils literal notranslate"><span class="pre">str(<age>)</span></code>) first.
Otherwise, you will likely raise an internal error in that very function.</p>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>On symbols, the <code class="docutils literal notranslate"><span class="pre">[...]</span></code> operator operates differently from strings - it returns a <em>subtree</em> of the produced output: <code class="docutils literal notranslate"><span class="pre"><name>[0]</span></code> returns the <code class="docutils literal notranslate"><span class="pre"><first_name></span></code> element, not the first character.
If you want to access a character (or a range of characters) of a symbol, convert it into a string first, as in <code class="docutils literal notranslate"><span class="pre">str(<name>)[0]</span></code>.</p>
</div>
<p>We will learn more about derivation trees, <code class="docutils literal notranslate"><span class="pre">DerivationTree</span></code> types, and their operators in <a class="reference internal" href="Paths.html#sec-paths"><span class="std std-ref">Accessing Input Elements</span></a>.</p>
</section>
<section id="constraints-on-the-command-line">
<h2>Constraints on the Command Line<a class="headerlink" href="#constraints-on-the-command-line" title="Link to this heading">#</a></h2>
<p>If you want to experiment with constraints, keeping on editing <code class="docutils literal notranslate"><span class="pre">.fan</span></code> files is a bit cumbersome.
As an alternative, Fandango also allows to specify constraints on the command line.
This is done with the <code class="docutils literal notranslate"><span class="pre">-c</span></code> (constraint) option, followed by the constraint expression (typically in quotes).</p>
<p>Starting with the original <a class="reference download internal" download="" href="_downloads/eb75cca3f691010aefb22548a3d912ba/persons.fan"><span class="xref download myst"><code class="docutils literal notranslate"><span class="pre">persons.fan</span></code></span></a>, we can thus apply age constraints as follows:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>-n<span class="w"> </span><span class="m">10</span><span class="w"> </span>-c<span class="w"> </span><span class="s1">'25 <= int(<age>) and int(<age>) <= 45'</span>
</pre></div>
</div>
<p>Constraints can be given multiple times, so the above can also be obtained as</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>-n<span class="w"> </span><span class="m">10</span><span class="w"> </span>-c<span class="w"> </span><span class="s1">'25 <= int(<age>)'</span><span class="w"> </span>-c<span class="w"> </span><span class="s1">'int(<age>) <= 45'</span>
</pre></div>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>On the command line, always put constraints in single quotes (<code class="docutils literal notranslate"><span class="pre">'...'</span></code>), as the angle brackets might otherwise be interpreted as I/O redirection.</p>
</div>
<p>When do constraints belong in a <code class="docutils literal notranslate"><span class="pre">.fan</span></code> file, and when on the command line?
As a rule of thumb:</p>
<ul class="simple">
<li><p>If a constraint is <em>necessary</em> for obtaining valid input files (i.e. if the inputs would not be accepted otherwise), it belongs into the <code class="docutils literal notranslate"><span class="pre">.fan</span></code> file.</p></li>
<li><p>If a constraint is <em>optional</em>, for instance for shaping inputs towards a particular goal, then it can also go on the command line.</p></li>
</ul>
</section>
<section id="how-fandango-solves-constraints">
<h2>How Fandango Solves Constraints<a class="headerlink" href="#how-fandango-solves-constraints" title="Link to this heading">#</a></h2>
<p>How does Fandango obtain these inputs?
In a nutshell, Fandango is an <em>evolutionary</em> test generator:</p>
<ol class="arabic simple">
<li><p>It first uses the grammar to generate a <em>population</em> of inputs.</p></li>
<li><p>It then checks which individual inputs are <em>closest</em> in fulfilling the given constraints.
For instance, for a constraint <code class="docutils literal notranslate"><span class="pre">int(<X>)</span> <span class="pre">==</span> <span class="pre">100</span></code>, an input where <code class="docutils literal notranslate"><span class="pre"><X></span></code> has a value of 90 is closer to fulfillment than one with value of, say 20.</p></li>
</ol>
<div class="margin docutils">
<p>Selecting the best inputs is also known as “survival of the fittest”</p>
</div>
<ol class="arabic simple" start="3">
<li><p>The best inputs are selected, the others are discarded.</p></li>
<li><p>Fandango then generates new <em>offspring</em> by <em>mutating</em> the remaining inputs, recomputing parts according to grammar rules.
It can also exchange parts with those from other inputs; this is called <em>crossover</em>.</p></li>
<li><p>Fandango then repeats Steps 2-4 until all inputs satisfy the constraints.</p></li>
</ol>
<p>All of this happens within Fandango, which runs through these steps with high speed.
The <code class="docutils literal notranslate"><span class="pre">-v</span></code> option (verbose) produces some info on how the algorithm progresses:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>-v<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>-n<span class="w"> </span><span class="m">10</span><span class="w"> </span>-c<span class="w"> </span><span class="s1">'int(<age>) % 7 == 0'</span>
</pre></div>
</div>
<div class="cell tag_remove-input tag_scroll-output docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: ---------- Parsing FANDANGO content ----------
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: File mode: text
fandango:INFO: ---------- Initializing base population ----------
fandango:INFO: ---------- Initializing FANDANGO algorithm ----------
fandango:INFO: ---------- Done initializing base population ----------
fandango:INFO: ---------- Generating for 500 generations----------
fandango:INFO: Generating (additional) initial population (size: 100)...
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Fojzwskndwcb Tkzr,370224726045297221
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Hltqppezcutiuoylrknvd Gfob,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Rdi Gtti,07836766295239896
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Rhsjhxqdoaxdlm Dgjarjimtexihveoh,52178187242498935142
Xpywacj Mpy,452445137200
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Dpyledqiqbjyotdkzv Nqae,8295611253143753
Mvnvoquenj Eckehzeosxiyua,3402434880432882
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Yprthrbrd Hqefnr,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Megrmzign Mmuprlea,0
Hrfderequgtzlvnbstog Sqdhrapjzunlklkxqq,91
</pre></div>
</div>
</div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <code class="docutils literal notranslate"><span class="pre">-v</span></code> option comes right after <code class="docutils literal notranslate"><span class="pre">fandango</span></code> (and not after <code class="docutils literal notranslate"><span class="pre">fandango</span> <span class="pre">fuzz</span></code>), as <code class="docutils literal notranslate"><span class="pre">-v</span></code> affects all commands (and not just <code class="docutils literal notranslate"><span class="pre">fuzz</span></code>).</p>
</div>
</section>
<section id="the-fandango-progress-bar">
<span id="sec-progress-bar"></span><h2>The Fandango Progress Bar<a class="headerlink" href="#the-fandango-progress-bar" title="Link to this heading">#</a></h2>
<p>While Fandango is solving constraints, you may see a <em>progress bar</em> in the terminal.
The progress bar looks like this:</p>
<p><img alt="progress-bar" src="_images/progress-bar.png" /></p>
<p>The progress bar is composed of three parts.
On the leftmost side, we have the <em>Fandango logo</em> (“💃 Fandango”), followed by a <em>generation counter</em> (“6/500”) showing how often (6) the population has evolved (out of a maximum 500).</p>
<p>Most of the line, however, is filled by a <em>fitness visualization</em> illustrating how the fitness is distributed across the inputs in the population.
Each fraction of the line corresponds to an equal fraction of individual inputs.
Hence, a 1/70 of the line (typically one character) stands for 1/70 of the population.</p>
<p>The <em>color</em> of each fraction how <em>fit</em> the inputs in the fraction are - on a scale from <em>bright green</em> (perfect fitness, fulfilling the given constraints) to <em>dark red</em> (very little fitness, far away from fulfilling the constraints).
Depending on its capabilities, your terminal may also show shades between these colors.
Inputs that do not satisfy the constraints at all (zero fitness) are shown in gray.</p>
<p>In the above example, we can see that Fandango already has produces a few inputs that satisfy the constraints; a few more are close and may get there through further evolution.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>By default, the progress bar only shows up if</p>
<ul class="simple">
<li><p>Fandango’s standard error is a terminal;</p></li>
<li><p>Fandango is not run within Jupyter notebook (Jupyter cannot interpret the terminal escape sequences); and</p></li>
<li><p>Fandango logging is turned off (it also writes to standard error).</p></li>
</ul>
<p>The option <code class="docutils literal notranslate"><span class="pre">--progress-bar=on</span></code> turns on the progress bar even if the above conditions are not met.
The option <code class="docutils literal notranslate"><span class="pre">--progress-bar=off</span></code> turns the progress bar off.</p>
</div>
</section>
<section id="soft-constraints-and-optimization">
<span id="sec-soft-constraints"></span><h2>Soft Constraints and Optimization<a class="headerlink" href="#soft-constraints-and-optimization" title="Link to this heading">#</a></h2>
<p>So far, we have seen constraints that <em>have</em> to be satisfied for Fandango to produce a string.
On top, Fandango also supports so-called “soft” constraints that Fandango <em>aims</em> to satisfy as good as it can.
These “soft” constraints come in two forms:</p>
<ul class="simple">
<li><p><strong>maximizing</strong> constraints: These constraints specify an <em>expression</em> whose value should be as <em>high</em> as possible</p></li>
<li><p><strong>minimizing</strong> constraints: These constraints specify an expression whose value should be as <em>low</em> as possible.</p></li>
</ul>
<p>Such soft constraints are specified</p>
<ul class="simple">
<li><p>on the command line, using <code class="docutils literal notranslate"><span class="pre">--maximizing</span> <span class="pre">EXPR</span></code> and <code class="docutils literal notranslate"><span class="pre">--minimizing</span> <span class="pre">EXPR</span></code>, respectively; or</p></li>
<li><p>in the <code class="docutils literal notranslate"><span class="pre">.fan</span></code> file, introducing them with <code class="docutils literal notranslate"><span class="pre">minimizing</span></code> and <code class="docutils literal notranslate"><span class="pre">maximizing</span></code> (instead of <code class="docutils literal notranslate"><span class="pre">where</span></code>), respectively.</p></li>
</ul>
<p>If, for instance, you want Fandango to maximize the <code class="docutils literal notranslate"><span class="pre"><age></span></code> field, you write</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>--maximize<span class="w"> </span><span class="s1">'int(<age>)'</span>
</pre></div>
</div>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tobdzbkykecgdqkxq Ivwtezejjlsxn,4962553207
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Fchqxvpblbbixbzq Cvyvumysfqbifbyt,4295202210393341
Evhrdycrazocdnla Uhiyhonfqbvdtkszl,6076380178707318660
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiq Nwnbusvu,83674923649840657994
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiv Nwnbusvu,83674923649840657994
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiq Gwnbusvu,83674923649840657994
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiq Zfgmse,83674923649840657994
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiq Zfgmse,88674923649840657994
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiq Gwnbusvu,88674923649840657994
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Tyiq Tfgmse,88674923649840657994
</pre></div>
</div>
</div>
</div>
<p>Conversely, minimizing the <code class="docutils literal notranslate"><span class="pre"><age></span></code> field yields</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>--minimize<span class="w"> </span><span class="s1">'int(<age>)'</span>
</pre></div>
</div>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Kosfug Uymrodvxqojhcgf,871144
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Dbzkic Hijrqqwrhyhc,3144
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Rjkyivxqvipglz Dapkrwmxppfldaqb,6
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Kuqovkjualjkpjtbn Ip,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Dtaddxuxotqxuaf Ip,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Dyjsiurrw Hclfbivohixudiogw,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Kuqovkjvaljkpjtbn Ip,0
Tbooceyjbxkvzuicxhb Ip,0
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Kuqovkjualjkpjtbn Kp,0
Kuqovkjualjkpjnbn Ip,0
</pre></div>
</div>
</div>
</div>
<p>Alternatively, you could also add to the <code class="docutils literal notranslate"><span class="pre">.fan</span></code> file:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">maximizing</span> <span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span>
</pre></div>
</div>
<p>or</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">minimizing</span> <span class="nb">int</span><span class="p">(</span><span class="o"><</span><span class="n">age</span><span class="o">></span><span class="p">)</span>
</pre></div>
</div>
<p>respectively.</p>
<p>To express optional goals (i.e., real “soft” constraints), simply use a <em>Boolean</em> expressions as the expressions for <code class="docutils literal notranslate"><span class="pre">--maximize</span></code> or <code class="docutils literal notranslate"><span class="pre">--minimize</span></code>.
Then, Fandango will aim to maximize (or minimize) its value.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Remember that in Python <code class="docutils literal notranslate"><span class="pre">True</span></code> is equivalent to 1, and <code class="docutils literal notranslate"><span class="pre">False</span></code> is equivalent to 0; therefore, “maximizing” a Boolean value means that Fandango will aim to solve it.</p>
</div>
<p>Here is an example of a “soft” Boolean constraints, aiming to obtain names that start with “F”:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>--maximize<span class="w"> </span><span class="s1">'<name>.startswith("A")'</span><span class="w"> </span>-n<span class="w"> </span><span class="m">10</span>
</pre></div>
</div>
<div class="cell tag_remove-input docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Bvfvmgojnodflg Nkefkfkhzcma,7631086920220845136
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Sxybv Sx,144859578016432105
Aofdozziko Zefdcdr,235047732
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Oydxwljiqc Agsgiutllmawzqtsyx,3969248703138588
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Afbvxxsigtvehqqgumqdi Bcftdsm,6117803428933933
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Azxipspqohdrutzqcq Cbjf,5276595442
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Aqatpebxanvx Llp,17
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ycgklbdhurnrcymzg Autuoxpx,747636
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Mfdbbzzxojcnupv Antp,60
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Ad Ujlqvizdd,42913
</pre></div>
</div>
</div>
</div>
<p>As you see, “soft” constraints are truly optional :-)</p>
</section>
<section id="when-constraints-cannot-be-solved">
<h2>When Constraints Cannot be Solved<a class="headerlink" href="#when-constraints-cannot-be-solved" title="Link to this heading">#</a></h2>
<p>Normally, Fandango continues evolving the population until all inputs satisfy the constraints.
Some constraints, however, may be difficult or even impossible to solve.
After a maximum number of generations (which can be set using <code class="docutils literal notranslate"><span class="pre">-N</span></code>), Fandango stops and produces the inputs it has generated so far.
We can see this if we specify <code class="docutils literal notranslate"><span class="pre">False</span></code> as a constraint:</p>
<aside class="margin sidebar">
<p class="sidebar-title"></p>
<p>The <code class="docutils literal notranslate"><span class="pre">-N</span></code> option limits the number of generations - the default is 500.</p>
</aside>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>fandango<span class="w"> </span>-v<span class="w"> </span>fuzz<span class="w"> </span>-f<span class="w"> </span>persons.fan<span class="w"> </span>-n<span class="w"> </span><span class="m">10</span><span class="w"> </span>-c<span class="w"> </span><span class="s1">'False'</span><span class="w"> </span>-N<span class="w"> </span><span class="m">50</span>
</pre></div>
</div>
<div class="cell tag_remove-input tag_scroll-output docutils container">
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: ---------- Parsing FANDANGO content ----------
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: File mode: text
fandango:INFO: ---------- Initializing base population ----------
fandango:INFO: ---------- Initializing FANDANGO algorithm ----------
fandango:INFO: ---------- Done initializing base population ----------
fandango:INFO: ---------- Generating for 50 generations----------
fandango:INFO: Generating (additional) initial population (size: 100)...
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Initial population generated in 1.67 seconds
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 1 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 1: Increasing mutation rate from 0.200 to 0.220
fandango:INFO: Generation 1: Decreasing crossover rate from 0.800 to 0.760
fandango:INFO: Generation 1: Increasing MAX_REPETITION from 20 to 30
fandango:INFO: Generation 1 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 2 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 2: Increasing mutation rate from 0.220 to 0.242
fandango:INFO: Generation 2: Decreasing crossover rate from 0.760 to 0.722
fandango:INFO: Generation 2: Increasing MAX_REPETITION from 30 to 45
fandango:INFO: Generation 2 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 3 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 3: Increasing mutation rate from 0.242 to 0.266
fandango:INFO: Generation 3: Decreasing crossover rate from 0.722 to 0.686
fandango:INFO: Generation 3: Increasing MAX_REPETITION from 45 to 68
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 3 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 4 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 4: Increasing mutation rate from 0.266 to 0.293
fandango:INFO: Generation 4: Decreasing crossover rate from 0.686 to 0.652
fandango:INFO: Generation 4: Increasing MAX_REPETITION from 68 to 102
fandango:INFO: Generation 4 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 5 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 5: Increasing mutation rate from 0.293 to 0.322
fandango:INFO: Generation 5: Decreasing crossover rate from 0.652 to 0.619
fandango:INFO: Generation 5: Increasing MAX_REPETITION from 102 to 153
fandango:INFO: Generation 5 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 6 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 6: Increasing mutation rate from 0.322 to 0.354
fandango:INFO: Generation 6: Decreasing crossover rate from 0.619 to 0.588
fandango:INFO: Generation 6: Increasing MAX_REPETITION from 153 to 230
fandango:INFO: Generation 6 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 7 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 7: Increasing mutation rate from 0.354 to 0.390
fandango:INFO: Generation 7: Decreasing crossover rate from 0.588 to 0.559
fandango:INFO: Generation 7: Increasing MAX_REPETITION from 230 to 345
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 7 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 8 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 8: Increasing mutation rate from 0.390 to 0.429
fandango:INFO: Generation 8: Decreasing crossover rate from 0.559 to 0.531
fandango:INFO: Generation 8: Increasing MAX_REPETITION from 345 to 518
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 8 stats -- Best fitness: 0.01, Avg fitness: 0.01, Avg diversity: 0.01, Population size: 100
fandango:INFO: Generation 9 - Average Fitness: 0.01
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>fandango:INFO: Generation 9: Increasing mutation rate from 0.429 to 0.472
fandango:INFO: Generation 9: Decreasing crossover rate from 0.531 to 0.504
fandango:INFO: Generation 9: Increasing MAX_REPETITION from 518 to 777