CoolFace
Apppublic

nicoloddo/x-tree-search

sourceHugging Facecc-by-nc-4.0updated 1y agoView on Hugging Face
1likes
markov_chain.cpython-312.pyc83 linesDownload Raw Back to __pycache__
1�

2�
h���,�dZddlZddlZGd�d�Zy)a�34We will use tensors for the probabilities in the context of the Markov Chain implementation.5This is why:6 71. Preparation for Advanced Operations (e.g. Gradient Computation): If we plan to adjust the transition probabilities in a learning process (e.g., using reinforcement learning or optimization techniques to find optimal transitions), tensors are necessary because they can store gradients. PyTorch automatically computes gradients for tensors during backpropagation, allowing for easy optimization.8 92. GPU Acceleration: PyTorch tensors can be moved to a GPU, which significantly speeds up computations involving large arrays of data. If your Markov Chain scales up to handle very large states or transitions, or if you perform many repeated calculations (such as in simulations or probabilistic inferences), using the GPU can provide performance benefits.10 113. Unified Data Type: By using tensors throughout your project, especially if it heavily relies on PyTorch for other tasks, you maintain consistency in data types. This consistency can simplify the interface between different parts of your project, reducing the need for data type conversions and potentially decreasing bugs or performance issues.12 134. Interoperability: If your Markov Chain is part of a larger system that includes neural networks or other machine learning models built with PyTorch, using tensors ensures that all parts of your system can easily interact. This is particularly relevant when outputs from one part (like a neural network predicting transition probabilities based on state features) are inputs to another (like your Markov Chain).14 155. Batch Operations: PyTorch excels at performing batch operations on tensors, which can be advantageous if you're computing transitions for multiple states simultaneously or if you're aggregating results across multiple runs of a stochastic model. Vectorization can lead to code that is not only faster but also more concise and easier to understand.16 176. Flexibility for Future Enhancements: Even if your current project scope doesn't require complex operations on the transition probabilities, planning for future enhancements by using tensors allows you to add features later without a major overhaul. For example, you could add features like dynamic adjustment of probabilities, integration with machine learning for adaptive learning rates, or real-time analysis of state transitions.18 19 20tl;dr: while using tensors for storing probabilities in a Markov Chain might introduce a bit of overhead for simple scenarios, it offers significant advantages for scalability, performance, and future expansion of your project. It aligns with best practices when using a deep learning framework like PyTorch, preparing your project for potential complexities and integrations.21�Nc�^�eZdZd�ZGd�d�Zed��Zd�Z	d�Zd�Z	d�Z22	d	�Zd23�Zy)�MarkovChainc��i|_y�N)�nodes��selfs �\C:\Users\nicom\Projects\x-tree-search\workspace\x-tree-search\src\structures\markov_chain.py�__init__zMarkovChain.__init__s����24�	�c�(�eZdZdZd�Zd�Zd�Zd�Zy)�MarkovChain.MarkovNodezCMarkovNode objects should be handled through the MarkovChain class.c� �||_i|_yr)�name�connections)r	rs  r25rzMarkovChain.MarkovNode.__init__!s���D�I�!�D��
rc�,�tj|�Sr)�torch�tensor)r	�probabilitys  r26�__format_probabilityz+MarkovChain.MarkovNode.__format_probability+s���<�<��,�,rc�D�|j|�}||j|<yr)�_MarkovNode__format_probabilityr)r	�noders   r27�__add_connectionz'MarkovChain.MarkovNode.__add_connection.s"���3�3�K�@�K�%0�D���T�"rc��ttd�|jD��dz28�dkstjd�yy)Nc3�&K�|]	\}}|���y�wr���.0�_�probs   r29�	<genexpr>z;MarkovChain.MarkovNode.check_consistency.<locals>.<genexpr>5s����@�/?�G�A�t�4�/?���������ư>�)Probabilities for each node must sum to 1F)�abs�sumr�warnings�warnrs r30�check_consistencyz(MarkovChain.MarkovNode.check_consistency3s;���s�@�t�/?�/?�@�@�1�D�E��L��
�
�I�J��MrN)�__name__�31__module__�__qualname__�__doc__rr�_MarkovNode__add_connectionr+rrr32�33MarkovNoders��Q�	�	-�	1�34	rr1c�h�|�}|j�D]\}}|j||��|S)a35        Class method to create a MarkovChain from a dictionary.36        37        The dictionary should be formatted as follows:38        {39            'NodeName': [('OutboundConnectedNodeName1', probability1), ('OutboundConnectedNodeName2', probability2), ...],40            ...41        }42 43        Each key represents a node, and its value is a list of tuples where each tuple consists of a target node name44        and the transition probability to that node. The sum of the probabilities for each list should be 1.45        )�items�add_many_transitions)�cls�46input_dict�mcrrs     r47�	from_dictzMarkovChain.from_dict9s8���U��!+�!1�!1�!3��D�+��#�#�D�+�6�"4��	rc�x�||jvr|j|�|j|<|j|Sr)rr1)r	�	node_names  r48�49__add_nodezMarkovChain.__add_nodeLs4���D�J�J�&�$(�O�O�I�$>�D�J�J�y�!��z�z�)�$�$rc�l�|j|�}|j|�}|j||�y)z�50        Ensures both nodes are in the chain and adds a directed connection.51        Please make sure the connections probabilities add up to 1. 52        N)�_MarkovChain__add_noder0)r	�	from_node�to_noder�	node_from�node_tos      r53�add_transitionzMarkovChain.add_transitionSs0��54�O�O�I�.�	��/�/�'�*���-�-�g�{�Crc���td�|D��std��ttd�|D��dz55�dkstd��|D]\}}|j	|||��y)z�56        Args:57            node (str): The node to which add the connections.58            connections (list of tuples): Outbound connections of the node as tuples: ('OutboundConnectedNodeName1', probability1)59            c3�NK�|]\}}t|ttf����y�wr)�60isinstance�float�intrs   r61r"z3MarkovChain.add_many_transitions.<locals>.<genexpr>bs ����M��g�a��:�d�U�C�L�1��s�#%z#All probabilities should be numbersc3�&K�|]	\}}|���y�wrrrs   r62r"z3MarkovChain.add_many_transitions.<locals>.<genexpr>ds����7�;���4�t�;�r#r$r%r&N)�all�63ValueErrorr'r(rB)r	rr�target_noders     r64r4z MarkovChain.add_many_transitions\sh���M��M�M��B�C�C��3�7�;�7�7�!�;�<�t�C��H�I�I�(3�$�K������k�;�?�)4rc��|jj|�}|jj|�}|r||jvr|j|Syr)r�getr)r	r>r?r@rAs     r65�get_transition_probabilityz&MarkovChain.get_transition_probabilityjsJ���J�J�N�N�9�-�	��*�*�.�.��)����I�$9�$9�9��(�(��1�1�rc���i}|jj�D]Y\}}g}|jj�D]0\}}|j|j|j�f��2|||<�[|S)aw66        Converts the MarkovChain instance into a dictionary format.67 68        Returns:69            dict: A dictionary where each key is a node name, and the value is a list of tuples.70                Each tuple contains (connected_node_name, transition_probability) indicating71                the probability of transitioning from the key node to the connected node.72        )rr3r�appendr�item)r	�output_dictr:r�connections_list�connected_node�probability_tensors       r73�to_dictzMarkovChain.to_dictss�����#�z�z�/�/�1�O�I�t�!��6:�6F�6F�6L�6L�6N�2�� 2� �'�'��)<�)<�>P�>U�>U�>W�(X�Y�7O�&6�K�	�"�
 2��rc	���d}|jj�D]V\}}|d|�d�z
}|jj�D]+\}}||�d|j�d|j	��d�z
}�-�X|S)Nz&Markov Chain States and Probabilities74z75State: z76Connections:77z -> z78 with P = �79)rr3rrrQ)r	�resultr:rrTr!s      r80�__str__zMarkovChain.__str__�s���:��#�z�z�/�/�1�O�I�t��	�)��,<�=�=�F�(,�(8�(8�(>�(>�(@�$����Y�K�t�N�,?�,?�+@�81�4�9�9�;�-�WY�Z�Z��)A� 2��
rN)
r,r-r.rr1�classmethodr8r=rBr4rNrVrZrrr82rrsO�����4����$%�(�D�@����(rr)r/r)rrrrr83�<module>r\s���(��u�ur