Ejemplo n.º 1
0
def createData(numProblems, time_steps, withAnimation, withHeader):
    # Constants and variables to track program performance
    orbital_periods = 3
    numInvalid = 0
    totalCalcTime = 0

    # Prepare the master file
    with open("outputs/master.csv", 'w', newline='') as csvFile:
        header = ["Trial", "Path", "Mass 1", "Mass 2", "Mass 3", "Mass 4", "x Position 1", "y Position 1", "x Position 2", "y Position 2", "x Position 3", "y Position 3", "x Position 4", "y Position 4", "x Velocity 1", "y Velocity 1", "x Velocity 2", "y Velocity 2", "x Velocity 3", "y Velocity 3", "x Velocity 4", "y Velocity 4"]
        csvWriter = csv.writer(csvFile)
        if withHeader:
            csvWriter.writerow(header)

    dataset = []

    for n in range(numProblems):
        print("Trial " + str(n+1))
        calcTime = 0
        valid = False
        
        # While loop used to keep generating problems until a valid one is solved
        while not valid:
            # Generate random masses in range 0 - 2
            m1 = newMass()
            m2 = newMass()
            m3 = newMass()
            m4 = newMass()

            p = getPositions()
            v = getVelocities()

            problem = FourBodyProblem(orbital_periods, time_steps, m1, m2, m3, m4, p[0], p[1], p[2], p[3], v[0], v[1], v[2], v[3])

            # Track time for performance comparisons
            s = time.perf_counter()
            output = problem.calculate_trajectories()
            e = time.perf_counter()
            calcTime = e - s

            # Ensure validity
            valid = testValidity(output)

            # Print to screen to notify of an invalid solution
            # Add one to the tracker variable
            if not valid:
                print("Solution invalid, recalculating...")
                numInvalid += 1

        totalCalcTime += calcTime

        if withAnimation:
            problem.display_trajectories(animated=True, save_animation=False)

        # Problem can be written to csv file if the problem needs to be checked for matching the master file
        #problem.to_csv("outputs/num" + str(n) + ".csv", withHeader=withHeader)

        # Prepare row to be written to master file
        problemArgs = [str(n+1), "num" + str(n) + ".csv", str(m1), str(m2), str(m3), str(m4), str(p[0][0]), str(p[0][1]), str(p[1][0]), str(p[1][1]), str(p[2][0]), str(p[2][1]), str(p[3][0]), str(p[3][1]), str(v[0][0]), str(v[0][1]), str(v[1][0]), str(v[1][1]), str(v[2][0]), str(v[2][1]), str(v[3][0]), str(v[3][1])]

        with open('outputs/master.csv', 'a', newline='') as csvFile:
            csvWriter = csv.writer(csvFile)
            csvWriter.writerow(problemArgs)

        # Add the new problem to the dataset array, making sure only to add position values
        if (len(dataset) == 0):
            dataset = output[:,0:8]
        else:
            dataset = np.append(dataset, output[:,0:8], axis=0)

    # Save the dataset array to a npy file for transferring to ml program
    np.save("outputs/trainingOutputs.npy", dataset)

    # Calculate and print program performance statistics
    avgCalcTime = ( totalCalcTime / numProblems ) * 1000
    print()
    print("Done Generating Solutions")
    print("=========================")
    print("Number Generated: " + str(numProblems))
    print("Number Invalid: " + str(numInvalid))
    print("Total Calculation Time (s): " + str(totalCalcTime))
    print("Avg Calculation Time (ms): " + str(avgCalcTime))

#createData(500, time_steps=1500, withAnimation=False, withHeader=False)
Ejemplo n.º 2
0
def main():
    classical_solution = FourBodyProblem(
        3, 1000, 1.493274915, 0.777043742, 0.69905188, 1.644425625,
        [-0.2235866560691493, 0.4662489456089146],
        [0.45460058216449073, 1.856775210402708],
        [-0.581630066079448, 2.652030222251113],
        [-1.0887887921718722, 1.8916735993939726],
        [-0.001983479092496618, -0.023803719445120764],
        [-0.04168724914664399, -0.022727286512001968],
        [-0.0006659848704990909, -0.01954219193498398],
        [0.0222475650915121, -0.019197877602321424])

    classical_solution.calculate_trajectories()

    classical_solution.display_trajectories(animated=False,
                                            save_animation=False)

    model = Sequential([
        Dense(256, activation='relu', input_shape=[21]),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(256, activation='relu'),
        Dense(8),
    ])

    model.compile(optimizer='SGD',
                  loss='mean_squared_error',
                  metrics=['accuracy'])

    # Load the weights on the model
    model.load_weights("assets/sgd_checkpoints/cp.ckpt")

    time_span = np.linspace(0, 3, 1000)

    ml_outputs = []

    for n in time_span:
        ml_input = [
            n, 1.493274915, 0.777043742, 0.69905188, 1.644425625,
            -0.2235866560691493, 0.4662489456089146, 0.45460058216449073,
            1.856775210402708, -0.581630066079448, 2.652030222251113,
            -1.0887887921718722, 1.8916735993939726, -0.001983479092496618,
            -0.023803719445120764, -0.04168724914664399, -0.022727286512001968,
            -0.0006659848704990909, -0.01954219193498398, 0.0222475650915121,
            -0.019197877602321424
        ]
        ml_input = np.array([ml_input], dtype='float32')
        mlResults = model(ml_input)
        if (len(ml_outputs) == 0):
            ml_outputs = mlResults
        else:
            ml_outputs = np.append(ml_outputs, mlResults, axis=0)

    print(str(ml_outputs))
    print(str(len(ml_outputs[0])))

    display_trajectories(ml_outputs, animated=False, save_animation=False)
from classical_solution_four_bodies import FourBodyProblem

three_body_problem = FourBodyProblem(1, 1500, 0.2, 1.1, 2, 1.3, [0.5, 0],
                                     [-0.5, 0], [0, 0.5], [0, -0.5], [0, 0.01],
                                     [0, -0.01], [-0.03, 0], [0.05, 0.05])

results = three_body_problem.calculate_trajectories()

print(str(results))

three_body_problem.display_trajectories(animated=False, save_animation=False)

three_body_problem.to_csv("output.csv")