
    +j                     T    d dl Zd dlmZmZmZ d dlZd dlmZ d dl	m
Z
  G d d      Zy)    N)ListTupleCallablec                   t    e Zd ZdZddZddZd Zd Zdee	   de
j                  de
j                  fd	Zd
 Zy)IonicMLSimulatorz
    Simulates ionic bonding for ML problems.
    Each ion = (locus_embedding, reciprocal_embedding, charge, data)
    Bonding = weighted sum of reciprocals, then stabilize -> update parameters.
    c                 .    || _         || _        g | _        y )N)d_modellrmemory)selfr	   learning_rates      D/home/per/Documents/ionic_programming_language/ionic_ml_simulator.py__init__zIonicMLSimulator.__init__   s        c                 X    | j                  ||      }| j                  ||      }||||dS )z4Create an ion from a data point (e.g., (x, y) pair).locusrecipchargedata)_encode_locus_compute_reciprocal)r   r   
locus_typer   r   r   s         r   
create_ionzIonicMLSimulator.create_ion   s7     ""44((u5&$OOr   c                 Z    t         j                  j                  | j                        dz  S )z@Map data to an embedding vector (simplified: random projection).皙?nprandomrandnr	   )r   r   r   s      r   r   zIonicMLSimulator._encode_locus   s      yyt||,s22r   c                 Z    t         j                  j                  | j                        dz  S )z:Reciprocal = -gradient of loss w.r.t. parameters (for ML).r   r   )r   r   r   s      r   r   z$IonicMLSimulator._compute_reciprocal   s     
 yyt||,s22r   ionsmodel_parametersreturnc                    t        |      }t        j                  ||f      }t        |      D ]  }t        |      D ]r  }||   d   ||   d   }}t        j                  j                  |      t        j                  j                  |      z  dz   }	t        j                  ||      |	z  |||f<   t  ||j                  dd      z
  }t        j                  |      t        j                  t        j                  |      dd      z  }
|
j                  d      }t        j                  |      }t        |      D ]  \  }}|||   |d   z  z  } || j                  |z  z
  }|S )z
        Perform ionic bonding: combine all reciprocal vectors weighted by complementarity.
        Returns updated parameters (stable state).
        r   g-q=   T)axiskeepdimsr   )r'   )lenr   zerosrangelinalgnormdotmaxexpsummean
zeros_like	enumerater
   )r   r"   r#   nscoresijrirjdenomweights
ion_weightbonded_recipion
new_paramss                  r   bondzIonicMLSimulator.bond&   sO    I1a&!q 	6A1X 6a)477+;B+biinnR.@@EI!vvb"~5q!t6	6 &**!d*;;&&.266"&&.q4#PP \\q\)
}}%56o 	9FAsJqMCL88L	9 &,(>>
r   c                    g }|D ]  \  }}|j                           |t        j                  |t        j                              } ||t        j                  |            }|j	                          t        j                  |j                         D 	cg c]*  }	|	j                  j                         j                         , c}	      }
| j                  |d      }||
d||fd}|j                  |        t        j                  |j                         D 	cg c]*  }	|	j                  j                         j                         , c}	      }| j                  ||      }d}|j                         D ]\  }	|	j                         }t        j                  ||||z    |	j                         j#                  |	j$                        |	_        ||z  }^ j'                         S c c}	w c c}	w )zSOne ionic training step: create ions from batch, bond them to get parameter update.)dtypeinput      ?r   r   )	zero_gradtorchtensorfloat32backwardr   concatenate
parametersgradnumpyflattenr   appendr   rA   numelrC   reshapeshapeitem)r   
data_batchmodelloss_fnr"   xypredlossprM   r   r?   current_paramsr@   offsetsizes                    r   
train_stepzIonicMLSimulator.train_stepA   s     	DAqOOau}}=>D4a1DMMO>>UEUEUEW"X166<<>#9#9#;"XYD&&q'2E "DC!AOCKK	 5K[K[K](^a)?)?)A(^_YYt^4
!!# 	A779D\\*VF4K"@PXXYZY`Y`aAFdNF	 yy{! #Y )_s   /G!
/G&N)   g{Gz?)defaultrE   )__name__
__module____qualname____doc__r   r   r   r   r   dictr   ndarrayrA   r`    r   r   r   r      sG    

P3
3d rzz bjj 6r   r   )rN   r   typingr   r   r   rG   torch.nnnntorch.optimoptimr   ri   r   r   <module>ro      s#     ( (   U Ur   