def test_GetWidthTestTrue(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.assertEqual(self.layer.get_width(), self.width_layer)
class CalculationTest(unittest.TestCase): def test_GetPressureNow(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.height_rocket = 12000.0 # [m] self.height_layer_below = 0.0 # [m] self.assertEqual(self.layer.get_pressure_now(self.height_rocket, self.height_layer_below), 21139.080525291483) def test_GetTemperatureNow(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.height_rocket = 12000.0 # [m] self.height_layer_below = 0.0 # [m] self.assertEqual(self.layer.get_temperature_now(self.height_rocket, self.height_layer_below), 380.5) def test_GetDensityNow(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.height_rocket = 12000.0 # [m] self.height_layer_below = 0.0 # [m] self.assertEqual(self.layer.get_density_now(self.height_rocket, self.height_layer_below), 0.19351737724422885)
def __init__(self, layers_size): self.layer_size = layers_size # tworzenie sieci dla zadanej ilości warstw o określonej wielkości self.network = [] self.network.append(Layer(layers_size[0], layers_size[0], first_layer=True)) for i in range(len(layers_size) - 2): self.network.append(Layer(layers_size[i], layers_size[i + 1])) self.network.append(Layer(layers_size[-2], layers_size[-1], last_layer=True))
def test_SetPressureLow(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer_set = Layer(0.0, 0.0, 0.0, 0.0) self.layer_set.set_pressure_low(self.pressure_low) self.assertEqual(self.layer_set.get_pressure_low(), self.pressure_low)
def test_GetTemperatureNow(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.height_rocket = 12000.0 # [m] self.height_layer_below = 0.0 # [m] self.assertEqual(self.layer.get_temperature_now(self.height_rocket, self.height_layer_below), 380.5)
def test_GetDensityNow(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.height_rocket = 12000.0 # [m] self.height_layer_below = 0.0 # [m] self.assertEqual(self.layer.get_density_now(self.height_rocket, self.height_layer_below), 0.19351737724422885)
def Forward(self, x): T = len(x) # total number of time steps layers = [] prev = np.zeros(self.nHid) for t in range(T): layer = Layer() input = np.zeros(self.wordDim) input[x[t]] = 1 layer.forward(input, prev, self.U, self.W, self.V) prev = layer.s layers.append(layer) return layers
def __init__(self, dimensions, loss_func, learning_rate=0.02): assert (isinstance(dimensions, list)) assert (isinstance(loss_func, LossFunction)) assert (isinstance(learning_rate, np.float)) self.lossFunc = loss_func super().__init__() self.nlayers = len(dimensions) self.xdim = dimensions[0][0] self.ydim = dimensions[-1][1] self.learning_rate = learning_rate self.layers = [] oldny = self.xdim # initialize all the layers for lay in dimensions: assert (len(lay) == 3) nx, ny, actfunc = lay assert (isinstance(nx, int)) assert (oldny == nx) # do the dimensions check? assert (isinstance(ny, int)) assert (isinstance(actfunc, ActivationFunction)) self.layers.append(Layer(nx, ny, actfunc)) oldny = ny return
class WidthTest(unittest.TestCase): def test_GetWidthTestTrue(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer = Layer(self.width_layer, self.temp_gradient, self.temp_low, self.pressure_low) self.assertEqual(self.layer.get_width(), self.width_layer) def test_SetWidthTest(self): self.width_layer = 18000.0 # [m] self.temp_gradient = 6.5 / 1000.0 # [K/m] self.temp_low = 30.0 + 272.5 # [K] self.pressure_low = 101325.0 # [Pa] self.layer_set = Layer(0.0, 0.0, 0.0, 0.0) self.layer_set.set_width(self.width_layer) self.assertEqual(self.layer_set.get_width(), self.width_layer)
def __init__(self,size,walls=None): self.size=size*2-Vector.Vector2(1,1) self.basesize=size self.objs=set() self.layers=[Layer(0,"Walls"),EntityLayer(0,"Dots"),EntityLayer(0,"Enemies"),EntityLayer(0,"Players")] self.ldict = {l.name: l for l in self.layers} if walls: self["Walls"].objs=walls self["Walls"].fix(Vector.zero,self.size,self) else: self.regen() self.rs=self.scale*16+16
def main(): """ Declare your planet, atmosphere with its layers, the rocket with its parts, the data object and the time delta here and run the simulation :return: """ # Setup here sim_max_step = 100000 # Maximum time steps the simulation should run sim_time_step = 0.1 # [s] Timestep the simulation uses earth = Planet(pos_planet=[0.0, 0.0], mass_planet=5.974e+24, radius_planet=12756.32 / 2.0 * 1000) earth_radius = earth.get_radius() troposphere = Layer(pressure_low=101325.0, width_layer=18000.0, temp_gradient=-0.0065, temp_low=288.15) stratosphere = Layer(pressure_low=16901.37, width_layer=32000.0, temp_gradient=0.0031875, temp_low=171.15) mesosphere = Layer(pressure_low=1.02, width_layer=30000.0, temp_gradient=-0.003333, temp_low=273.15) thermosphere = Layer(pressure_low=0.04, width_layer=420000.0, temp_gradient=-0.000405, temp_low=173.15) exosphere = Layer(pressure_low=0.0, width_layer=9999999999.0, temp_gradient=0.0, temp_low=3.0) earth_atmosphere = Atmosphere() earth_atmosphere.add_layer(troposphere) earth_atmosphere.add_layer(stratosphere) earth_atmosphere.add_layer(mesosphere) earth_atmosphere.add_layer(thermosphere) earth_atmosphere.add_layer(exosphere) # Set maximum propellant mass A150_mass_propellant = 484.8076 # Calculate A150 one tank: sim_flight_name = "A150_OneTank" sim_data = Data( data_file="./Results_2/{}/Results_Data.csv".format(sim_flight_name)) sim_data.name = "Einstufig" A150_payload = 0.0 # kg moegliche Nutzlast A150_nose_cone = RocketPart(mass_part=7.0 + 2.31336 + A150_payload, surface_part=0.1140, drag_coefficient_part=0.27) A150_liquid_tank = Tank(mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=A150_mass_propellant, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150 = Rocket(pos=[earth_radius, 0.0], velocity=[0.0, 0.0], acceleration=[0.0, 0.0]) A150_booster = Tank(mass_part=28.1232, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=117.6074, mass_change_tank=47.1733, velocity_exhaust_tank=1747.6074, surface_nozzle=0.0434, pressure_nozzle=101325.0) A150.append_part(A150_nose_cone) A150.append_part(A150_liquid_tank) A150.append_part(A150_booster) A150.set_mass() A150.set_surface() sim_flight = Flight(sim_time_step, earth, A150, earth_atmosphere, sim_data) # Running the simulation run_sim(sim_flight, sim_flight_name, sim_max_step, sim_time_step) # Calculate A150 two tanks 1 sim_flight_name = "A150_RefTankA" sim_data = Data( data_file="./Results_2/{}/Results_Data.csv".format(sim_flight_name)) sim_data.name = "Stufe 1 leer" A150_payload = 0.0 # kg moegliche Nutzlast A150_nose_cone = RocketPart(mass_part=7.0 + 2.31336 + A150_payload, surface_part=0.1140, drag_coefficient_part=0.27) A150_liquid_tank_one = Tank(mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=A150_mass_propellant, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150_liquid_tank_two = Tank(mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=0, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150 = Rocket(pos=[earth_radius, 0.0], velocity=[0.0, 0.0], acceleration=[0.0, 0.0]) A150_booster = Tank(mass_part=28.1232, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=117.6074, mass_change_tank=47.1733, velocity_exhaust_tank=1747.6074, surface_nozzle=0.0434, pressure_nozzle=101325.0) A150.append_part(A150_nose_cone) A150.append_part(A150_liquid_tank_one) A150.append_part(A150_liquid_tank_two) A150.append_part(A150_booster) A150.set_mass() A150.set_surface() sim_flight_a = Flight(sim_time_step, earth, A150, earth_atmosphere, sim_data) # Running the simulation run_sim(sim_flight_a, sim_flight_name, sim_max_step, sim_time_step) # Calculate A150 two tanks 1 sim_flight_name = "A150_RefTankB" sim_data = Data( data_file="./Results_2/{}/Results_Data.csv".format(sim_flight_name)) sim_data.name = "Stufe 2 leer" A150_payload = 0.0 # kg moegliche Nutzlast A150_nose_cone = RocketPart(mass_part=7.0 + 2.31336 + A150_payload, surface_part=0.1140, drag_coefficient_part=0.27) A150_liquid_tank_one = Tank(mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=0, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150_liquid_tank_two = Tank(mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=A150_mass_propellant, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150 = Rocket(pos=[earth_radius, 0.0], velocity=[0.0, 0.0], acceleration=[0.0, 0.0]) A150_booster = Tank(mass_part=28.1232, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=117.6074, mass_change_tank=47.1733, velocity_exhaust_tank=1747.6074, surface_nozzle=0.0434, pressure_nozzle=101325.0) A150.append_part(A150_nose_cone) A150.append_part(A150_liquid_tank_one) A150.append_part(A150_liquid_tank_two) A150.append_part(A150_booster) A150.set_mass() A150.set_surface() sim_flight_b = Flight(sim_time_step, earth, A150, earth_atmosphere, sim_data) # Running the simulation run_sim(sim_flight_b, sim_flight_name, sim_max_step, sim_time_step) # Searching for optimal tank setup divisor = 2 count = 0 sim_opt_max_step = 40 propellant_step = A150_mass_propellant / sim_opt_max_step while count < sim_opt_max_step: sim_flight_name = "A150_TankSetup_{0:03d}".format(count) sim_data = Data(data_file="./Results_2/{}/Results_Data.csv".format( sim_flight_name)) A150_mass_propellant_tank_one = propellant_step * count A150_mass_propellant_tank_two = A150_mass_propellant - propellant_step * count sim_data.name = "Stufe 1: {0:.2f} kg Stufe 2: {1:.2f} kg".format( A150_mass_propellant_tank_two, A150_mass_propellant_tank_one) A150 = Rocket(pos=[earth_radius, 0.0], velocity=[0.0, 0.0], acceleration=[0.0, 0.0]) A150_liquid_tank_one_split = Tank( mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=A150_mass_propellant_tank_one, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150_liquid_tank_two_split = Tank( mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=A150_mass_propellant_tank_two, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150_booster = Tank(mass_part=28.1232, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=117.6074, mass_change_tank=47.1733, velocity_exhaust_tank=1747.6074, surface_nozzle=0.0434, pressure_nozzle=101325.0) A150.append_part(A150_nose_cone) A150.append_part(A150_liquid_tank_one_split) A150.append_part(A150_liquid_tank_two_split) A150.append_part(A150_booster) A150.set_mass() A150.set_surface() sim_flight_p = Flight(sim_time_step, earth, A150, earth_atmosphere, sim_data) # Running the simulation run_sim(sim_flight_p, sim_flight_name, sim_max_step, sim_time_step) count += 1 # Calculate A150 one tank, no booster: sim_flight_name = "A150_NoBooster" sim_data = Data( data_file="./Results_2/{}/Results_Data.csv".format(sim_flight_name)) sim_data.name = "Einstufig ohne Booster" A150_payload = 0.0 # kg moegliche Nutzlast A150_nose_cone = RocketPart(mass_part=7.0 + 2.31336 + A150_payload, surface_part=0.1140, drag_coefficient_part=0.27) A150_liquid_tank = Tank(mass_part=116.1216, surface_part=0.0, drag_coefficient_part=0.0, mass_propellant=A150_mass_propellant, mass_change_tank=9.394, velocity_exhaust_tank=1417.32, surface_nozzle=0.0275, pressure_nozzle=101325.0) A150 = Rocket(pos=[earth_radius, 0.0], velocity=[0.0, 0.0], acceleration=[0.0, 0.0]) A150.append_part(A150_nose_cone) A150.append_part(A150_liquid_tank) A150.set_mass() A150.set_surface() sim_flight_nb = Flight(sim_time_step, earth, A150, earth_atmosphere, sim_data) # Running the simulation run_sim(sim_flight_nb, sim_flight_name, sim_max_step, sim_time_step) print "Optimization has ended after {} iterations, Results available".format( count - 1)
def test_suite(): """ Runs all the tests available: - checks the derivative dA - checks the derivative dW - checks the derivative db ... all of the above for the Activation Functions - Sigmoid - TanH - ReLU - LeakyRelu - Softplus """ n_x = 7 n_y = 4 samples = 3 afs = [ afuncs.Sigmoid(), afuncs.TanH(), afuncs.ReLU(), afuncs.LeakyRelu(), afuncs.Softplus() ] for af in afs: res = True lay = Layer(n_x, n_y, af) x = np.random.randn(n_x, samples) y = lay.get_y(x) d_x = np.ones((n_y, samples)) d_a, d_w, d_b = lay.get_grad(d_x) new_x = x.reshape(n_x * samples, 1) num = grad_num(new_x, test_x, lay) err = eval_err(num, d_a.reshape(n_x * samples, 1), "error in X") if not err: res = False print("dA: " + str(d_a.shape)) print(d_a) print("num dA: " + str(num.reshape(n_x, samples).shape)) print(num.reshape(n_x, samples)) new_w = lay.W.reshape(n_x * n_y, 1) num = grad_num(new_w, testd_w, lay) / samples err = eval_err(num, d_w.reshape(n_x * n_y, 1), "error in W") if not err: res = False print("dW: " + str(d_w.shape)) print(d_w) print("num dW: " + str(num.reshape(n_y, n_x).shape)) print(num.reshape(n_y, n_x)) new_b = lay.b num = grad_num(new_b, testd_b, lay) / samples err = eval_err(num, d_b, "error in b") if not err: res = False print("db: " + str(d_b.shape)) print(d_b) print("num dA: " + str(num.shape)) print(num) if res: print('All tests on Layers ran successfully') return res