compiled-knowledge 4.0.0a5__cp313-cp313-macosx_10_13_universal2.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.
- ck/__init__.py +0 -0
- ck/circuit/__init__.py +13 -0
- ck/circuit/circuit.c +38749 -0
- ck/circuit/circuit.cpython-313-darwin.so +0 -0
- ck/circuit/circuit_py.py +807 -0
- ck/circuit/tmp_const.py +74 -0
- ck/circuit_compiler/__init__.py +2 -0
- ck/circuit_compiler/circuit_compiler.py +26 -0
- ck/circuit_compiler/cython_vm_compiler/__init__.py +1 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.c +17373 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-313-darwin.so +0 -0
- ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py +96 -0
- ck/circuit_compiler/interpret_compiler.py +223 -0
- ck/circuit_compiler/llvm_compiler.py +388 -0
- ck/circuit_compiler/llvm_vm_compiler.py +546 -0
- ck/circuit_compiler/named_circuit_compilers.py +57 -0
- ck/circuit_compiler/support/__init__.py +0 -0
- ck/circuit_compiler/support/circuit_analyser.py +81 -0
- ck/circuit_compiler/support/input_vars.py +148 -0
- ck/circuit_compiler/support/llvm_ir_function.py +234 -0
- ck/example/__init__.py +53 -0
- ck/example/alarm.py +366 -0
- ck/example/asia.py +28 -0
- ck/example/binary_clique.py +32 -0
- ck/example/bow_tie.py +33 -0
- ck/example/cancer.py +37 -0
- ck/example/chain.py +38 -0
- ck/example/child.py +199 -0
- ck/example/clique.py +33 -0
- ck/example/cnf_pgm.py +39 -0
- ck/example/diamond_square.py +68 -0
- ck/example/earthquake.py +36 -0
- ck/example/empty.py +10 -0
- ck/example/hailfinder.py +539 -0
- ck/example/hepar2.py +628 -0
- ck/example/insurance.py +504 -0
- ck/example/loop.py +40 -0
- ck/example/mildew.py +38161 -0
- ck/example/munin.py +22982 -0
- ck/example/pathfinder.py +53674 -0
- ck/example/rain.py +39 -0
- ck/example/rectangle.py +161 -0
- ck/example/run.py +30 -0
- ck/example/sachs.py +129 -0
- ck/example/sprinkler.py +30 -0
- ck/example/star.py +44 -0
- ck/example/stress.py +64 -0
- ck/example/student.py +43 -0
- ck/example/survey.py +46 -0
- ck/example/triangle_square.py +54 -0
- ck/example/truss.py +49 -0
- ck/in_out/__init__.py +3 -0
- ck/in_out/parse_ace_lmap.py +216 -0
- ck/in_out/parse_ace_nnf.py +288 -0
- ck/in_out/parse_net.py +480 -0
- ck/in_out/parser_utils.py +185 -0
- ck/in_out/pgm_pickle.py +42 -0
- ck/in_out/pgm_python.py +268 -0
- ck/in_out/render_bugs.py +111 -0
- ck/in_out/render_net.py +177 -0
- ck/in_out/render_pomegranate.py +184 -0
- ck/pgm.py +3494 -0
- ck/pgm_circuit/__init__.py +1 -0
- ck/pgm_circuit/marginals_program.py +352 -0
- ck/pgm_circuit/mpe_program.py +237 -0
- ck/pgm_circuit/pgm_circuit.py +75 -0
- ck/pgm_circuit/program_with_slotmap.py +234 -0
- ck/pgm_circuit/slot_map.py +35 -0
- ck/pgm_circuit/support/__init__.py +0 -0
- ck/pgm_circuit/support/compile_circuit.py +83 -0
- ck/pgm_circuit/target_marginals_program.py +103 -0
- ck/pgm_circuit/wmc_program.py +323 -0
- ck/pgm_compiler/__init__.py +2 -0
- ck/pgm_compiler/ace/__init__.py +1 -0
- ck/pgm_compiler/ace/ace.py +252 -0
- ck/pgm_compiler/factor_elimination.py +383 -0
- ck/pgm_compiler/named_pgm_compilers.py +63 -0
- ck/pgm_compiler/pgm_compiler.py +19 -0
- ck/pgm_compiler/recursive_conditioning.py +226 -0
- ck/pgm_compiler/support/__init__.py +0 -0
- ck/pgm_compiler/support/circuit_table/__init__.py +9 -0
- ck/pgm_compiler/support/circuit_table/circuit_table.c +16042 -0
- ck/pgm_compiler/support/circuit_table/circuit_table.cpython-313-darwin.so +0 -0
- ck/pgm_compiler/support/circuit_table/circuit_table_py.py +269 -0
- ck/pgm_compiler/support/clusters.py +556 -0
- ck/pgm_compiler/support/factor_tables.py +398 -0
- ck/pgm_compiler/support/join_tree.py +275 -0
- ck/pgm_compiler/support/named_compiler_maker.py +33 -0
- ck/pgm_compiler/variable_elimination.py +89 -0
- ck/probability/__init__.py +0 -0
- ck/probability/empirical_probability_space.py +47 -0
- ck/probability/probability_space.py +568 -0
- ck/program/__init__.py +3 -0
- ck/program/program.py +129 -0
- ck/program/program_buffer.py +180 -0
- ck/program/raw_program.py +61 -0
- ck/sampling/__init__.py +0 -0
- ck/sampling/forward_sampler.py +211 -0
- ck/sampling/marginals_direct_sampler.py +113 -0
- ck/sampling/sampler.py +62 -0
- ck/sampling/sampler_support.py +232 -0
- ck/sampling/uniform_sampler.py +66 -0
- ck/sampling/wmc_direct_sampler.py +169 -0
- ck/sampling/wmc_gibbs_sampler.py +147 -0
- ck/sampling/wmc_metropolis_sampler.py +159 -0
- ck/sampling/wmc_rejection_sampler.py +113 -0
- ck/utils/__init__.py +0 -0
- ck/utils/iter_extras.py +153 -0
- ck/utils/map_list.py +128 -0
- ck/utils/map_set.py +128 -0
- ck/utils/np_extras.py +51 -0
- ck/utils/random_extras.py +64 -0
- ck/utils/tmp_dir.py +94 -0
- ck_demos/__init__.py +0 -0
- ck_demos/ace/__init__.py +0 -0
- ck_demos/ace/copy_ace_to_ck.py +15 -0
- ck_demos/ace/demo_ace.py +44 -0
- ck_demos/all_demos.py +88 -0
- ck_demos/circuit/__init__.py +0 -0
- ck_demos/circuit/demo_circuit_dump.py +22 -0
- ck_demos/circuit/demo_derivatives.py +43 -0
- ck_demos/circuit_compiler/__init__.py +0 -0
- ck_demos/circuit_compiler/compare_circuit_compilers.py +32 -0
- ck_demos/circuit_compiler/show_llvm_program.py +26 -0
- ck_demos/pgm/__init__.py +0 -0
- ck_demos/pgm/demo_pgm_dump.py +18 -0
- ck_demos/pgm/demo_pgm_dump_stress.py +18 -0
- ck_demos/pgm/demo_pgm_string_rendering.py +15 -0
- ck_demos/pgm/show_examples.py +25 -0
- ck_demos/pgm_compiler/__init__.py +0 -0
- ck_demos/pgm_compiler/compare_pgm_compilers.py +50 -0
- ck_demos/pgm_compiler/demo_compiler_dump.py +50 -0
- ck_demos/pgm_compiler/demo_factor_elimination.py +47 -0
- ck_demos/pgm_compiler/demo_join_tree.py +25 -0
- ck_demos/pgm_compiler/demo_marginals_program.py +53 -0
- ck_demos/pgm_compiler/demo_mpe_program.py +55 -0
- ck_demos/pgm_compiler/demo_pgm_compiler.py +38 -0
- ck_demos/pgm_compiler/demo_recursive_conditioning.py +33 -0
- ck_demos/pgm_compiler/demo_variable_elimination.py +33 -0
- ck_demos/pgm_compiler/demo_wmc_program.py +29 -0
- ck_demos/pgm_inference/__init__.py +0 -0
- ck_demos/pgm_inference/demo_inferencing_basic.py +188 -0
- ck_demos/pgm_inference/demo_inferencing_mpe_cancer.py +45 -0
- ck_demos/pgm_inference/demo_inferencing_wmc_and_mpe_sprinkler.py +154 -0
- ck_demos/pgm_inference/demo_inferencing_wmc_student.py +110 -0
- ck_demos/programs/__init__.py +0 -0
- ck_demos/programs/demo_program_buffer.py +24 -0
- ck_demos/programs/demo_program_multi.py +24 -0
- ck_demos/programs/demo_program_none.py +19 -0
- ck_demos/programs/demo_program_single.py +23 -0
- ck_demos/programs/demo_raw_program_interpreted.py +21 -0
- ck_demos/programs/demo_raw_program_llvm.py +21 -0
- ck_demos/sampling/__init__.py +0 -0
- ck_demos/sampling/check_sampler.py +71 -0
- ck_demos/sampling/demo_marginal_direct_sampler.py +40 -0
- ck_demos/sampling/demo_uniform_sampler.py +38 -0
- ck_demos/sampling/demo_wmc_direct_sampler.py +40 -0
- ck_demos/utils/__init__.py +0 -0
- ck_demos/utils/compare.py +88 -0
- ck_demos/utils/convert_network.py +45 -0
- ck_demos/utils/sample_model.py +216 -0
- ck_demos/utils/stop_watch.py +384 -0
- compiled_knowledge-4.0.0a5.dist-info/METADATA +50 -0
- compiled_knowledge-4.0.0a5.dist-info/RECORD +167 -0
- compiled_knowledge-4.0.0a5.dist-info/WHEEL +5 -0
- compiled_knowledge-4.0.0a5.dist-info/licenses/LICENSE.txt +21 -0
- compiled_knowledge-4.0.0a5.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import List, Sequence
|
|
4
|
+
|
|
5
|
+
from ck.circuit import CircuitNode
|
|
6
|
+
from ck.pgm import PGM
|
|
7
|
+
from ck.pgm_circuit import PGMCircuit
|
|
8
|
+
from ck.pgm_compiler.support import clusters
|
|
9
|
+
from ck.pgm_compiler.support.circuit_table import CircuitTable, product, sum_out
|
|
10
|
+
from ck.pgm_compiler.support.clusters import ClusterAlgorithm
|
|
11
|
+
from ck.pgm_compiler.support.factor_tables import make_factor_tables, FactorTables
|
|
12
|
+
|
|
13
|
+
# Standard cluster algorithms.
|
|
14
|
+
MIN_DEGREE: ClusterAlgorithm = clusters.min_degree
|
|
15
|
+
MIN_FILL: ClusterAlgorithm = clusters.min_fill
|
|
16
|
+
MIN_DEGREE_THEN_FILL: ClusterAlgorithm = clusters.min_degree_then_fill
|
|
17
|
+
MIN_FILL_THEN_DEGREE: ClusterAlgorithm = clusters.min_fill_then_degree
|
|
18
|
+
MIN_WEIGHTED_DEGREE: ClusterAlgorithm = clusters.min_weighted_degree
|
|
19
|
+
MIN_WEIGHTED_FILL: ClusterAlgorithm = clusters.min_weighted_fill
|
|
20
|
+
MIN_TRADITIONAL_WEIGHTED_FILL: ClusterAlgorithm = clusters.min_traditional_weighted_fill
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def compile_pgm(
|
|
24
|
+
pgm: PGM,
|
|
25
|
+
const_parameters: bool = True,
|
|
26
|
+
*,
|
|
27
|
+
algorithm: ClusterAlgorithm = MIN_FILL_THEN_DEGREE,
|
|
28
|
+
pre_prune_factor_tables: bool = True,
|
|
29
|
+
) -> PGMCircuit:
|
|
30
|
+
"""
|
|
31
|
+
Compile the PGM to an arithmetic circuit, using variable elimination.
|
|
32
|
+
|
|
33
|
+
Conforms to the `PGMCompiler` protocol.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
pgm: The PGM to compile.
|
|
37
|
+
const_parameters: If true, the potential function parameters will be circuit
|
|
38
|
+
constants, otherwise they will be circuit variables.
|
|
39
|
+
algorithm: algorithm to get an elimination order.
|
|
40
|
+
pre_prune_factor_tables: if true, then heuristics will be used to remove any provably zero row.
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
a PGMCircuit object.
|
|
44
|
+
"""
|
|
45
|
+
factor_tables: FactorTables = make_factor_tables(
|
|
46
|
+
pgm=pgm,
|
|
47
|
+
const_parameters=const_parameters,
|
|
48
|
+
multiply_indicators=True,
|
|
49
|
+
pre_prune_factor_tables=pre_prune_factor_tables,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
elimination_order: Sequence[int] = algorithm(pgm).eliminated
|
|
53
|
+
|
|
54
|
+
cur_tables: List[CircuitTable] = list(factor_tables.tables)
|
|
55
|
+
for rv_idx in elimination_order:
|
|
56
|
+
next_tables: List[CircuitTable] = []
|
|
57
|
+
tables_with_rv: List[CircuitTable] = []
|
|
58
|
+
for table in cur_tables:
|
|
59
|
+
if rv_idx in table.rv_idxs:
|
|
60
|
+
tables_with_rv.append(table)
|
|
61
|
+
else:
|
|
62
|
+
next_tables.append(table)
|
|
63
|
+
if len(tables_with_rv) > 0:
|
|
64
|
+
while len(tables_with_rv) > 1:
|
|
65
|
+
# product the two smallest tables
|
|
66
|
+
tables_with_rv.sort(key=lambda _t: -len(_t))
|
|
67
|
+
x = tables_with_rv.pop()
|
|
68
|
+
y = tables_with_rv.pop()
|
|
69
|
+
tables_with_rv.append(product(x, y))
|
|
70
|
+
next_tables.append(sum_out(tables_with_rv[0], (rv_idx,)))
|
|
71
|
+
cur_tables = next_tables
|
|
72
|
+
# All rvs are now eliminated
|
|
73
|
+
tops: List[CircuitNode] = [
|
|
74
|
+
table.top()
|
|
75
|
+
for table in cur_tables
|
|
76
|
+
if len(table) > 0
|
|
77
|
+
]
|
|
78
|
+
top = factor_tables.circuit.optimised_add(tops)
|
|
79
|
+
top.circuit.remove_unreachable_op_nodes(top)
|
|
80
|
+
|
|
81
|
+
return PGMCircuit(
|
|
82
|
+
rvs=tuple(pgm.rvs),
|
|
83
|
+
conditions=(),
|
|
84
|
+
circuit_top=top,
|
|
85
|
+
number_of_indicators=factor_tables.number_of_indicators,
|
|
86
|
+
number_of_parameters=factor_tables.number_of_parameters,
|
|
87
|
+
slot_map=factor_tables.slot_map,
|
|
88
|
+
parameter_values=factor_tables.parameter_values,
|
|
89
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from typing import Sequence, Iterable, Tuple, Dict, List
|
|
2
|
+
|
|
3
|
+
from ck.pgm import RandomVariable, Indicator, Instance
|
|
4
|
+
from ck.probability.probability_space import ProbabilitySpace, Condition, check_condition
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class EmpiricalProbabilitySpace(ProbabilitySpace):
|
|
8
|
+
def __init__(self, rvs: Sequence[RandomVariable], samples: Iterable[Instance]):
|
|
9
|
+
"""
|
|
10
|
+
Assumes:
|
|
11
|
+
len(sample) == len(rvs), for each sample in samples.
|
|
12
|
+
0 <= sample[i] < len(rvs[i]), for each sample in samples, for i in range(len(rvs)).
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
rvs: The random variables.
|
|
16
|
+
samples: instances (state indexes) that are samples from the given rvs.
|
|
17
|
+
"""
|
|
18
|
+
self._rvs: Sequence[RandomVariable] = tuple(rvs)
|
|
19
|
+
self._samples: List[Instance] = list(samples)
|
|
20
|
+
self._rv_idx_to_sample_idx: Dict[int, int] = {
|
|
21
|
+
rv.idx: i
|
|
22
|
+
for i, rv in enumerate(self._rvs)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def rvs(self) -> Sequence[RandomVariable]:
|
|
27
|
+
return self._rvs
|
|
28
|
+
|
|
29
|
+
def wmc(self, *condition: Condition) -> float:
|
|
30
|
+
condition: Tuple[Indicator, ...] = check_condition(condition)
|
|
31
|
+
|
|
32
|
+
checks = [set() for _ in self._rvs]
|
|
33
|
+
for ind in condition:
|
|
34
|
+
checks[self._rv_idx_to_sample_idx[ind.rv_idx]].add(ind.state_idx)
|
|
35
|
+
for i in range(len(checks)):
|
|
36
|
+
if len(checks[i]) > 0:
|
|
37
|
+
checks[i] = set(range(len(self._rvs[i]))).difference(checks[i])
|
|
38
|
+
|
|
39
|
+
def satisfied(instance: Instance) -> bool:
|
|
40
|
+
return not any((state in check) for state, check in zip(instance, checks))
|
|
41
|
+
|
|
42
|
+
return sum(1 for _ in filter(satisfied, self._samples))
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def z(self) -> float:
|
|
46
|
+
return len(self._samples)
|
|
47
|
+
|