|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>Domain Packing</title> |
| 6 | + <link href="https://sentient-lang.github.io/prism-sentient/sentient.prism.css" rel="stylesheet" /> |
| 7 | + <style> |
| 8 | +.container { |
| 9 | +width: 60em; |
| 10 | +} |
| 11 | + </style> |
| 12 | +</head> |
| 13 | +<body> |
| 14 | +<div class="container"> |
| 15 | +<pre><code class="language-sentient"># I have a problem at work that I _think_ is the Knapsack Problem but perhaps |
| 16 | +# someone here can help me research/find a heuristic solution: we have access |
| 17 | +# to the <redacted> and get a finite number of “rules” we can use (e.g. 1000) |
| 18 | +# and each rule can only be 1024 characters long and have a maximum of 30 |
| 19 | +# operators in them (e.g. “foo OR bar OR baz”), we have a list of domains |
| 20 | +# (e.g. example.com, example.org) we want to pack domains into rules as |
| 21 | +# efficiently as possible in the minimum number of rules. |
| 22 | + |
| 23 | +# Here's an attempt at a Sentient program to solve the decision version of |
| 24 | +# this problem, i.e. Can 100 domains fit into 5 rules? |
| 25 | +# You could then ratchet the number of rules down by binary search to find |
| 26 | +# the minimum number of rules. |
| 27 | + |
| 28 | +# Some constants of the problem. |
| 29 | +maxLength = 1024; |
| 30 | +maxOrs = 30; |
| 31 | +orLength = 4; |
| 32 | + |
| 33 | +# An array containing the length of each domain. |
| 34 | +array100<int> domains; |
| 35 | + |
| 36 | +# A 2D array containing whether each domain is a member of each rule. |
| 37 | +# The number of rules can be reduced to see if the problem can be satisfied with |
| 38 | +# fewer rules. The number of members should match the number of domains. |
| 39 | +array5<array10<bool>> ruleMembers; |
| 40 | + |
| 41 | +# All domain lengths must be positive. |
| 42 | +domains.each(function (length) { |
| 43 | + invariant length.positive?; |
| 44 | +}); |
| 45 | + |
| 46 | +# Track how many domains have been allocated to rules. |
| 47 | +accountedFor = 0; |
| 48 | + |
| 49 | +# Go through the members of each rule. |
| 50 | +ruleMembers.each(function^ (members, rule) { |
| 51 | + numberOfDomains = 0; |
| 52 | + lengthOfRule = 0; |
| 53 | + |
| 54 | + # Go through all the domains. |
| 55 | + domains.each(function^ (length, index) { |
| 56 | + # Check whether the current domain is a member of the rule. |
| 57 | + isMember = members[index]; |
| 58 | + |
| 59 | + # If it is, increment the number of domains and add to the rule length. |
| 60 | + numberOfDomains += isMember ? 1 : 0; |
| 61 | + lengthOfRule += isMember ? length : 0; |
| 62 | + }); |
| 63 | + |
| 64 | + # Add the ' OR ' lengths to the rule length. |
| 65 | + numberOfOrs = numberOfDomains - 1; |
| 66 | + lengthOfRule += numberOfOrs * orLength; |
| 67 | + |
| 68 | + # The rule should not exceed 1024 characters. |
| 69 | + invariant lengthOfRule <= maxLength; |
| 70 | + |
| 71 | + # The rule should not contain more than 30 ORs. |
| 72 | + invariant numberOfOrs <= maxOrs; |
| 73 | + |
| 74 | + # These domains are now accounted for. |
| 75 | + accountedFor += numberOfDomains; |
| 76 | +}); |
| 77 | + |
| 78 | +# All domains should now be accounted for. |
| 79 | +invariant accountedFor == domains.count; |
| 80 | + |
| 81 | +# Allow the domains to be set at runtime. |
| 82 | +# Return the rule members as output. |
| 83 | +expose domains, ruleMembers; |
| 84 | +</code></pre> |
| 85 | +</div> |
| 86 | +<script src="https://sentient-lang.github.io/prism-sentient/demo/prism.js"></script> |
| 87 | +<script src="https://sentient-lang.github.io/prism-sentient/sentient.prism.js"></script> |
| 88 | +</body> |
| 89 | +</html> |
| 90 | + |
0 commit comments