Beispiel #1
0
def new_sample() -> list:
    """
    Generate a sample data set.

    :return: The generated data set.
    """
    return pp.generate_train_set(
        n=app_config.PREF_NUM_DATA,
        second_order_coil=Coil(0.2, 100),
        first_order_coil=Coil(0.5, 50),
        delta_volt_per_seconds=100)
Beispiel #2
0
def generate_train_set(n: int,
                       first_order_coil: Coil,
                       second_order_coil: Coil,
                       delta_volt_per_seconds: float) -> list:
    """
    Randomly generate data set in case of a coil derives current
    to three other coils that orthogonally aligned in 3-dimensional space.

    :param n: The number of samples in data set.
    :param first_order_coil: The first-order coil that derives current.
    :param second_order_coil: The second-order coil derived by the first-order coil.
    :param delta_volt_per_seconds: The change of voltage in a second in volts.

    :return: The generated data set.
    """
    if n <= 0:
        raise IndexError('The number of samples must exceed zero.')

    # Deepcopy the first-order coil to translate and rotate.
    first = first_order_coil.__copy__()

    # Deepcopy the second-order coil in each three orthogonal directions.
    coil_x = Coil(second_order_coil.area, second_order_coil.num_wind, up_vector=torch.FloatTensor([1, 0, 0]))
    coil_y = Coil(second_order_coil.area, second_order_coil.num_wind, up_vector=torch.FloatTensor([0, 1, 0]))
    coil_z = Coil(second_order_coil.area, second_order_coil.num_wind, up_vector=torch.FloatTensor([0, 0, 1]))

    # Randomly generate the sample in range:
    inputs, outputs = [], make_outputs(n)
    for datum in outputs:
        first.position = datum[:3]
        first.up_vector = datum[3:]

        # Derive the three second-order coils and calculates the current.
        derived_x = -derive_current(first, coil_x, delta_voltage=delta_volt_per_seconds, delta_time=1)
        derived_y = -derive_current(first, coil_y, delta_voltage=delta_volt_per_seconds, delta_time=1)
        derived_z = -derive_current(first, coil_z, delta_voltage=delta_volt_per_seconds, delta_time=1)

        # Derived current as input
        inputs.append((derived_x, derived_y, derived_z))

    return [torch.FloatTensor(inputs), outputs]
Beispiel #3
0
def render():
    tm.update()
    update(tm.get_delta())
    lookAt()

    # Predict position, angle
    angle_r = torch.deg2rad(angle)
    up = Coil.calc_up_vector(angle_r[0], angle_r[1])

    pred = predictor.DoubleModelPredictor()
    pred = predictor.TorchPredictor()
    pred_pos, pred_up = pred.inference(position, up)
    pred_angle = Coil.calc_angles(pred_up)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # Origin anchor
    drawAnchor(colors_rgb)

    # Real position
    glPushMatrix()
    glTranslatef(position[0], position[1], position[2])
    glRotatef(angle[0], 0, 1, 0)
    glRotatef(angle[1], 1, 0, 0)
    drawAnchor(colors_white)
    glPopMatrix()

    # Predicted position
    glPushMatrix()
    glTranslatef(pred_pos[0], pred_pos[1], pred_pos[2])
    glRotatef(pred_angle[0], 0, 1, 0)
    glRotatef(pred_angle[1], 1, 0, 0)
    drawAnchor(colors_light_gray)
    glPopMatrix()

    print(position, pred_pos)

    glutSwapBuffers()
Beispiel #4
0
    def inference(
        self, position: torch.FloatTensor, up_vector: torch.FloatTensor
    ) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
        pos_model: MLPRegressor = self.model[0]
        up_model: MLPRegressor = self.model[1]

        first_order_coil = Coil(0.5,
                                50,
                                position=position,
                                up_vector=up_vector)
        currents = torch.FloatTensor([
            -derive_current(first_order_coil,
                            second_order_coil,
                            delta_voltage=100,
                            delta_time=1)
            for second_order_coil in __second_order_coils__
        ]).reshape(1, -1)

        return pos_model.predict(currents)[0], up_model.predict(currents)[0]
Beispiel #5
0
    def inference(
        self, position: torch.FloatTensor, up_vector: torch.FloatTensor
    ) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
        model: PositionRegression = self.model

        first_order_coil = Coil(0.5,
                                50,
                                position=position,
                                up_vector=up_vector)
        currents = torch.FloatTensor([
            -derive_current(first_order_coil,
                            second_order_coil,
                            delta_voltage=100,
                            delta_time=1)
            for second_order_coil in __second_order_coils__
        ]).view(-1, 3)

        pred = model.predict(currents)[0].tolist()
        return torch.FloatTensor(
            pred[:3]) * app_config.PREF_SPACE_SIZE, torch.FloatTensor(pred[3:])
    pytha_x = np.cos(1.0472)
    pytha_y = np.sin(1.0472)
    d_x = d * pytha_x
    d_y = d * pytha_y
    nb_elem = int((c * r) - round(r / 2))

    for i in range(r):
        if i % 2 == 0:
            d_x = 0
            if i != 0:
                """Minus one coil according to theory"""
                coils_list.pop()
        else:
            d_x = d * pytha_x
        for j in range(c):
            coil = Coil((d * j + d_x), (d_y * i), 0, rad_a, rad_b, coil_definition)
            coils_list.append(coil)

    """If preset on a cylinder"""
elif preset == 1 and rad_c != "0":
    rad_c *= 0.01
    rad_a *= 0.01
    rad_b *= 0.01
    d = 0.75 * 2 * rad_a
    """60 deg = 1.0472 rad"""
    pytha_x = np.cos(1.0472)
    pytha_y = np.sin(1.0472)
    d_x = d * pytha_x
    d_y = d * pytha_y
    nb_elem = int((c * r))
    pytha_x = np.cos(1.0472)
    pytha_y = np.sin(1.0472)
    d_x = d * pytha_x
    d_y = d * pytha_y
    nb_elem = int((c * r) - round(r / 2))

    for i in range(r):
        if i % 2 == 0:
            d_x = 0
            if i != 0:
                """Minus one coil according to theory"""
                coils_list.pop()
        else:
            d_x = d * pytha_x
        for j in range(c):
            coil = Coil((d * j + d_x), (d_y * i), 0, rad_a, rad_b,
                        coil_definition)
            coils_list.append(coil)
    """If preset on a cylinder"""
elif preset == 1 and rad_c != "0":
    rad_c *= 0.01
    rad_a *= 0.01
    rad_b *= 0.01
    d = 0.75 * 2 * rad_a
    """60 deg = 1.0472 rad"""
    pytha_x = np.cos(1.0472)
    pytha_y = np.sin(1.0472)
    d_x = d * pytha_x
    d_y = d * pytha_y
    nb_elem = int((c * r))

    for i in range(r):
Beispiel #8
0
import pickle

from typing import Tuple

import app_config
import torch
from sklearn.neural_network import MLPRegressor
from coil import Coil, derive_current
from model import PositionRegression

__second_order_coils__ = [
    Coil(0.2, 100, up_vector=torch.FloatTensor((1, 0, 0))),
    Coil(0.2, 100, up_vector=torch.FloatTensor((0, 1, 0))),
    Coil(0.2, 100, up_vector=torch.FloatTensor((0, 0, 1))),
]


class Predictor:
    def __init__(self):
        with open(app_config.PATH_MODEL, 'rb') as model_file:
            self.model = pickle.load(model_file)

    def inference(self, position: torch.FloatTensor,
                  up_vector: torch.FloatTensor) -> Tuple[torch.FloatTensor]:
        pass


class SingleModelPredictor(Predictor):
    def inference(
        self, position: torch.FloatTensor, up_vector: torch.FloatTensor
    ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: