In [1]:
#import os
#os.chdir('/home/aotsvetkov/programms/physlearn')

In [2]:
#os.getcwd()

In [3]:
# Необходмые команды импорта.
import sys
sys.path.append('../../physlearn/')
sys.path.append('../../source')

from physlearn.NeuralNet import NeuralNet
from physlearn.Optimizer import Optimizer
import numpy
import math
import matplotlib.pyplot as plt
from matplotlib import rc
import tensorflow as tf
import scipy
from IPython.display import clear_output

import pickle

%matplotlib inline


D:\Anaconda\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

In [4]:
#rc('text', usetex=True)

In [5]:
def save_data(folder_name, data, file_name):
    cur_dir = os.getcwd()
    try:
        os.mkdir(folder_name)
    except OSError:
        pass
    os.chdir(folder_name)
    with open(file_name, 'wb') as f:
        pickle.dump(data, f)
    
    os.chdir(cur_dir)

In [6]:
k = 1.0
m = 1.0
omega_sq = k / m
omega = math.sqrt(omega_sq)
x0 = 0.0
speed0 = 1.0

In [7]:
if (speed0 != 0) and (x0 != 0):
    phi0 = math.atan(x0 * omega / speed0)
    A = x0 / (math.sin(phi0))
elif (x0 == 0.0) and (speed0 != 0):
    phi0 = 0.0
    A = speed0 / (omega * math.cos(phi0))
elif (speed0 == 0) and (x0 != 0):
    phi0 = math.pi / 2
    A = x0 / (math.sin(phi0))
else:
    phi0 = 0.0
    A = 0

In [8]:
begin_hamiltonian = (k * (x0 ** 2) / 2) + (m * (speed0 ** 2) / 2)

In [9]:
amount_of_points = 30
batch_size = 30
min_time = 0
max_time = 2 * math.pi / math.sqrt(omega_sq)

In [10]:
t_list = numpy.linspace(min_time, max_time, amount_of_points).reshape((1, amount_of_points))
x_expected = A * numpy.sin(omega * t_list + phi0)

In [11]:
t_cv = numpy.linspace(min_time, max_time, 1000).reshape((1, 1000))

In [12]:
net = NeuralNet(-5, 5)

In [13]:
net.add_input_layer(1)
net.add(10, net.sigmoid)
net.add_output_layer(1, net.linear)

In [14]:
net.compile()

In [15]:
dim = net.return_unroll_dim()

In [16]:
def return_cost():
    y = net.return_graph()
    speed = tf.gradients(y, net.x)[0]
    acc = tf.gradients(speed, net.x)[0]
    #print(y)
    equation_part = tf.reduce_sum((acc + omega_sq * y) ** 2)
    begin_part = ((y[0][0] - x0) ** 2) + ((speed[0][0] - speed0) ** 2)
    current_hamiltonian = (k * (y ** 2) / 2) + (m * (speed ** 2) / 2)
    hamilton_part = tf.reduce_sum((current_hamiltonian - begin_hamiltonian) ** 2)
    cost_tensor = begin_part + (1 / amount_of_points) * ((equation_part) + hamilton_part)
    return cost_tensor

In [17]:
def cost(params):
    net.roll_matrixes(params)
    return return_cost()

In [18]:
opt = Optimizer()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-d0135b2612db> in <module>()
----> 1 opt = Optimizer()

TypeError: 'module' object is not callable

In [ ]:
opt.params_dict['number_of_individuals'] = 433
opt.params_dict['sess'] = net.sess
opt.params_dict['placeholder'] = net.x
opt.params_dict['x'] = t_list

In [ ]:
res = opt.optimize(cost, dim, optimizer=opt.diff_evolution_tf, max_time=200, min_func_value=1e-4)
net.roll_matrixes(res.x)

In [ ]:
res

In [ ]:
x_pred = net.run(t_cv)
plt.plot(t_cv[0], x_pred[0])
plt.plot(t_list[0], x_expected[0], 'x')

In [ ]:
plt.plot(res.cost_list)

In [ ]:
time_list = res.misc['time_list']

In [ ]:
speed = [i / time_list[i] for i in range(1, len(time_list))]
i_list = [i for i in range(1, len(time_list))]

In [ ]:
plt.plot(i_list, speed)

In [ ]:
numpy.arange(5, 448, 16)