compiled-knowledge 4.0.0a20__cp313-cp313-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of compiled-knowledge might be problematic. Click here for more details.

Files changed (178) hide show
  1. ck/__init__.py +0 -0
  2. ck/circuit/__init__.py +17 -0
  3. ck/circuit/_circuit_cy.c +37523 -0
  4. ck/circuit/_circuit_cy.cp313-win32.pyd +0 -0
  5. ck/circuit/_circuit_cy.pxd +32 -0
  6. ck/circuit/_circuit_cy.pyx +768 -0
  7. ck/circuit/_circuit_py.py +836 -0
  8. ck/circuit/tmp_const.py +74 -0
  9. ck/circuit_compiler/__init__.py +2 -0
  10. ck/circuit_compiler/circuit_compiler.py +26 -0
  11. ck/circuit_compiler/cython_vm_compiler/__init__.py +1 -0
  12. ck/circuit_compiler/cython_vm_compiler/_compiler.c +19824 -0
  13. ck/circuit_compiler/cython_vm_compiler/_compiler.cp313-win32.pyd +0 -0
  14. ck/circuit_compiler/cython_vm_compiler/_compiler.pyx +380 -0
  15. ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py +121 -0
  16. ck/circuit_compiler/interpret_compiler.py +223 -0
  17. ck/circuit_compiler/llvm_compiler.py +388 -0
  18. ck/circuit_compiler/llvm_vm_compiler.py +546 -0
  19. ck/circuit_compiler/named_circuit_compilers.py +57 -0
  20. ck/circuit_compiler/support/__init__.py +0 -0
  21. ck/circuit_compiler/support/circuit_analyser/__init__.py +13 -0
  22. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c +10618 -0
  23. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cp313-win32.pyd +0 -0
  24. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.pyx +98 -0
  25. ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_py.py +93 -0
  26. ck/circuit_compiler/support/input_vars.py +148 -0
  27. ck/circuit_compiler/support/llvm_ir_function.py +234 -0
  28. ck/example/__init__.py +53 -0
  29. ck/example/alarm.py +366 -0
  30. ck/example/asia.py +28 -0
  31. ck/example/binary_clique.py +32 -0
  32. ck/example/bow_tie.py +33 -0
  33. ck/example/cancer.py +37 -0
  34. ck/example/chain.py +38 -0
  35. ck/example/child.py +199 -0
  36. ck/example/clique.py +33 -0
  37. ck/example/cnf_pgm.py +39 -0
  38. ck/example/diamond_square.py +68 -0
  39. ck/example/earthquake.py +36 -0
  40. ck/example/empty.py +10 -0
  41. ck/example/hailfinder.py +539 -0
  42. ck/example/hepar2.py +628 -0
  43. ck/example/insurance.py +504 -0
  44. ck/example/loop.py +40 -0
  45. ck/example/mildew.py +38161 -0
  46. ck/example/munin.py +22982 -0
  47. ck/example/pathfinder.py +53747 -0
  48. ck/example/rain.py +39 -0
  49. ck/example/rectangle.py +161 -0
  50. ck/example/run.py +30 -0
  51. ck/example/sachs.py +129 -0
  52. ck/example/sprinkler.py +30 -0
  53. ck/example/star.py +44 -0
  54. ck/example/stress.py +64 -0
  55. ck/example/student.py +43 -0
  56. ck/example/survey.py +46 -0
  57. ck/example/triangle_square.py +54 -0
  58. ck/example/truss.py +49 -0
  59. ck/in_out/__init__.py +3 -0
  60. ck/in_out/parse_ace_lmap.py +216 -0
  61. ck/in_out/parse_ace_nnf.py +322 -0
  62. ck/in_out/parse_net.py +480 -0
  63. ck/in_out/parser_utils.py +185 -0
  64. ck/in_out/pgm_pickle.py +42 -0
  65. ck/in_out/pgm_python.py +268 -0
  66. ck/in_out/render_bugs.py +111 -0
  67. ck/in_out/render_net.py +177 -0
  68. ck/in_out/render_pomegranate.py +184 -0
  69. ck/pgm.py +3475 -0
  70. ck/pgm_circuit/__init__.py +1 -0
  71. ck/pgm_circuit/marginals_program.py +352 -0
  72. ck/pgm_circuit/mpe_program.py +237 -0
  73. ck/pgm_circuit/pgm_circuit.py +79 -0
  74. ck/pgm_circuit/program_with_slotmap.py +236 -0
  75. ck/pgm_circuit/slot_map.py +35 -0
  76. ck/pgm_circuit/support/__init__.py +0 -0
  77. ck/pgm_circuit/support/compile_circuit.py +83 -0
  78. ck/pgm_circuit/target_marginals_program.py +103 -0
  79. ck/pgm_circuit/wmc_program.py +323 -0
  80. ck/pgm_compiler/__init__.py +2 -0
  81. ck/pgm_compiler/ace/__init__.py +1 -0
  82. ck/pgm_compiler/ace/ace.py +299 -0
  83. ck/pgm_compiler/factor_elimination.py +395 -0
  84. ck/pgm_compiler/named_pgm_compilers.py +63 -0
  85. ck/pgm_compiler/pgm_compiler.py +19 -0
  86. ck/pgm_compiler/recursive_conditioning.py +231 -0
  87. ck/pgm_compiler/support/__init__.py +0 -0
  88. ck/pgm_compiler/support/circuit_table/__init__.py +17 -0
  89. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c +16396 -0
  90. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cp313-win32.pyd +0 -0
  91. ck/pgm_compiler/support/circuit_table/_circuit_table_cy.pyx +332 -0
  92. ck/pgm_compiler/support/circuit_table/_circuit_table_py.py +304 -0
  93. ck/pgm_compiler/support/clusters.py +568 -0
  94. ck/pgm_compiler/support/factor_tables.py +406 -0
  95. ck/pgm_compiler/support/join_tree.py +332 -0
  96. ck/pgm_compiler/support/named_compiler_maker.py +43 -0
  97. ck/pgm_compiler/variable_elimination.py +91 -0
  98. ck/probability/__init__.py +0 -0
  99. ck/probability/empirical_probability_space.py +50 -0
  100. ck/probability/pgm_probability_space.py +32 -0
  101. ck/probability/probability_space.py +622 -0
  102. ck/program/__init__.py +3 -0
  103. ck/program/program.py +137 -0
  104. ck/program/program_buffer.py +180 -0
  105. ck/program/raw_program.py +67 -0
  106. ck/sampling/__init__.py +0 -0
  107. ck/sampling/forward_sampler.py +211 -0
  108. ck/sampling/marginals_direct_sampler.py +113 -0
  109. ck/sampling/sampler.py +62 -0
  110. ck/sampling/sampler_support.py +232 -0
  111. ck/sampling/uniform_sampler.py +72 -0
  112. ck/sampling/wmc_direct_sampler.py +171 -0
  113. ck/sampling/wmc_gibbs_sampler.py +153 -0
  114. ck/sampling/wmc_metropolis_sampler.py +165 -0
  115. ck/sampling/wmc_rejection_sampler.py +115 -0
  116. ck/utils/__init__.py +0 -0
  117. ck/utils/iter_extras.py +163 -0
  118. ck/utils/local_config.py +270 -0
  119. ck/utils/map_list.py +128 -0
  120. ck/utils/map_set.py +128 -0
  121. ck/utils/np_extras.py +51 -0
  122. ck/utils/random_extras.py +64 -0
  123. ck/utils/tmp_dir.py +94 -0
  124. ck_demos/__init__.py +0 -0
  125. ck_demos/ace/__init__.py +0 -0
  126. ck_demos/ace/copy_ace_to_ck.py +15 -0
  127. ck_demos/ace/demo_ace.py +49 -0
  128. ck_demos/all_demos.py +88 -0
  129. ck_demos/circuit/__init__.py +0 -0
  130. ck_demos/circuit/demo_circuit_dump.py +22 -0
  131. ck_demos/circuit/demo_derivatives.py +43 -0
  132. ck_demos/circuit_compiler/__init__.py +0 -0
  133. ck_demos/circuit_compiler/compare_circuit_compilers.py +32 -0
  134. ck_demos/circuit_compiler/show_llvm_program.py +26 -0
  135. ck_demos/pgm/__init__.py +0 -0
  136. ck_demos/pgm/demo_pgm_dump.py +18 -0
  137. ck_demos/pgm/demo_pgm_dump_stress.py +18 -0
  138. ck_demos/pgm/demo_pgm_string_rendering.py +15 -0
  139. ck_demos/pgm/show_examples.py +25 -0
  140. ck_demos/pgm_compiler/__init__.py +0 -0
  141. ck_demos/pgm_compiler/compare_pgm_compilers.py +63 -0
  142. ck_demos/pgm_compiler/demo_compiler_dump.py +60 -0
  143. ck_demos/pgm_compiler/demo_factor_elimination.py +47 -0
  144. ck_demos/pgm_compiler/demo_join_tree.py +25 -0
  145. ck_demos/pgm_compiler/demo_marginals_program.py +53 -0
  146. ck_demos/pgm_compiler/demo_mpe_program.py +55 -0
  147. ck_demos/pgm_compiler/demo_pgm_compiler.py +38 -0
  148. ck_demos/pgm_compiler/demo_recursive_conditioning.py +33 -0
  149. ck_demos/pgm_compiler/demo_variable_elimination.py +33 -0
  150. ck_demos/pgm_compiler/demo_wmc_program.py +29 -0
  151. ck_demos/pgm_compiler/time_fe_compiler.py +93 -0
  152. ck_demos/pgm_inference/__init__.py +0 -0
  153. ck_demos/pgm_inference/demo_inferencing_basic.py +188 -0
  154. ck_demos/pgm_inference/demo_inferencing_mpe_cancer.py +45 -0
  155. ck_demos/pgm_inference/demo_inferencing_wmc_and_mpe_sprinkler.py +154 -0
  156. ck_demos/pgm_inference/demo_inferencing_wmc_student.py +110 -0
  157. ck_demos/programs/__init__.py +0 -0
  158. ck_demos/programs/demo_program_buffer.py +24 -0
  159. ck_demos/programs/demo_program_multi.py +24 -0
  160. ck_demos/programs/demo_program_none.py +19 -0
  161. ck_demos/programs/demo_program_single.py +23 -0
  162. ck_demos/programs/demo_raw_program_interpreted.py +21 -0
  163. ck_demos/programs/demo_raw_program_llvm.py +21 -0
  164. ck_demos/sampling/__init__.py +0 -0
  165. ck_demos/sampling/check_sampler.py +71 -0
  166. ck_demos/sampling/demo_marginal_direct_sampler.py +40 -0
  167. ck_demos/sampling/demo_uniform_sampler.py +38 -0
  168. ck_demos/sampling/demo_wmc_direct_sampler.py +40 -0
  169. ck_demos/utils/__init__.py +0 -0
  170. ck_demos/utils/compare.py +120 -0
  171. ck_demos/utils/convert_network.py +45 -0
  172. ck_demos/utils/sample_model.py +216 -0
  173. ck_demos/utils/stop_watch.py +384 -0
  174. compiled_knowledge-4.0.0a20.dist-info/METADATA +50 -0
  175. compiled_knowledge-4.0.0a20.dist-info/RECORD +178 -0
  176. compiled_knowledge-4.0.0a20.dist-info/WHEEL +5 -0
  177. compiled_knowledge-4.0.0a20.dist-info/licenses/LICENSE.txt +21 -0
  178. compiled_knowledge-4.0.0a20.dist-info/top_level.txt +2 -0
ck/utils/tmp_dir.py ADDED
@@ -0,0 +1,94 @@
1
+ """
2
+ This is a module for creating a temporary directory and making it the working directory.
3
+
4
+ Usage:
5
+ from tmp_dir import tmp_dir
6
+
7
+ with tmp_dir():
8
+ # do some stuff
9
+ """
10
+ import os as _os
11
+ import shutil as _shutil
12
+ import tempfile as _tempfile
13
+ from pathlib import Path
14
+ from typing import Union
15
+
16
+
17
+ class tmp_dir:
18
+ __slots__ = ('_new_dir', '_old_cwd', '_chdir', '_delete_when_done')
19
+
20
+ def __init__(
21
+ self,
22
+ name: Union[Path, str, None] = None,
23
+ chdir: bool = True,
24
+ delete_when_done: bool = True
25
+ ):
26
+ """
27
+ Create a temporary directory, make it the working
28
+ directory (unless chdir is False), clean up when done
29
+ (unless delete_when_done is False).
30
+
31
+ Args:
32
+ name: the name of the temporary directory. If name
33
+ is None, then `tempfile.mkdtemp()` is used.
34
+ chdir: if True, then chdir to the temporary directory.
35
+ delete_when_done: if True, then the temporary directory is
36
+ deleted when done.
37
+ """
38
+
39
+ if name is None:
40
+ self._new_dir = Path(_tempfile.mkdtemp())
41
+ else:
42
+ self._new_dir = Path(name).absolute()
43
+ self._new_dir.mkdir(exist_ok=True, parents=True)
44
+
45
+ self._old_cwd = Path.cwd()
46
+ self._chdir = chdir
47
+ if chdir:
48
+ _os.chdir(self._new_dir)
49
+ self._delete_when_done = delete_when_done
50
+
51
+ def __del__(self):
52
+ """
53
+ Calls self.done().
54
+ """
55
+ self.done()
56
+
57
+ @property
58
+ def path(self) -> Path:
59
+ """
60
+ What is the path to the temporary directory.
61
+ """
62
+ return self._new_dir
63
+
64
+ def __truediv__(self, arg) -> Path:
65
+ """
66
+ Append a path element, just like a Path object.
67
+ """
68
+ return self.path / arg
69
+
70
+ def done(self) -> None:
71
+ """
72
+ Change back to the old working directory and delete the temporary
73
+ directory, along with all its contents.
74
+
75
+ Subsequent calls to done() take no further action.
76
+ """
77
+ if self._new_dir is not None:
78
+ if self._chdir:
79
+ _os.chdir(self._old_cwd)
80
+ if self._delete_when_done:
81
+ _shutil.rmtree(self._new_dir)
82
+ self._old_cwd = None
83
+ self._new_dir = None
84
+
85
+ def __enter__(self):
86
+ # nothing to do - already created at __init__
87
+ return self
88
+
89
+ def __exit__(self, exc_type, exc_val, exc_tb):
90
+ self.done()
91
+ return exc_val is None
92
+
93
+ def __str__(self):
94
+ return str(self._new_dir)
ck_demos/__init__.py ADDED
File without changes
File without changes
@@ -0,0 +1,15 @@
1
+ """
2
+ This is an example Python script for copying Ace to CK.
3
+ """
4
+ from ck.pgm_compiler.ace import copy_ace_to_default_location
5
+
6
+
7
+ SOURCE_ACE: str = r'C:\Research\Ace\ace_v3.0_windows'
8
+
9
+
10
+ def main() -> None:
11
+ copy_ace_to_default_location(SOURCE_ACE)
12
+
13
+
14
+ if __name__ == "__main__":
15
+ main()
@@ -0,0 +1,49 @@
1
+ from ck import example
2
+ from ck.pgm import PGM
3
+ from ck.pgm_compiler import ace
4
+ from ck.pgm_circuit import PGMCircuit
5
+ from ck.pgm_circuit.wmc_program import WMCProgram
6
+
7
+
8
+ def main() -> None:
9
+
10
+ if not ace.ace_available():
11
+ print("ACE is not available")
12
+ exit(1)
13
+
14
+ pgm: PGM = example.Rain()
15
+
16
+ # `ace.compile_pgm` will look for an Ace installation in
17
+ # a default location. If Ace is not installed in the default
18
+ # location, then either: (1) pass the location as an argument
19
+ # to `ace.compile_pgm`, or (2) copy the Ace files to
20
+ # the default location using `ace.copy_ace_to_default_location`.
21
+ #
22
+ # Here is an example showing how to copy Ace to the default
23
+ # location from a source directory.
24
+ #
25
+ # ace.copy_ace_to_default_location(r'C:\Research\Ace\ace_v3.0_windows')
26
+
27
+ pgm_cct: PGMCircuit = ace.compile_pgm(pgm, print_output=True)
28
+
29
+ print(f'PGM: {pgm.name}')
30
+ print()
31
+ print(f'Circuit:')
32
+ pgm_cct.dump()
33
+ print()
34
+
35
+ wmc = WMCProgram(pgm_cct)
36
+
37
+ print('Showing Program results:')
38
+ for indicators in pgm.instances_as_indicators():
39
+ instance_as_str = pgm.indicator_str(*indicators)
40
+ wmc_value = wmc.wmc(*indicators)
41
+ pgm_value = pgm.value_product_indicators(*indicators)
42
+ print(f' {instance_as_str:80} {wmc_value:.6f} {pgm_value:.6f}')
43
+
44
+ print()
45
+ print('Done.')
46
+
47
+
48
+ if __name__ == '__main__':
49
+ main()
ck_demos/all_demos.py ADDED
@@ -0,0 +1,88 @@
1
+ """
2
+ Run all the demo scripts (except nominated exclusions).
3
+ This may be used as a 'smoke test'.
4
+ """
5
+ import sys
6
+ from pathlib import Path
7
+ from subprocess import call
8
+
9
+ from ck_demos.utils.stop_watch import StopWatch
10
+
11
+ DEMO_TOP_DIR = '.' # relative to the directory this script is in
12
+
13
+ # These are scripts and directories we explicitly will not include
14
+ EXCEPTIONS = [
15
+ 'utils', # package of utilities
16
+ 'ace', # 3rd party, optional dependency
17
+ ]
18
+
19
+ # These are what are printed as a separator lines
20
+ LINE = '-' * 80
21
+ LINE_2 = '=' * 80
22
+
23
+
24
+ def main() -> int:
25
+ python_exe = sys.executable
26
+ print('Python executable:', python_exe)
27
+ print()
28
+
29
+ errors = []
30
+ script_count = 0
31
+ python_env = None
32
+ total_time = StopWatch()
33
+
34
+ # Start search of demo directories in the script directory.
35
+ script_path = Path(__file__)
36
+ script_name = script_path.stem
37
+ demos_top_dir_path = (script_path.parent / DEMO_TOP_DIR)
38
+ dirs = find_demo_dirs(demos_top_dir_path)
39
+
40
+ # Always exclude self if we happen to find our own script.
41
+ exceptions = set(EXCEPTIONS + [Path(__file__).name])
42
+
43
+ for demo_dir in dirs:
44
+ for script in demo_dir.iterdir():
45
+ if (
46
+ script.is_file() and
47
+ script.name.endswith('.py') and
48
+ (not script.name.startswith('_')) and
49
+ script.name not in exceptions
50
+ ):
51
+ print(LINE)
52
+ print(script.name)
53
+ print(LINE)
54
+ script_count += 1
55
+
56
+ time = StopWatch()
57
+ return_code = call([python_exe, script.as_posix()], env=python_env)
58
+ time.stop()
59
+
60
+ if return_code != 0:
61
+ errors.append(f'Error code {return_code}: {script.name}')
62
+ print(LINE)
63
+ print(f'exited with code {return_code}, time = {time}, file = {script.name}')
64
+
65
+ print(LINE_2)
66
+ print(f'Done running {script_name}, {script_count} scripts in {total_time}s')
67
+ print(f'Number of errors: {len(errors)}')
68
+ for error in errors:
69
+ print(error)
70
+
71
+ # Provide a useful exit code.
72
+ if len(errors) > 0:
73
+ return -1
74
+ else:
75
+ return 0
76
+
77
+
78
+ def find_demo_dirs(demos_top_dir_path: Path):
79
+ return [demos_top_dir_path] + [
80
+ d
81
+ for d in demos_top_dir_path.iterdir()
82
+ if d.is_dir() and d.name not in EXCEPTIONS
83
+ ]
84
+
85
+
86
+ if __name__ == '__main__':
87
+ ret_code = main()
88
+ exit(ret_code)
File without changes
@@ -0,0 +1,22 @@
1
+ """
2
+ This demo shows how to dump a Circuit.
3
+ """
4
+ from ck.circuit import Circuit
5
+
6
+
7
+ def main() -> None:
8
+ cct = Circuit()
9
+ x0 = cct.new_var() # this var will have index 0
10
+ x1 = cct.new_var() # this var will have index 1
11
+ c123 = cct.const(123)
12
+ m = cct.mul(x0, x1)
13
+ _ = cct.add(c123, m)
14
+
15
+ cct.dump()
16
+
17
+ # with open('demo_cct_dump.txt', 'w') as out:
18
+ # circuit.dump(out)
19
+
20
+
21
+ if __name__ == '__main__':
22
+ main()
@@ -0,0 +1,43 @@
1
+ """
2
+ This is a script for exploring partial derivatives.
3
+ """
4
+ from ck.circuit_compiler.llvm_compiler import compile_circuit
5
+ from ck.circuit import Circuit
6
+ from ck.program import Program
7
+
8
+
9
+ def main() -> None:
10
+ cct = Circuit()
11
+ x = cct.new_vars(2) # indicators x[0] and x[1]
12
+ y = cct.new_vars(2) # indicators y[0] and y[1]
13
+ f = cct.new_vars(2) # factor between x and y
14
+ q = cct.add(
15
+ cct.mul(x[0], y[0], f[0]),
16
+ cct.mul(x[0], y[1], f[1]),
17
+ cct.mul(x[1], y[0], f[1]),
18
+ cct.mul(x[1], y[1], f[0]),
19
+ )
20
+
21
+ derivatives = cct.partial_derivatives(q, f) # returns a list: [f0', f1']
22
+
23
+ # prog is a function from [x0, x1, y0, y1, f0, f1] to [f0', f1', q]
24
+ prog = Program(compile_circuit(*derivatives + [q]))
25
+
26
+ # Make a dataset.
27
+ dataset = [
28
+ # x y f
29
+ [1, 0, 1, 0, 0.4, 0.6],
30
+ [0, 1, 1, 0, 0.4, 0.6],
31
+ [1, 0, 0, 1, 0.4, 0.6],
32
+ [0, 1, 0, 1, 0.4, 0.6],
33
+ [1, 1, 1, 1, 0.4, 0.6],
34
+ ]
35
+
36
+ for instance in dataset:
37
+ result = prog(*instance)
38
+ print(instance, result[:-1], result[-1])
39
+ print()
40
+
41
+
42
+ if __name__ == '__main__':
43
+ main()
File without changes
@@ -0,0 +1,32 @@
1
+ from typing import Sequence
2
+
3
+ from ck import example
4
+ from ck.circuit_compiler import NamedCircuitCompiler
5
+ from ck.pgm import PGM
6
+ from ck.pgm_compiler.named_pgm_compilers import NamedPGMCompiler
7
+ from ck_demos.utils.compare import compare
8
+
9
+ PGMS: Sequence[PGM] = [
10
+ example.Rain(),
11
+ # example.Insurance(),
12
+ # example.Pathfinder(),
13
+ # example.Mildew(),
14
+ ]
15
+
16
+ PGM_COMPILERS: Sequence[NamedPGMCompiler] = [NamedPGMCompiler.FE_BEST_JOINTREE]
17
+
18
+ CCT_COMPILERS: Sequence[NamedCircuitCompiler] = list(NamedCircuitCompiler)
19
+
20
+
21
+ def main() -> None:
22
+ compare(
23
+ pgms=PGMS,
24
+ pgm_compilers=PGM_COMPILERS,
25
+ cct_compilers=CCT_COMPILERS,
26
+ )
27
+ print()
28
+ print('Done.')
29
+
30
+
31
+ if __name__ == '__main__':
32
+ main()
@@ -0,0 +1,26 @@
1
+ from ck import example
2
+ from ck.circuit import CircuitNode
3
+ from ck.circuit_compiler import llvm_vm_compiler
4
+ from ck.circuit_compiler.support.llvm_ir_function import LLVMRawProgram
5
+ from ck.pgm import PGM
6
+ from ck.pgm_circuit import PGMCircuit
7
+ from ck.pgm_compiler import NamedPGMCompiler, PGMCompiler
8
+
9
+ EXAMPLE_PGM: PGM = example.Rain()
10
+ PGM_COMPILER: PGMCompiler = NamedPGMCompiler.FE_BEST_JOINTREE
11
+
12
+
13
+ def main() -> None:
14
+ pgm_cct: PGMCircuit = PGM_COMPILER(EXAMPLE_PGM)
15
+ top: CircuitNode = pgm_cct.circuit_top
16
+
17
+ program: LLVMRawProgram = llvm_vm_compiler.compile_circuit(top)
18
+
19
+ print(program.llvm_program)
20
+
21
+ print()
22
+ print('Done.')
23
+
24
+
25
+ if __name__ == '__main__':
26
+ main()
File without changes
@@ -0,0 +1,18 @@
1
+ from ck import example
2
+
3
+
4
+ def main() -> None:
5
+ """
6
+ Demonstrate dumping a PGM
7
+ """
8
+ pgm = example.Alarm()
9
+
10
+ print('Dumping PGM')
11
+ pgm.dump()
12
+
13
+ print()
14
+ print('Done.')
15
+
16
+
17
+ if __name__ == '__main__':
18
+ main()
@@ -0,0 +1,18 @@
1
+ """
2
+ Demonstrate dumping a PGM - stressing the method
3
+ """
4
+ from ck import example
5
+
6
+
7
+ def main() -> None:
8
+ pgm = example.Stress()
9
+
10
+ print('Dumping PGM')
11
+ pgm.dump(show_function_values=True)
12
+
13
+ print()
14
+ print('Done.')
15
+
16
+
17
+ if __name__ == '__main__':
18
+ main()
@@ -0,0 +1,15 @@
1
+ from ck.pgm import PGM
2
+
3
+
4
+ def main() -> None:
5
+ pgm = PGM()
6
+
7
+ a = pgm.new_rv('A', ('x', 'y', 'z'))
8
+ b = pgm.new_rv('B', (3, 5))
9
+
10
+ print(pgm.indicator_str(a[0], b[1], a[2]))
11
+ print(pgm.condition_str(a[0], b[1], a[2]))
12
+
13
+
14
+ if __name__ == '__main__':
15
+ main()
@@ -0,0 +1,25 @@
1
+ from ck import example
2
+ from ck.pgm import PGM
3
+
4
+ INDENT: str = ' '
5
+ PRECISION: int = 3
6
+ MAX_STATE_DIGITS = 21
7
+
8
+
9
+ def main() -> None:
10
+ """
11
+ Show all standard PGM examples
12
+ """
13
+ for example_name, pgm_class in example.ALL_EXAMPLES.items():
14
+ pgm: PGM = pgm_class()
15
+
16
+ print(example_name)
17
+ pgm.dump_synopsis(prefix=INDENT)
18
+ print()
19
+
20
+ print()
21
+ print('Done.')
22
+
23
+
24
+ if __name__ == '__main__':
25
+ main()
File without changes
@@ -0,0 +1,63 @@
1
+ from typing import Sequence
2
+
3
+ from ck import example
4
+ from ck.circuit_compiler import NamedCircuitCompiler
5
+ from ck.pgm import PGM
6
+ from ck.pgm_circuit.support.compile_circuit import DEFAULT_CIRCUIT_COMPILER
7
+ from ck.pgm_compiler.named_pgm_compilers import NamedPGMCompiler
8
+ from ck_demos.utils.compare import compare
9
+
10
+ # @formatter:off
11
+
12
+ # =========================================
13
+ # Experiment configuration
14
+ # =========================================
15
+
16
+ CACHE_CIRCUITS: bool = True
17
+ BREAK_BETWEEN_PGMS: bool = True
18
+ COMMA_NUMBERS: bool = True
19
+ PRINT_HEADER: bool = True
20
+
21
+ PGMS: Sequence[PGM] = [
22
+ example.Rain(),
23
+ example.Cancer(),
24
+ example.Earthquake(),
25
+ example.Asia(),
26
+ example.Survey(),
27
+ example.Sachs(),
28
+ example.Child(),
29
+ example.Alarm(),
30
+
31
+ # example.Hailfinder(),
32
+ # example.Insurance(),
33
+ # example.Pathfinder(),
34
+ # example.Mildew(),
35
+ ]
36
+
37
+ PGM_COMPILERS: Sequence[NamedPGMCompiler] = [
38
+ named_compiler
39
+ for named_compiler in NamedPGMCompiler
40
+ if named_compiler.name.startswith('FE_') and 'WEIGHTED' not in named_compiler.name
41
+ ] + [NamedPGMCompiler.ACE]
42
+
43
+ CCT_COMPILERS: Sequence[NamedCircuitCompiler] = [DEFAULT_CIRCUIT_COMPILER]
44
+
45
+ # @formatter:on
46
+
47
+
48
+ def main() -> None:
49
+ compare(
50
+ pgms=PGMS,
51
+ pgm_compilers=PGM_COMPILERS,
52
+ cct_compilers=CCT_COMPILERS,
53
+ cache_circuits=CACHE_CIRCUITS,
54
+ break_between_pgms=BREAK_BETWEEN_PGMS,
55
+ comma_numbers=COMMA_NUMBERS,
56
+ print_header=PRINT_HEADER,
57
+ )
58
+ print()
59
+ print('Done.')
60
+
61
+
62
+ if __name__ == '__main__':
63
+ main()
@@ -0,0 +1,60 @@
1
+ from ck import example
2
+ from ck.circuit import Circuit
3
+ from ck.pgm import PGM
4
+ from ck.pgm_circuit import PGMCircuit
5
+ from ck.pgm_circuit.wmc_program import WMCProgram
6
+ from ck.pgm_compiler.factor_elimination import join_tree_to_circuit
7
+ from ck.pgm_compiler.support.clusters import min_degree, Clusters
8
+ from ck.pgm_compiler.support.join_tree import JoinTree, clusters_to_join_tree
9
+
10
+
11
+ def main() -> None:
12
+ """
13
+ This demo shows the full compilation chain for factor elimination.
14
+
15
+ Process:
16
+ Rain example -> PGM
17
+ min_degree -> Clusters
18
+ clusters_to_join_tree -> JoinTree
19
+ join_tree_to_circuit -> PGMCircuit
20
+ default circuit compiler -> WMCProgram
21
+ """
22
+ pgm: PGM = example.Rain()
23
+
24
+ print(f'PGM {pgm.name!r}')
25
+ print()
26
+
27
+ clusters: Clusters = min_degree(pgm)
28
+
29
+ clusters.dump()
30
+
31
+ join_tree: JoinTree = clusters_to_join_tree(clusters)
32
+
33
+ print('Join Tree:')
34
+ join_tree.dump()
35
+ print()
36
+
37
+ pgm_circuit: PGMCircuit = join_tree_to_circuit(join_tree)
38
+ circuit: Circuit = pgm_circuit.circuit_top.circuit
39
+
40
+ print('Circuit:')
41
+ circuit.dump()
42
+ print()
43
+
44
+ wmc = WMCProgram(pgm_circuit)
45
+
46
+ print()
47
+ print('Showing Program Results:')
48
+ print(f' {"State":80} {"WMC":8} {"PGM":8}')
49
+ for indicators in pgm.instances_as_indicators():
50
+ instance_as_str = pgm.indicator_str(*indicators)
51
+ wmc_value = wmc.wmc(*indicators)
52
+ pgm_value = pgm.value_product_indicators(*indicators)
53
+ print(f' {instance_as_str:80} {wmc_value:8.6f} {pgm_value:8.6f}')
54
+
55
+ print()
56
+ print('Done.')
57
+
58
+
59
+ if __name__ == '__main__':
60
+ main()
@@ -0,0 +1,47 @@
1
+ from ck import example
2
+ from ck.pgm import PGM
3
+ from ck.pgm_circuit import PGMCircuit
4
+ from ck.pgm_circuit.wmc_program import WMCProgram
5
+ from ck.pgm_compiler import factor_elimination
6
+ from ck_demos.utils.stop_watch import StopWatch
7
+
8
+
9
+ def main() -> None:
10
+ pgm: PGM = example.Insurance()
11
+
12
+ print(f'PGM: {pgm.name}')
13
+
14
+ time = StopWatch()
15
+ pgm_cct: PGMCircuit = factor_elimination.compile_pgm_best_jointree(pgm)
16
+ time.stop()
17
+ print(f'time to compile PGM to Circuit: {time}')
18
+
19
+ time.start()
20
+ wmc = WMCProgram(pgm_cct)
21
+ time.stop()
22
+ print(f'time to compile Circuit to Program: {time}')
23
+
24
+ time.start()
25
+ for _ in range(1000):
26
+ wmc.compute()
27
+ time.stop()
28
+ print(f'time to execute Program: {time.seconds() * 1000:,.3f}μs ', end='')
29
+
30
+ # print()
31
+ # print(f'Circuit:')
32
+ # pgm_cct.dump()
33
+ # print()
34
+ #
35
+ # print('Showing Program results:')
36
+ # for indicators in pgm.instances_as_indicators():
37
+ # instance_as_str = pgm.indicator_str(*indicators)
38
+ # wmc_value = wmc.wmc(*indicators)
39
+ # pgm_value = pgm.value_product_indicators(*indicators)
40
+ # print(f' {instance_as_str:80} {wmc_value:.6f} {pgm_value:.6f}')
41
+
42
+ print()
43
+ print('Done.')
44
+
45
+
46
+ if __name__ == '__main__':
47
+ main()
@@ -0,0 +1,25 @@
1
+ from ck import example
2
+ from ck.pgm_compiler.support.clusters import min_degree, Clusters
3
+
4
+
5
+ def main() -> None:
6
+ pgm = example.Alarm()
7
+
8
+ clusters: Clusters = min_degree(pgm)
9
+
10
+ print('Elimination order:')
11
+ for rv_idx in clusters.eliminated:
12
+ print(f' {pgm.rvs[rv_idx]}')
13
+ print()
14
+
15
+ print('Clusters:')
16
+ for cluster in clusters.clusters:
17
+ cluster_str = ', '.join(str(pgm.rvs[rv_idx]) for rv_idx in cluster)
18
+ print(f' {cluster_str}')
19
+
20
+ print()
21
+ print('Done.')
22
+
23
+
24
+ if __name__ == '__main__':
25
+ main()