
    h
                        S r  " S S5      r\S:X  GaY  \" S5        \" 5       r\" S5        \" S5        \R                  S/SS	9r\" S
\" \S   5       35        \" S\S    35        \" S\S   S 35        \" S5        \" S5        / SQr\R                  S\5      r\" S\S   S 35        \" S\S   S 35        \" S\S    35        \" S5        \R                  SS5      r\" S\S   S 35        \" S\S    35        \" S 5        \R                  S!S"5      r\" S#\S$    35        \" S%\S&    35        \" S'5        \" S(5        \" S)5        \" S*5        \" S+5        \" S,5        \" S-5        g.g.)/zM
Complex Inference Engine - Multi-step reasoning and probabilistic inference
c                   N    \ rS rSrS rSS jrS rS rS rS r	S r
S	 rS
 rSrg)ComplexInference   c                      / U l         0 U l        g )N)inference_historybelief_network)selfs    G/home/james-whalen/eden-agi-project/core/reasoning/complex_inference.py__init__ComplexInference.__init__   s    !#     c                     / nUn[        U5       H(  nU R                  U5      nUR                  U5        U/nM*     UUU(       a  US   OSS[        U5      -  S.$ )z$Perform multi-step logical inferenceno_conclusiong?)premisesstepsfinal_conclusion
confidence)rangeapply_inference_rulesappendlen)r   r   r   conclusionscurrentstepnext_conclusions          r	   multi_step_inference%ComplexInference.multi_step_inference
   sj    %LD"88AO/&'G	 ! ! 3>BO[!11	
 	
r   c                    U(       d  g[        U[        5      (       a  US   R                  5       OUR                  5       nSU;   a,  SU;   a&  UR                  S5      nUS   R	                  5       nU$ SU;   d  SU;   a  gS	U;   d  S
U;   a  gg)zApply logical inference rulesno_input    ifthen   zleads tocauseszdownstream effect occursalleveryzapplies to specific instanceszlogical consequence follows)
isinstancelistlowersplitstrip)r   
statements	statementpartsconsequences        r	   r   &ComplexInference.apply_inference_rules   s    -7
D-I-IJqM'')zO_O_Oa	 99!4OOF+E(..*K "h)&;- II!52,r   c                 l   SnSnSnU Hu  nSUR                  5       ;   d  SUR                  5       ;   a  US-  nUS-  nM7  SUR                  5       ;   d  SUR                  5       ;   a  US	-  nUS
-  nMk  US-  nUS-  nMw     [        SX4-   5      nUU[        U5      UUUS:  a  SS.$ US:  a  SS.$ SS.$ )zReason under uncertainty      ?r    strongveryg?r#   weakslightg?333333?g?gffffff?ffffff?highmoderatelow)eventprior_probabilityevidence_piecesevidence_strengthposterior_probabilityr   )r)   minr   )r   r<   evidencepriorconfidence_adjustmentsr?   piece	posteriors           r	   probabilistic_reasoning(ComplexInference.probabilistic_reasoning3   s      "#E5;;=(Fekkm,C&#-&!Q&!5;;=(H,E&$.&!S(!&#-&!S(!  e<=	 !&"8}!2%.$-O&
 	
 HQSV
 	
 ]b
 	
r   c                 P    U R                  X5      nUS:  a  UUUSUS.$ UUUSSS.$ )zReason from similar casesr2   zsimilar outcome expected)sourcetarget
similarity	inferencer   z&cases too different for strong analogyr7   )find_similarities)r   source_casetarget_casesimilaritiess       r	   reason_by_analogy"ComplexInference.reason_by_analogyR   sR     --kG #%%*7*  &%*E! r   c                 `   [        [        U5      R                  5       R                  5       5      n[        [        U5      R                  5       R                  5       5      nU(       a  U(       d  g[	        UR                  U5      5      n[	        UR                  U5      5      nUS:  a  XV-  $ S$ )zFind similarity between casesg        r    )setstrr)   r*   r   intersectionunion)r   case1case2words1words2overlaptotals          r	   rN   "ComplexInference.find_similaritiesi   s     SZ%%'--/0SZ%%'--/0Vf))&12FLL()"'!)w44r   c                 0    UUSU R                  X5      SS.$ )z7Reason about what would happen if things were differentzoutcomes would differcounterfactual_inference)actualcounterfactual
comparisonkey_differencestype)identify_differences)r   actual_eventalternatives      r	   counterfactual_reasoning)ComplexInference.counterfactual_reasoningw   s(     #)1#88S.
 	
r   c                     SSSS.$ )z*Identify key differences between scenariosz(alternative conditions in counterfactualzoutcomes differ from actualzcausal chain altered)changed_conditionsexpected_divergence	reasoning )r   	scenario1	scenario2s      r	   rg   %ComplexInference.identify_differences   s     #M#@/
 	
r   c                 :    [        U5      S:  a  USSSS.$ USSS.$ )	z)Infer unobserved causes from observationsr#   zcommon underlying factorzpattern suggests hidden causer8   )observationsinferred_causero   r   zinsufficient datar7   )ru   rv   r   )r   )r   ru   s     r	   infer_hidden_causes$ComplexInference.infer_hidden_causes   s<     |q  ,"<<!	  )1
 	
r   )r   r   N)   )__name__
__module____qualname____firstlineno__r
   r   r   rG   rR   rN   rj   rg   rw   __static_attributes__rp   r   r	   r   r      s/    !
$-.
>.5


r   r   __main__zCOMPLEX INFERENCE TESTu   
🧠 Multi-step inference:z,   Premise: 'If it rains then ground is wet'zif it rains then ground is wetry   )r   z
   Steps: r   z
   Final: r   z   Confidence: r   z.0%u   
📊 Probabilistic reasoning:z   Event: 'System will succeed')zstrong positive indicatorszteam is experiencedzresources availablezsystem successz
   Prior: r=   z   Posterior: r@   u   
🔗 Analogical reasoning:z-successful product launch with good marketingz(new product launch with strong marketingz   Similarity: rL   z   Inference: rM   u   
🤔 Counterfactual reasoning:ztook highway, arrived in 1 hourztook backroadsz	   Type: rf   z   Comparison: rd   u   
🧠 Eden can now:z!   - Multi-step logical inferencez   - Probabilistic reasoningz   - Analogical reasoningz   - Counterfactual thinkingz   - Infer hidden causesu"   
✅ COMPLEX INFERENCE OPERATIONALN)__doc__r   rz   printrM   r   resultr   rB   rG   rR   rj   rp   r   r	   <module>r      s  S
 S
j z	
"# "I 

()	
89++-M,NVW+XF	Js6'?+,
-.	Jv012
34	OF<05
67 

+,	
+,[H../?JF	Jv1237
89	N6"9:3?
@A	OF<01
23 

()((72F 
OF<05
67	N6+./
01 

,-//)F 
IfVn%
&'	OF<01
23	
 !	
-.	
()	
%&	
()	
$%	
/0_ r   