|
1 | 1 | #lang racket |
2 | 2 |
|
3 | | -(require racket/runtime-path) |
| 3 | +(require racket/runtime-path math/base) |
4 | 4 | (require "egraph-conversion.rkt" "../timeline.rkt" |
5 | 5 | "../syntax/types.rkt" "../points.rkt" "../common.rkt") |
6 | 6 | (require (for-syntax syntax/parse)) |
|
19 | 19 | (define egg-if-match-limit 10000) |
20 | 20 | (define HIGH-COST 100000000) |
21 | 21 | ;; Number of egraphs to run (independent samples) |
22 | | -(define egg-num-egraphs 3) |
| 22 | +(define egg-num-egraphs 1) |
23 | 23 | ;; local error threshold for search |
24 | 24 | (define ERROR-THRESHOLD 0.0) |
25 | 25 |
|
26 | 26 |
|
| 27 | +;; var-intervals is a hash from variable names to |
| 28 | +;; an interval: (list start end) |
27 | 29 | (struct econfig |
28 | | - (ctx pctx exprs egg-data local-error? num-sample num-variants)) |
| 30 | + (ctx pctx exprs egg-data local-error? num-sample num-variants var-intervals)) |
29 | 31 |
|
30 | 32 | (define (add-to-ruleset ruleset commands) |
31 | 33 | (for/list ([command commands]) |
|
1496 | 1498 | )))) |
1497 | 1499 |
|
1498 | 1500 |
|
| 1501 | +(define (in-interval? point config) |
| 1502 | + (for/and ([var (context-vars (econfig-ctx config))] |
| 1503 | + [num point]) |
| 1504 | + (define interval (hash-ref (econfig-var-intervals config) var)) |
| 1505 | + (and (<= (first interval) num) |
| 1506 | + (<= num (second interval))))) |
| 1507 | + |
1499 | 1508 | (define (setup-ground-truth config) |
1500 | | - (define points (for/list ([(point exact) (in-pcontext (econfig-pctx config))]) |
| 1509 | + (define points |
| 1510 | + (for/list ([(point exact) (in-pcontext (econfig-pctx config))] |
| 1511 | + #:when (in-interval? point config)) |
1501 | 1512 | point)) |
1502 | 1513 | (define shuffled (shuffle points)) |
1503 | 1514 | (append |
|
1511 | 1522 | [num point]) |
1512 | 1523 | `(set (ival ,(expr->egglog (econfig-ctx config) var (econfig-egg-data config)) |
1513 | 1524 | ,i) |
1514 | | - (interval ,num ,num))))))) |
| 1525 | + (interval ,(egglog-float num) ,(egglog-float num)))))))) |
1515 | 1526 |
|
1516 | 1527 |
|
1517 | 1528 | (define run-ground-truth-compute |
|
1526 | 1537 | ,(expr->egglog |
1527 | 1538 | (econfig-ctx config) var (econfig-egg-data config)) |
1528 | 1539 | -1) |
1529 | | - (ival-Empty))))) |
| 1540 | + (interval |
| 1541 | + ,(egglog-float |
| 1542 | + (first (hash-ref (econfig-var-intervals config) var))) |
| 1543 | + ,(egglog-float |
| 1544 | + (second (hash-ref (econfig-var-intervals config) var)))))))) |
1530 | 1545 |
|
1531 | 1546 | (define (build-egglog config) |
1532 | 1547 | (append |
|
1739 | 1754 | [`(mostaccurate ,args ...) #f] |
1740 | 1755 | [else #t])) |
1741 | 1756 |
|
| 1757 | +(define (egglog-float num) |
| 1758 | + (define inexact (exact->inexact num)) |
| 1759 | + (cond |
| 1760 | + [(equal? inexact +inf.0) |
| 1761 | + 'inf] |
| 1762 | + [(equal? inexact -inf.0) |
| 1763 | + '-inf] |
| 1764 | + [else |
| 1765 | + inexact])) |
| 1766 | + |
| 1767 | + |
| 1768 | +(define (random-area ctx pctx) |
| 1769 | + (define points |
| 1770 | + (for/list ([(point exact) (in-pcontext pctx)]) |
| 1771 | + point)) |
| 1772 | + |
| 1773 | + (cond |
| 1774 | + [(empty? points) |
| 1775 | + (make-hash |
| 1776 | + (for/list ([var (context-vars ctx)]) |
| 1777 | + (cons var (list -inf.0 +inf.0))))] |
| 1778 | + [else |
| 1779 | + (define shuffled (shuffle points)) |
| 1780 | + (define rand-point (first shuffled)) |
| 1781 | + (define area-size 0.5) |
| 1782 | + (make-hash |
| 1783 | + (for/list ([var (context-vars ctx)] |
| 1784 | + [num rand-point]) |
| 1785 | + (cons var (list (- num area-size) (+ num area-size)))))])) |
| 1786 | + |
| 1787 | + |
1742 | 1788 | (define (run-egglog ctx pctx exprs num-variants) |
1743 | 1789 | (define num-egraphs |
1744 | 1790 | (if (equal? num-variants 0) |
|
1758 | 1804 | (if (> i 0) |
1759 | 1805 | 0 |
1760 | 1806 | num-variants) |
| 1807 | + (random-area ctx pctx) |
1761 | 1808 | )))))) |
1762 | 1809 |
|
1763 | 1810 | (define (run-egglog-random-point config) |
1764 | 1811 | (define-values (egglog-process egglog-output egglog-in err) |
1765 | | - (subprocess #f #f (current-error-port) egglog-binary)) |
| 1812 | + (subprocess #f #f #f egglog-binary)) |
1766 | 1813 |
|
1767 | 1814 | (define egglog-program |
1768 | 1815 | (apply ~s #:separator "\n" |
|
1774 | 1821 |
|
1775 | 1822 | (displayln egglog-program egglog-in) |
1776 | 1823 | (close-output-port egglog-in) |
1777 | | - |
| 1824 | + |
1778 | 1825 | (define all-variants |
1779 | 1826 | (for/list ([expr (econfig-exprs config)]) |
1780 | 1827 | (read egglog-output))) |
|
1792 | 1839 |
|
1793 | 1840 | (close-input-port egglog-output) |
1794 | 1841 |
|
| 1842 | + ;; todo how to read all the error with a timeout? |
| 1843 | + (define err-results (read-string 1000 err)) |
| 1844 | + (close-input-port err) |
| 1845 | + (when (not (equal? err-results eof)) |
| 1846 | + (error (format "Egglog error: ~a" err-results))) |
| 1847 | + |
1795 | 1848 |
|
1796 | 1849 | (for ([result results]) |
1797 | 1850 | (when (equal? result eof) |
1798 | 1851 | (displayln egglog-program) |
1799 | | - (error "Egglog failed to produce a result"))) |
| 1852 | + (error "Egglog failed to produce a result"))) |
1800 | 1853 |
|
1801 | 1854 |
|
1802 | 1855 | (define converted |
|
0 commit comments