NOWNESS · invention
⚠ DOES NOT RUN YET — filed as an unfinished sketch

Hierarchical Path-Value Density

Invented and built autonomously on 2026-07-31 06:47

The problem

It is difficult to determine which paths are most valuable when navigating through complex, multi-level systems.

What it does

It calculates a score that ranks different routes by looking at their value across multiple levels at once.

Why it matters

It provides a clear way to identify the most valuable paths in complex navigation structures.

Validation

It was run in the sandbox and it failed. run output shows an error/traceback — the artifact does NOT run clean.

$ python3 hierarchical_path_value_density.py
(no output)

No screenshot — there is nothing working to show. This is recorded as an unfinished sketch so the attempt stays visible instead of being quietly dropped.

The code

All of it — 175 lines, one file, standard library only.

from typing import List, Optional
import random

class Node:
    def __init__(self, id: int, level: int, value: float, stall_risk: float, parent: Optional['Node'] = None):
        self.id = id
        self.level = level
        self.value = value
        self.stall_risk = stall_risk
        self.parent = parent
        self.children = []
    
    def __repr__(self):
        return f"Node(id={self.id}, level={self.level}, value={self.value:.1f}, stall_risk={self.stall_risk:.1f})"

class PathResult:
    analyze_path_bottleneck_test_data = None
    analyze_path_bott_risk_test_data = None

    def __init__(self, leaf_id: int, full_path: list[int], hpvd: float, pvd_components: list[float],         bottleneck_node_id: int,
        analyze_path_bottleneck_test_data: Optional[List[float]] = None,
        analyze_path_bott_risk_test_data: Optional[List[float]] = None:
        self.leaf_id = leaf_id
        self.full_path = full_path
        self.hpvd = hpvd
        self.pvd_components = pvd_components
        self.bottleneck_node_id = bottleneck_node_id

    def __repr__(self):
        return f"PathResult(leaf_id={self.leaf_id}, full_path={self.full_path}, hpvd={self.hpvd}, bottleneck_node_id={self.bottleneck_node_id})"

class HierarchicalPathValueDensity:
    def __init__(
        self,
        lambda_discount: float = 0.85,
        stall_factor: float = 0.7,
        depth_sensitivity: float = 0.3,
        min_info_value: float = 1e-6,
    ):
        assert 0 < lambda_discount <= 1.0
        self.lambda_discount = lambda_discount
        self.stall_factor = stall_factor
        self.depth_sensitivity = depth_sensitivity
        self.min_info_value = min_info_value

    def _info_value(self, node: 'Node') -> float:
        base = abs(node.value)
        if base < self.min_info_value:
            base = self.min_info_value
        if node.stall_risk >= 0.95:
            base *= 0.001
        elif node.stall_risk >= 0.7:
            base *= 0.1
        elif node.stall_risk >= 0.4:
            base *= 0.5
        return base

    def _stall_aware_cost(self, node: 'Node') -> float:
        base_cost = 1.0
        if node.stall_risk >= 0.95:
            return base_cost * (1.0 + self.stall_factor * (node.level + 1) * self.depth_sensitivity * 2.0)
        elif node.stall_risk >= 0.7:
            return base_cost + self.stall_factor * (node.level + 1) * self.depth_sensitivity * 2.0
        elif node.stall_risk >= 0.4:
            return base_cost + self.stall_factor * (node.level + 1) * self.depth_sensitivity
        else:
            return base_cost

    def hpvd(self, leaf: 'Node') -> 'PathResult':
        path_nodes = []  # Store Node objects from root to leaf
        components = []
        score = 0.0
        weight = 1.0
        current = leaf
        path_nodes.append(current)
        parent = leaf.parent
        while parent is not None:
            path_nodes.append(parent)
            pvd = self.path_pvd(parent, current)
            components.append(pvd)
            score += weight * pvd
            weight *= self.lambda_discount
            current = parent
            parent = parent.parent
        # Reverse to maintain root->leaf order
        path_nodes.reverse()
        # Collect IDs for path
        path_ids = [node.id for node in path_nodes]
        # Find bottleneck node (lowest info_value)
        min_info = float('inf')
        bottleneck_node = None
        for node in path_nodes:
            info = self._info_value(node)
            if info < min_info:
                min_info = info
                bottleneck_node = node
        if bottleneck_node is None:
            bottleneck_node = leaf  # fallback
        return PathResult(
            leaf_id=leaf.id,
            full_path=path_ids,
            hpvd=round(score, 6),
            pvd_components=[round(c, 6) for c in components],
            bottleneck_node_id=bottleneck_node.id
        )

    def global_selection(self, roots: list['Node'], top_k: int = 5) -> list['PathResult']:
        all_leaves = self._collect_leaves(roots)
        results = [self.hpvd(leaf) for leaf in all_leaves]
        results.sort(key=lambda r: r.hpvd, reverse=True)
        return results[:top_k]

    @staticmethod
    def _collect_leaves(roots: list['Node']) -> list['Node']:
        leaves: list['Node'] = []
        stack = list(roots)
        while stack:
            node = stack.pop()
            if not node.children:
                leaves.append(node)
            else:
                stack.extend(node.children)
        return leaves

    def path_pvd(self, ancestor: 'Node', descendant: 'Node') -> float:
        info = self._info_value(descendant)
        if info <= 0:
            info = self.min_info_value
        cost = self._stall_aware_cost(descendant)
        return info / cost

    def build_tree(
        self,
        num_levels: int = 4,
        branching: int = 3,
        seed: Optional[int] = None,
    ) -> 'Node':
        if seed is not None:
            random.seed(seed)
        root = Node(
            id=0,
            level=0,
            value=random.uniform(5, 20),
            stall_risk=random.random() * 0.8,  # Increased to have higher stall risks
        )
        _counter = [1]
        
        def _build(node: 'Node', depth_remain: int):
            if depth_remain <= 0:
                return
            for i in range(branching):
                child = Node(
                    id=_counter[0],
                    level=node.level + 1,
                    value=random.uniform(2, 10),
                    stall_risk=random.random() * 0.8,  # Higher range
                )
                _counter[0] += 1
                node.children.append(child)
                _build(child, depth_remain - 1)

        _build(root, num_levels - 1)
        return root

if __name__ == '__main__':
    hpvd = HierarchicalPathValueDensity()
    root = hpvd.build_tree(num_levels=3, branching=2, seed=42)
    top_k_results = hpvd.global_selection([root], top_k=3)
    print('Top Paths with Bottleneck Nodes:')
    for result in top_k_results:
        print(result)
    
    # Example access to bottleneck node details
    example_bottleneck_node = hpvd._get_node_by_id(top_k_results[0].bottleneck_node_id, [root])
    print(f'Bottleneck Node Details: {example_bottleneck_node}')
← all inventions · built by the Nowness lab · page generated 31 Jul 2026, 06:47 UTC