예제 #1
0
파일: board.py 프로젝트: GSchowalter/Chess
    def __init__(self):
        self.space = dict()
        self.color = dict()

        space_letters = ["A", "B", "C", "D", "E", "F", "G", "H"]

        for i in range(71, 7, -1):
            self.space[space_letters[i % 8] + str(i // 8)] = space.Space(
                'w', None) if i % 2 == 0 else space.Space('b', None)

        # Initialize a standard board
        # TODO: change to accept FEN notation
        self.initialize_standard_setup()
예제 #2
0
    def test_earth_moon_loop(self):
        """Test that moon loops over Earth."""
        moon = dot.Dot()
        moon.mass = constants.MOON.MASS
        moon.pos[0] = constants.MOON.PERIGEE_ORBIT_RADIUS
        init_moon_pos = numpy.array(moon.pos)
        moon.vel[1] = constants.MOON.MAX_ORBITAL_VELOCITY

        earth = dot.Dot()
        earth.mass = constants.EARTH.MASS
        # Set system inertion to 0
        earth.vel[
            1] = -constants.MOON.MAX_ORBITAL_VELOCITY * moon.mass / earth.mass

        test_space = space.Space(dots=[earth, moon])

        # Distance between moon initial position and position after loop
        min_distance = numpy.inf

        n_loops = 4.
        n_iterations = 10000
        t = 0
        dt = n_loops * constants.MOON.SIDEREAL_ROTATION_PERIOD / n_iterations
        for iteration in range(n_iterations):
            test_space.step(time=dt)
            t += dt
            if t > constants.MOON.SIDEREAL_ROTATION_PERIOD / 2:
                min_distance = min(
                    min_distance,
                    space.Space.distance(init_moon_pos, moon.pos),
                )

        assert min_distance < constants.MOON.RADIUS / 100, "Moon not reached init position."
예제 #3
0
def main():
    search_space = s.Space(0.0, epsilon, 15, __function__)
    particles_vector = [
        p.Particle() for _ in range(search_space.num_particles)
    ]
    search_space.particles = particles_vector
    search_space.set_pbest()
    search_space.set_gbest()

    iteration = 0
    n_iterations = 1000
    val_prev = val_prev_1 = 0.0
    while iteration < n_iterations or search_space.gbest_value < epsilon:
        search_space.set_pbest()
        search_space.set_gbest()

        # JEŚLI PRZEZ TRZY ITERACJE NIE ZMIENI SIE WARTOŚĆ => BREAK
        if iteration == 0:
            val_prev = search_space.gbest_value
        else:
            val_prev_1 = val_prev
            val_prev = search_space.gbest_value

        print("POŁOŻENIE: ", search_space.gbest_position, " WARTOŚĆ: ",
              search_space.gbest_value)
        saveToFile(__function__, search_space.gbest_value)
        if abs(search_space.gbest_value - search_space.target
               ) <= search_space.epsilon or val_prev_1 == val_prev:
            break

        search_space.move_particles()
        iteration += 1
    print("Solution: ", search_space.gbest_position, " value ",
          search_space.gbest_value)
예제 #4
0
def experiment(omega_factor):
    # Defining experiment parameters
    J0 = 1
    simulation_time = 2 * 10**(-9)
    # PEC box parameters
    x_length, y_length = 1, 1  # [m] (i.c. approximately 30 wavelengths)

    # Initializing a space with a PEC bounding box
    box = space.Space(x_length, y_length, simulation_time)
    src = source.Gaussian_pulse(1 / 2 * x_length, 1 / 2 * y_length, J0,
                                4 * 10**(-10), 10**(-10))
    box.set_source(src)

    lambda_min = 3 / omega_factor * src.get_lambda_min(1)
    Delta_p = lambda_min / 25
    Delta_t = 1 / (3 * c * np.sqrt(2 / Delta_p**2))
    box.define_discretization(Delta_p, Delta_p, Delta_t)

    # Measurement parameters
    measurement_points = [(1 / 2 * x_length, 1 / 2 * y_length)]
    measurement_titles = ["Reflected field"]

    # Adding measurment points
    box.add_measurement_points(measurement_points, measurement_titles)

    # Measuring time to perform measurement
    start = timeit.default_timer()
    box.FDTD(plot_space=False, visualize_fields=False, eps_averaging=False)
    time = timeit.default_timer() - start
    return [time, Delta_p, Delta_t, lambda_min]
예제 #5
0
def deviation(
        dvx1: object,
        dvy1: object,
        dvx2: object,
        dvy2: object,
        dist,
        plot: object = False
) -> object:  #simulation of first half of the orbit
    if plot:
        arrX.clear()
        arrY.clear()

    global dt
    unitmass = 1.0
    ut = unit.Unit(np.array([1.0, 0.0, 0.0]),
                   np.array([0.0 + dvx1, 1.0 + dvy1, 0.0]), unitmass)
    sp = space.Space()

    planetmass = 1.0 / G
    pl = planet.Planet(planetmass)
    sp.planet = pl
    sp.unit = ut
    pos = ut.pos
    angle = 0

    E0 = ut.getEnergy(planetmass)
    A0 = ut.vectorLenc(planetmass)

    flag = False

    while True:  #Condition for the whole orbit
        if (ut.pos[1] < ut.vel[1] * dt) and (ut.pos[0] < 0) and not flag:
            flag = True
            ut.vel[0] += dvx2
            ut.vel[1] += dvy2

        elif flag and (ut.pos[0] > 0) and (ut.pos[1] > 0):
            #pos_next = ut.pos[1] + ut.vel[1] * dt
            #vel_next = ut.vel + pl.acceleration(ut.pos) * dt
            #ut.vel = (ut.vel * abs(pos_next) + vel_next * abs(ut.pos[1])) / (abs(ut.pos[1]) + abs(pos_next))
            break

        angle += math.degrees(0.001 / 57)
        sp.step_rot(dt, dist, angle)

        if plot:

            arrX.append(ut.pos[0])
            arrY.append(ut.pos[1])

    E = ut.getEnergy(planetmass)
    A = ut.vectorLenc(planetmass)
    glob_vel = ut.vel
    glob_pos = ut.pos

    delta = ((E - E0)**2) + vectors.squarelength(A - A0)
    return delta
예제 #6
0
def worldInit():
    # things
    carKey = things.Thing("car key", tags=["take"])
    catFood = things.Thing("cat food",
                           tags=["take", "eat", "cat food"],
                           eat_val=6)
    # Actors
    hungryCat = actors.HouseCat(
        properName="Hungry Cat",
        commonName="Cat",
        description="He looks like he has a big appetite.",
        inventory=[catFood],
        hunger=15,
        hunger_rate=15,
    )
    # mouse = actors.Animal(
    #    commonName="mouse", description="It's a little mouse.", tags=[]
    # )
    # Spaces
    room27 = space.Space("Room 27", "My hotel room.")
    hallway = space.Space("the second floor hallway", "A long narrow hallway.")
    # Spaces -Exits
    doorway = space.Connection(room27,
                               hallway,
                               "south",
                               "north",
                               blocked=False)
    # Spaces -doors
    hallway.addThings(
        [things.Door("door", doorway, "An old wooden door.", room27)])
    # Spaces -things
    room27.addThings([carKey])
    room27.addThings([copy.copy(catFood)])
    hallway.addThings(
        [copy.copy(catFood),
         copy.copy(catFood),
         copy.copy(catFood)])
    # Spaces -Actors
    room27.addActors([hungryCat])
    # room27.addActors([copy.copy(mouse), copy.copy(mouse)])
    # Player
    player = actors.User("User", location=room27)
    actors.player = player
    return player
예제 #7
0
def main(parameters_file="example_scenarios/spatial/BlackScholes_scenario.json", input_data_file=None, output_pareto_file=None) :
    """
    Compute Pareto from the csv data files specified in the json output_pareto_file field.
    :param parameters_file: the json file the specify all the HyperMapper input parameters.
    :return: the csv file is written on disk.
    """

    print("######## compute_pareto.py #####################");
    print("### Parameters file is %s" % parameters_file); sys.stdout.flush()

    filename, file_extension = os.path.splitext(parameters_file)
    if file_extension != ".json":
        print("Error: invalid file name. \nThe input file has to be a .json file not a %s" % file_extension)
        exit(1)
    with open(parameters_file, 'r') as f:
        config = json.load(f)

    json_schema_file = 'scripts/schema.json'
    with open(json_schema_file, 'r') as f:
        schema = json.load(f)

    DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)
    DefaultValidatingDraft4Validator(schema).validate(config)

    application_name = config["application_name"]
    max_number_of_predictions = config['max_number_of_predictions']
    optimization_metrics = config["optimization_objectives"]
    number_of_cpus = config["number_of_cpus"]
    run_directory = config["run_directory"]
    if input_data_file == None:
        output_data_file = config["output_data_file"]
        if output_data_file == "output_samples.csv":
            output_data_file = application_name + "_" + output_data_file
        input_data_file = deal_with_relative_and_absolute_path(run_directory, output_data_file)
    if output_pareto_file == None:
        output_pareto_file = config["output_pareto_file"]
        if output_pareto_file == "output_pareto.csv":
            output_pareto_file = application_name + "_" + output_pareto_file
        output_pareto_file = deal_with_relative_and_absolute_path(run_directory, output_pareto_file)

    param_space = space.Space(config)
    print("### The input data file is %s" % input_data_file)
    print("### The output Pareto file is %s" % output_pareto_file)
    print("################################################")
    debug = False

    print("Computing the Pareto...")
    start_time = datetime.datetime.now()
    # Compute Pareto and save it to output_pareto_file
    with open(input_data_file, 'r') as f_csv_file_data_array:
        count_number_of_points_in_Pareto = compute_pareto(param_space, input_data_file, output_pareto_file,
                                                          debug, number_of_cpus)
    end_time = datetime.datetime.now()
    print(("Total time of computation is (read and Pareto computation): " + str((end_time - start_time).total_seconds()) + " seconds"))
    print(("The total size of the Pareto (RS + AL) is: %d" % count_number_of_points_in_Pareto)); sys.stdout.flush()
    print("End of the compute_pareto.py script!\n")
예제 #8
0
    def create_bombs(self):
        """
        Calls the generate_bomb_loc function to generate a list of bomb locations that 
        are then used to change the spaces at those locations to bombs.

        :return: void
        """
        Board.generate_bomb_loc(self.num_bomb, self.xdim, self.ydim)
        print(bomb_locations)
        for location in bomb_locations:
            self.gameboard[location[1] * self.xdim + location[0]] = space.Space(is_bomb = True, value = 9)
예제 #9
0
    def test_simple_earth_gravity(self):
        """Test gravitational acceleration at Earth surface near 9.8 m/s^2."""
        earth = dot.Dot()
        earth.mass = constants.EARTH.MASS

        simple_dot = dot.Dot()
        simple_dot.pos[0] = constants.EARTH.RADIUS

        test_space = space.Space(dots=[earth, simple_dot])

        test_space.step_acceleration()
        assert math.isclose(simple_dot.acc[0], -9.8, abs_tol=0.01)
예제 #10
0
    def create_board(gameboard:list, xdim:int, ydim:int):
        """
        Creates an empty two dimensional list of Spaces with the given dimensions.

        :param board: An empty list which will contain the spaces in the game
        :type board: list
        :param xdim: The x dimension of the board
        :type xdim: int
        :param ydim: The y dimension of the board
        :type ydim: int
        """
        for location in range(ydim * xdim):
            gameboard.append(space.Space())
예제 #11
0
def worldInit():
    # things
    pie = things.Thing("pie",
                       "A freshly baked pie.",
                       tags=["take", "eat"],
                       eat_val=10)
    knife = things.Thing("knife", "Sharp as all heck.", tags=["take"])

    # Actors
    GrumphTorgi = actors.Human("Grumph Torgi", "Grumph", "A villain!")
    SilbertHumperdinck = actors.Human("Silbert Humperdinck", "Sil",
                                      "Looks like a respectable fellow.")
    GertyVanFleek = actors.Human("Gerty Van Fleek",
                                 "Gerty",
                                 "An old pie woman of some sort.",
                                 inventory=[pie])
    MelissaMansname = actors.Human("Melissa Mansname", "Mel",
                                   "Just wed; nee Forthod")
    UmbrellaDeVille = actors.Human("Umbrella DeVille", "Ella",
                                   "Should be named deMaitreDe.")
    # Spaces
    Alley = space.Space("alley", "A dark alley.")
    Park = space.Space("park", "Look at this grass.")
    TorgiHome = space.Space("Torgi Household",
                            "The cluttered home of a man named Torgi.")
    MansnameHome = space.Space("Mansname Household",
                               "Wow! It's hard to come up with descriptions!")
    VanFleekHome = space.Space("Van Fleek Household", "Reeks of pie.")
    Restaurant = space.Space("Barren Grille", "I hear they have great desert.")
    # Spaces -Connections
    space.Connection(TorgiHome, Park, "north", "south")
    space.Connection(TorgiHome, Restaurant, "west", "east")
    space.Connection(Restaurant, Park, "north", "south")
    space.Connection(Park, Alley, "north", "south")
    space.Connection(VanFleekHome, Alley, "west", "east")
    space.Connection(MansnameHome, Alley, "east", "west")
    # Spaces -things
    tempKnife = copy(knife)
    TorgiHome.addThings([tempKnife])
    Restaurant.addThings([copy(pie)])

    # Spaces -Actors
    TorgiHome.addActors([GrumphTorgi, SilbertHumperdinck])
    Restaurant.addActors([UmbrellaDeVille])
    VanFleekHome.addActors([GertyVanFleek])
    MansnameHome.addActors([MelissaMansname])
    # Actors -Itinerary
    GrumphTorgi.addItinerary([
        (36000, ["take", tempKnife]),
        (43200, ["move", Park]),
        (57600, ["move", Alley]),
    ])
    # SilbertHumperdinck.addItinerary([(25200, 'wake up'), ()])
    # Player
    player = actors.User("User")
    Park.addActors([player])
    actors.player = player
    return player
예제 #12
0
def experiment(J0, eps_r, start_time, simulation_time):
    d = 0.05
    # PEC box parameters
    x_length, y_length = 0.5, 0.5  # [m] (i.c. approximately 30 wavelengths)

    # Initializing a space with a PEC bounding box
    box = space.Space(x_length, y_length, simulation_time)
    box.add_objects([
        dielectric.Dielectric(1 / 2 * x_length, 0, 1 / 2 * x_length, y_length,
                              eps_r)
    ])
    src = source.Gaussian_pulse(1 / 2 * x_length - d, 1 / 2 * y_length, J0,
                                4 * 10**(-10), 10**(-10))
    box.set_source(src)

    print("lambda_min ({}): {}".format(eps_r, src.get_lambda_min(eps_r)))
    Delta_p = src.get_lambda_min(eps_r) / 25
    Delta_t = 1 / (3 * c * np.sqrt(2 / Delta_p**2))
    box.define_discretization(Delta_p, Delta_p, Delta_t)

    # Measurement parameters
    measurement_points = [
        (1 / 2 * x_length - d, 1 / 2 * y_length),
        (1 / 2 * x_length + d, 1 / 2 * y_length),
    ]
    measurement_titles = ["Reflected field", "Transmitted field"]

    # Getting measurments
    box.add_measurement_points(measurement_points, measurement_titles)
    measurements = box.FDTD(plot_space=False,
                            visualize_fields=False,
                            eps_averaging=False)
    start = int(start_time / Delta_t)
    return np.array([
        measurements[0].time_E[start:],
        measurements[0].time_H[start:],
        measurements[0].H_x[start:],
        measurements[0].H_y[start:],
        measurements[0].E_z[start:],
        measurements[1].H_x[start:],
        measurements[1].H_y[start:],
        measurements[1].E_z[start:],
    ])
예제 #13
0
def deviation(dvx, dvy, dist):  #simulation of first half of the orbit
    global glob_pos  #some global names to store variables
    global glob_vel

    unitmass = 1.0
    ut = unit.Unit(np.array([1.0, 0.0, 0.0]),
                   np.array([0.0 + dvx, 1.1 + dvy, 0.0]), unitmass)
    sp = space.Space()

    count = 0
    planetmass = 1.0 / G
    pl = planet.Planet(planetmass)
    sp.planet = pl
    sp.unit = ut
    pos = ut.pos

    dt = 0.001  #simulation step

    E0 = ut.getEnergy(planetmass)
    A0 = ut.vectorLenc(planetmass)
    M0 = ut.getMomentum()

    while ((ut.pos[1] > ut.vel[1] * dt)
           or (ut.pos[0] > 0)):  #Condition for one half of the orbit
        #while (ut.movetime(dt) < math.pi):
        #while (count < math.pi/dt):
        sp.step(dt, dist)
        count += 1
        arrX.append(ut.pos[0])
        arrY.append(ut.pos[1])

    E = ut.getEnergy(planetmass)
    A = ut.vectorLenc(planetmass)
    M = ut.getMomentum()
    glob_vel = ut.vel
    glob_pos = ut.pos

    #delta = ((E - E0)**2) + vectors.squarelength(M - M0) + vectors.squarelength(A - A0)
    delta = ((E - E0)**2) + vectors.squarelength(A - A0)
    #print ('unit position: ',ut.pos)
    return delta
예제 #14
0
def deviation2(dvx, dvy, dist):  # simulation of the second half
    sp = space.Space()

    count = 0
    planetmass = 1.0 / G
    pl = planet.Planet(planetmass)
    sp.planet = pl

    unitmass = 1.0

    pos = glob_pos
    vel = glob_vel
    dv = np.array([dvx, dvy, 0])

    ut = unit.Unit(pos, (vel + dv), unitmass)
    sp.unit = ut
    #print ('start velocity', ut.vel)
    dt = 0.001

    E0 = ut.getEnergy(planetmass)

    A0 = ut.vectorLenc(planetmass)
    M0 = ut.getMomentum()

    #while (ut.pos[1] < ut.vel[1] * dt) or (ut.pos[0] < 0 ): #Condition for second half of the orbit
    #while (ut.pos[1] <= 0):
    while (count < math.pi / dt):
        sp.step(dt, dist)
        count += 1
        arrX.append(ut.pos[0])
        arrY.append(ut.pos[1])

    E = ut.getEnergy(planetmass)
    A = ut.vectorLenc(planetmass)
    M = ut.getMomentum()

    #delta = ((E - E0)**2) + vectors.squarelength(M - M0) + vectors.squarelength(A - A0)
    delta = ((E - E0)**2) + vectors.squarelength(A - A0)
    #print ('unit position: ', ut.pos , 'unit velocity: ', ut.vel)
    return delta
예제 #15
0
    def __init__(self,
                 alpha,
                 beta,
                 tau,
                 env,
                 gamma=0.99,
                 size=1000000,
                 batch_size=64,
                 checkpoint='tmp/ddpg',
                 max_actions=1e6,
                 k_ratio=0.1):
        super().__init__(alpha, beta, tau, env, gamma, size, batch_size,
                         checkpoint)
        self.experiment = env.spec.id
        if self.continious_action_space:
            self.action_space = space.Space(self.low, self.high, max_actions)
            max_actions = self.action_space.get_number_of_actions()
        else:
            max_actions = int(env.action_space.n) * 5
            self.action_space = space.Discrete_space(max_actions)

        self.k_nearest_neighbors = max(5, int(max_actions * k_ratio))
예제 #16
0
파일: main.py 프로젝트: malbingi/STUDIA
def main():
    search_space = s.Space(0.0, epsilon, 500)
    particles_vector = [
        p.Particle() for _ in range(search_space.num_particles)
    ]
    search_space.particles = particles_vector
    search_space.set_pbest()
    search_space.set_gbest()

    iteration = 0
    n_iterations = 100
    val_prev = val_prev_1 = 0.0
    while iteration < n_iterations or search_space.gbest_value <= epsilon:
        search_space.set_pbest()
        search_space.set_gbest()

        # JEŚLI PRZEZ TRZY ITERACJE NIE ZMIENI SIE WARTOŚĆ => BREAK

        if iteration == 0:
            val_prev = search_space.gbest_value
        else:
            val_prev_1 = val_prev
            val_prev = search_space.gbest_value

        #print("POŁOŻENIE: ", search_space.gbest_position, " WARTOŚĆ: ", search_space.gbest_value)
        if abs(search_space.gbest_value - search_space.target
               ) <= search_space.epsilon or val_prev_1 == val_prev:
            break

        search_space.move_particles()
        iteration += 1
        save(search_space.gbest_value, 0)
    print("Solution: ", search_space.gbest_position, " value ",
          search_space.gbest_value)
    file = open("model.txt", 'a')
    file.write(str(search_space.gbest_position) + " \n")
    file.close()
    return search_space.gbest_position
예제 #17
0
파일: main.py 프로젝트: malbingi/STUDIA
def main():
    search_space = s.Space(optimization_target, epsilon, 25)
    particles_vector = [
        p.Particle() for _ in range(search_space.num_particles)
    ]
    search_space.particles = particles_vector
    search_space.set_pbest()
    search_space.set_gbest()

    iteration = 0
    n_iterations = 1000
    val_prev = val_prev_1 = 0.0
    while iteration < n_iterations or search_space.gbest_value < epsilon:
        print(iteration)
        search_space.set_pbest()
        search_space.set_gbest()

        if iteration == 0:
            val_prev = search_space.gbest_value
        else:
            val_prev_1 = val_prev
            val_prev = search_space.gbest_value

        #print("POŁOŻENIE: ", search_space.gbest_position, " WARTOŚĆ: ", search_space.gbest_value)

        saveToFile(search_space.gbest_value, "main_22.csv")
        saveToFile(search_space.gbest_position, "coordinates_22.csv")

        if iteration > 10:
            if abs(search_space.gbest_value - search_space.target
                   ) <= search_space.epsilon or val_prev_1 == val_prev:
                break

        search_space.move_particles()
        iteration += 1
    #print("Solution: ", search_space.gbest_position, " value ", search_space.gbest_value)
    return search_space.gbest_position
예제 #18
0
omega_c = 10**5  # [Hz] = 1 GHz
omega_c = 0  # [Hz] = 1 GHz
sigma = 5 * 10**(-9)  # [s]
tc = 3 * sigma  # [s]

# Initializing the source (Choices: Sine_source, Gaussian_pulse, Gaussian_modulated_rd_pulse)
# src = source.Sine_source(x_source, y_source, J0, omega_c)
src = source.Gaussian_pulse(x_source, y_source, J0, tc, sigma)
# src = source.Gaussian_modulated_rf_pulse(x_source, y_source, J0, tc, sigma, omega_c)

# PEC box parameters
x_length, y_length = 2 * x_source, 2 * y_source  # [m]
t_length = 7 * tc  # [s]

# Initializing a space with a PEC bounding box
box = space.Space(x_length, y_length, t_length)
box.set_source(src)

# Discretization parameters (Based on limits in project description)
Delta_x = src.get_lambda_min(1) / 30
Delta_y = Delta_x
Delta_t = 1 / (c * np.sqrt(1 / Delta_x**2 + 1 / Delta_y**2)) * 1.0001

# Handing discretization parameters to our space
box.define_discretization(Delta_x, Delta_y, Delta_t)

# Measurement parameters
measurement_points = [
    (x_source, y_source)
]  # [(1.1*x_source, 1.1*y_source)] + [(1.2*x_source, 1.24*y_source)] # List of measurement point coordinates [(m, m)]
measurement_labels = [r"     $1.0001$ Courant limit     $t_{max} = 7 t_c$"
예제 #19
0
def main(config, black_box_function=None, output_file=""):
    """
    Run design-space exploration using bayesian optimization.
    :param config: dictionary containing all the configuration parameters of this optimization.
    :param output_file: a name for the file used to save the dse results.
    """
    start_time = (datetime.datetime.now())
    run_directory = config["run_directory"]
    hypermapper_mode = config["hypermapper_mode"]["mode"]

    # Start logging
    log_file = deal_with_relative_and_absolute_path(run_directory,
                                                    config["log_file"])
    sys.stdout.change_log_file(log_file)
    if (hypermapper_mode == 'client-server'):
        sys.stdout.switch_log_only_on_file(True)

    # Log the json configuration for this optimization
    sys.stdout.write_to_logfile(str(config) + "\n")

    # Create parameter space object and unpack hyperparameters from json
    param_space = space.Space(config)
    application_name = config["application_name"]
    optimization_metrics = config["optimization_objectives"]
    optimization_iterations = config["optimization_iterations"]
    evaluations_per_optimization_iteration = config[
        "evaluations_per_optimization_iteration"]
    number_of_cpus = config["number_of_cpus"]
    print_importances = config["print_parameter_importance"]
    epsilon_greedy_threshold = config["epsilon_greedy_threshold"]
    acquisition_function = config["acquisition_function"]
    weight_sampling = config["weight_sampling"]
    scalarization_method = config["scalarization_method"]
    scalarization_key = config["scalarization_key"]
    doe_type = config["design_of_experiment"]["doe_type"]
    number_of_doe_samples = config["design_of_experiment"]["number_of_samples"]
    model_type = config["models"]["model"]
    optimization_method = config["optimization_method"]
    time_budget = config["time_budget"]
    input_params = param_space.get_input_parameters()
    number_of_objectives = len(optimization_metrics)
    objective_limits = {}
    data_array = {}
    fast_addressing_of_data_array = {}
    objective_bounds = None
    exhaustive_search_data_array = None
    normalize_objectives = False
    debug = False

    if "feasible_output" in config:
        feasible_output = config["feasible_output"]
        feasible_output_name = feasible_output["name"]
        enable_feasible_predictor = feasible_output[
            "enable_feasible_predictor"]
        enable_feasible_predictor_grid_search_on_recall_and_precision = feasible_output[
            "enable_feasible_predictor_grid_search_on_recall_and_precision"]
        feasible_predictor_grid_search_validation_file = feasible_output[
            "feasible_predictor_grid_search_validation_file"]
        feasible_parameter = param_space.get_feasible_parameter()
        number_of_trees = config["models"]["number_of_trees"]

    if (weight_sampling == "bounding_box"):
        objective_bounds = {}
        user_bounds = config["bounding_box_limits"]
        if (len(user_bounds) == 2):
            if (user_bounds[0] > user_bounds[1]):
                user_bounds[0], user_bounds[1] = user_bounds[1], user_bounds[0]
            for objective in optimization_metrics:
                objective_bounds[objective] = user_bounds
                objective_limits[objective] = user_bounds
        elif (len(user_bounds) == number_of_objectives * 2):
            idx = 0
            for objective in optimization_metrics:
                objective_bounds[objective] = user_bounds[idx:idx + 2]
                if (objective_bounds[objective][0] >
                        objective_bounds[objective][1]):
                    objective_bounds[objective][0], objective_bounds[
                        objective][1] = objective_bounds[objective][
                            1], objective_bounds[objective][0]
                objective_limits[objective] = objective_bounds[objective]
                idx += 2
        else:
            print("Wrong number of bounding boxes, expected 2 or",
                  2 * number_of_objectives, "got", len(user_bounds))
            raise SystemExit
    else:
        for objective in optimization_metrics:
            objective_limits[objective] = [float("inf"), float("-inf")]

    if output_file == "":
        output_data_file = config["output_data_file"]
        if output_data_file == "output_samples.csv":
            output_data_file = application_name + "_" + output_data_file
    else:
        output_data_file = output_file

    exhaustive_search_data_array = None
    exhaustive_search_fast_addressing_of_data_array = None
    if hypermapper_mode == 'exhaustive':
        exhaustive_file = config["hypermapper_mode"]["exhaustive_search_file"]
        exhaustive_search_data_array, exhaustive_search_fast_addressing_of_data_array = param_space.load_data_file(
            exhaustive_file, debug=False, number_of_cpus=number_of_cpus)

    # Check if some parameters are correctly defined
    if hypermapper_mode == "default":
        if black_box_function == None:
            print("Error: the black box function must be provided")
            raise SystemExit
        if not callable(black_box_function):
            print("Error: the black box function parameter is not callable")
            raise SystemExit

    if (model_type == "gaussian_process") and (acquisition_function == "TS"):
        print(
            "Error: The TS acquisition function with Gaussian Process models is still under implementation"
        )
        print("Using EI acquisition function instead")
        config["acquisition_function"] = "EI"

    if number_of_cpus > 1:
        print(
            "Warning: HyperMapper supports only sequential execution for now. Running on a single cpu."
        )
        number_of_cpus = 1

    # If priors are present, use prior-guided optimization
    user_priors = False
    for input_param in config["input_parameters"]:
        if config["input_parameters"][input_param]["prior"] != "uniform":
            if number_of_objectives == 1:
                user_priors = True
            else:
                print(
                    "Warning: prior optimization does not work with multiple objectives yet, priors will be uniform"
                )
                config["input_parameters"][input_param]["prior"] = "uniform"

    if user_priors:
        bo_method = prior_guided_optimization
    else:
        bo_method = random_scalarizations
        normalize_objectives = True

    ### Resume previous optimization, if any
    beginning_of_time = param_space.current_milli_time()
    absolute_configuration_index = 0
    doe_t0 = datetime.datetime.now()
    if config["resume_optimization"] == True:
        resume_data_file = config["resume_optimization_data"]

        if not resume_data_file.endswith('.csv'):
            print("Error: resume data file must be a CSV")
            raise SystemExit
        if resume_data_file == "output_samples.csv":
            resume_data_file = application_name + "_" + resume_data_file

        data_array, fast_addressing_of_data_array = param_space.load_data_file(
            resume_data_file, debug=False, number_of_cpus=number_of_cpus)
        absolute_configuration_index = len(data_array[list(data_array.keys(
        ))[0]])  # get the number of points evaluated in the previous run
        beginning_of_time = beginning_of_time - data_array[
            param_space.get_timestamp_parameter()[0]][
                -1]  # Set the timestamp back to match the previous run
        print("Resumed optimization, number of samples = %d ......." %
              absolute_configuration_index)

    ### DoE phase
    if absolute_configuration_index < number_of_doe_samples:
        configurations = []
        default_configuration = param_space.get_default_or_random_configuration(
        )
        str_data = param_space.get_unique_hash_string_from_values(
            default_configuration)
        if str_data not in fast_addressing_of_data_array:
            fast_addressing_of_data_array[
                str_data] = absolute_configuration_index
            configurations.append(default_configuration)
            absolute_configuration_index += 1

        doe_configurations = []
        if absolute_configuration_index < number_of_doe_samples:
            doe_configurations = param_space.get_doe_sample_configurations(
                fast_addressing_of_data_array,
                number_of_doe_samples - absolute_configuration_index, doe_type)
        configurations += doe_configurations
        print(
            "Design of experiment phase, number of new doe samples = %d ......."
            % len(configurations))

        doe_data_array = param_space.run_configurations(
            hypermapper_mode, configurations, beginning_of_time,
            black_box_function, exhaustive_search_data_array,
            exhaustive_search_fast_addressing_of_data_array, run_directory)
        data_array = concatenate_data_dictionaries(
            data_array, doe_data_array,
            param_space.input_output_and_timestamp_parameter_names)
        absolute_configuration_index = number_of_doe_samples
        iteration_number = 1
    else:
        iteration_number = absolute_configuration_index - number_of_doe_samples + 1

    # If we have feasibility constraints, we must ensure we have at least one feasible and one infeasible sample before starting optimization
    # If this is not true, continue design of experiment until the condition is met
    if enable_feasible_predictor:
        while are_all_elements_equal(data_array[
                feasible_parameter[0]]) and optimization_iterations > 0:
            print(
                "Warning: all points are either valid or invalid, random sampling more configurations."
            )
            print("Number of doe samples so far:",
                  absolute_configuration_index)
            configurations = param_space.get_doe_sample_configurations(
                fast_addressing_of_data_array, 1, "random sampling")
            new_data_array = param_space.run_configurations(
                hypermapper_mode, configurations, beginning_of_time,
                black_box_function, exhaustive_search_data_array,
                exhaustive_search_fast_addressing_of_data_array, run_directory)
            data_array = concatenate_data_dictionaries(
                new_data_array, data_array,
                param_space.input_output_and_timestamp_parameter_names)
            absolute_configuration_index += 1
            optimization_iterations -= 1

    # Create output file with explored configurations from resumed run and DoE
    with open(
            deal_with_relative_and_absolute_path(run_directory,
                                                 output_data_file), 'w') as f:
        w = csv.writer(f)
        w.writerow(param_space.get_input_output_and_timestamp_parameters())
        tmp_list = [
            param_space.convert_types_to_string(j, data_array)
            for j in param_space.get_input_output_and_timestamp_parameters()
        ]
        tmp_list = list(zip(*tmp_list))
        for i in range(len(data_array[optimization_metrics[0]])):
            w.writerow(tmp_list[i])

    for objective in optimization_metrics:
        lower_bound = min(objective_limits[objective][0],
                          min(data_array[objective]))
        upper_bound = max(objective_limits[objective][1],
                          max(data_array[objective]))
        objective_limits[objective] = [lower_bound, upper_bound]
    print(
        "\nEnd of doe/resume phase, the number of evaluated configurations is: %d\n"
        % absolute_configuration_index)
    sys.stdout.write_to_logfile(
        ("End of DoE - Time %10.4f sec\n" %
         ((datetime.datetime.now() - doe_t0).total_seconds())))
    if doe_type == "grid_search" and optimization_iterations > 0:
        print(
            "Warning: DoE is grid search, setting number of optimization iterations to 0"
        )
        optimization_iterations = 0

    ### Main optimization loop
    if evaluations_per_optimization_iteration > 1:
        print("Warning, number of evaluations per iteration > 1")
        print(
            "HyperMapper bayesian optimization currently does not support multiple runs per iteration, setting evaluations per iteration to 1"
        )

    bo_t0 = datetime.datetime.now()
    run_time = (datetime.datetime.now() - start_time).total_seconds() / 60
    # run_time / time_budget < 1 if budget > elapsed time or budget == -1
    if time_budget > 0:
        print('starting optimization phase, limited to run for ', time_budget,
              ' minutes')
    elif time_budget == 0:
        print(
            'Time budget cannot be zero. To not limit runtime set time_budget = -1'
        )
        sys.exit()

    while iteration_number <= optimization_iterations and run_time / time_budget < 1:
        print("Starting optimization iteration", iteration_number)
        iteration_t0 = datetime.datetime.now()
        model_t0 = datetime.datetime.now()
        regression_models, _, _ = models.generate_mono_output_regression_models(
            data_array,
            param_space,
            input_params,
            optimization_metrics,
            1.00,
            config,
            model_type=model_type,
            number_of_cpus=number_of_cpus,
            print_importances=print_importances,
            normalize_objectives=normalize_objectives,
            objective_limits=objective_limits)

        classification_model = None
        if enable_feasible_predictor:
            classification_model, _, _ = models.generate_classification_model(
                application_name,
                param_space,
                data_array,
                input_params,
                feasible_parameter,
                1.00,
                config,
                debug,
                number_of_cpus=number_of_cpus,
                data_array_exhaustive=exhaustive_search_data_array,
                enable_feasible_predictor_grid_search_on_recall_and_precision=
                enable_feasible_predictor_grid_search_on_recall_and_precision,
                feasible_predictor_grid_search_validation_file=
                feasible_predictor_grid_search_validation_file,
                print_importances=print_importances)
        model_t1 = datetime.datetime.now()
        if (weight_sampling == "bounding_box"):
            objective_weights = sample_weight_bbox(optimization_metrics,
                                                   objective_bounds, 1)[0]
        elif (weight_sampling == "flat"):
            objective_weights = sample_weight_flat(optimization_metrics, 1)[0]
        else:
            print("Error: unrecognized option:", weight_sampling)
            raise SystemExit

        data_array_scalarization, _ = compute_data_array_scalarization(
            data_array, objective_weights, objective_limits,
            scalarization_method)
        data_array[scalarization_key] = data_array_scalarization.tolist()

        epsilon = random.uniform(0, 1)
        local_search_t0 = datetime.datetime.now()
        if epsilon > epsilon_greedy_threshold:
            best_configuration = bo_method(config, data_array, param_space,
                                           fast_addressing_of_data_array,
                                           regression_models, iteration_number,
                                           objective_weights, objective_limits,
                                           classification_model)

        else:
            sys.stdout.write_to_logfile(
                str(epsilon) + " < " + str(epsilon_greedy_threshold) +
                " random sampling a configuration to run\n")
            best_configuration = param_space.random_sample_configurations_without_repetitions(
                fast_addressing_of_data_array, 1)[0]
        local_search_t1 = datetime.datetime.now()

        configurations = [best_configuration]
        str_data = param_space.get_unique_hash_string_from_values(
            best_configuration)
        fast_addressing_of_data_array[str_data] = absolute_configuration_index
        absolute_configuration_index += 1

        black_box_function_t0 = datetime.datetime.now()
        new_data_array = param_space.run_configurations(
            hypermapper_mode, configurations, beginning_of_time,
            black_box_function, exhaustive_search_data_array,
            exhaustive_search_fast_addressing_of_data_array, run_directory)
        black_box_function_t1 = datetime.datetime.now()

        with open(
                deal_with_relative_and_absolute_path(run_directory,
                                                     output_data_file),
                'a') as f:
            w = csv.writer(f)
            tmp_list = [
                param_space.convert_types_to_string(j, new_data_array) for j in
                list(param_space.get_input_output_and_timestamp_parameters())
            ]
            tmp_list = list(zip(*tmp_list))
            for i in range(len(new_data_array[optimization_metrics[0]])):
                w.writerow(tmp_list[i])

        data_array = concatenate_data_dictionaries(
            new_data_array, data_array,
            param_space.input_output_and_timestamp_parameter_names)
        for objective in optimization_metrics:
            lower_bound = min(objective_limits[objective][0],
                              min(data_array[objective]))
            upper_bound = max(objective_limits[objective][1],
                              max(data_array[objective]))
            objective_limits[objective] = [lower_bound, upper_bound]

        iteration_number += 1
        run_time = (datetime.datetime.now() - start_time).total_seconds() / 60

        sys.stdout.write_to_logfile(("Model fitting time %10.4f sec\n" %
                                     ((model_t1 - model_t0).total_seconds())))
        sys.stdout.write_to_logfile(
            ("Local search time %10.4f sec\n" %
             ((local_search_t1 - local_search_t0).total_seconds())))
        sys.stdout.write_to_logfile(("Black box function time %10.4f sec\n" % (
            (black_box_function_t1 - black_box_function_t0).total_seconds())))
        sys.stdout.write_to_logfile(
            ("Total iteration time %10.4f sec\n" %
             ((datetime.datetime.now() - iteration_t0).total_seconds())))
    sys.stdout.write_to_logfile(
        ("End of BO phase - Time %10.4f sec\n" %
         ((datetime.datetime.now() - bo_t0).total_seconds())))

    print("End of Bayesian Optimization")
    sys.stdout.write_to_logfile(
        ("Total script time %10.2f sec\n" %
         ((datetime.datetime.now() - start_time).total_seconds())))
예제 #20
0
def main(parameters_file, output_hvi_file_name, list_of_dirs):
    """
    Plot the hypervolume indicator (HVI) results of the design space exploration.
    In this plot specifically we plot the HVI of HyperMapper's DSE against the HVI of a competing approach.
    On the x axis we plot time in seconds and on the y axis the HVI.
    HVI to be computed needs a real Pareto or at least a Pareto that is the best found by the results concatenation of
    HyperMapper and the competing approach.

    ######################################################
    ######### Input of this script ######################
    # 1) a file that is the real Pareto or the best Pareto found
    #    (supposing the we are comparing several approaches for example the best Pareto is the result of all these approaches combined).
    # 2) a file containing all the samples of the exploration (not only the Pareto).
    #    From this file we can compute the Pareto at time t and then the hvi at time t
    """
    xlabel = "Time (sec)"
    ylabel = "HyperVolume Indicator (HVI)"
    number_of_bins = 20

    filename, file_extension = os.path.splitext(parameters_file)
    if file_extension != ".json":
        print(
            "Error: invalid file name. \nThe input file has to be a .json file not a %s"
            % file_extension)
        exit(1)
    with open(parameters_file, 'r') as f:
        config = json.load(f)

    json_schema_file = 'scripts/schema.json'
    with open(json_schema_file, 'r') as f:
        schema = json.load(f)

    DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)
    DefaultValidatingDraft4Validator(schema).validate(config)

    if "application_name" in config:
        application_name = config["application_name"]
    else:
        application_name = ""

    print("########## plot_hvi.py #########################")
    print("### Parameters file is %s" % parameters_file)
    print("### Application name is %s" % application_name)
    print("### The input directories data are %s" % str(list_of_dirs))
    print("################################################")

    param_space = space.Space(config)
    optimization_metrics = param_space.get_optimization_parameters()

    ###################################################################################################################
    ########### Compute the hypervolume of all the input files concatenated as a reference for the HVI metric.
    ###################################################################################################################
    input_files = {}

    # y_data_mean is dict on the directories that for each entry in the dict contains the mean of each point x over multiple file repetitions in one directory; lower and upper are for the confidence interval.
    y_data_mean = defaultdict(list)
    y_data_median = defaultdict(list)
    y_data_min = defaultdict(list)
    y_data_max = defaultdict(list)
    y_data_lower = defaultdict(list)
    y_data_upper = defaultdict(list)
    bin_array_X = {}
    number_or_runs_in_bins = {}

    for dir in list_of_dirs:
        input_files[dir] = [f for f in listdir(dir) if isfile(join(dir, f))]

    for dir in list_of_dirs:
        files_to_remove = []
        for file in input_files[dir]:
            filename, file_extension = os.path.splitext(file)
            if file_extension != ".csv":
                print(
                    "Warning: file %s is not a csv file, it will not be considered in the HVI plot. "
                    % file)
                files_to_remove.append(file)
        # Don't move this for loop inside the previous identical one otherwise you will remove the elements before they get process because of overlapping references.
        for file in files_to_remove:
            input_files[dir].remove(file)

    for dir in list_of_dirs:
        if len(input_files[dir]) == 0:
            print(
                "Warning: directory %s is empty, it will not be considered in the HVI plot."
            )
            del input_files[dir]

    if len(input_files) == 0:
        print("Error: there no input files to compute the HVI.")

    print("The files used as a input are: ")
    for i, dir in enumerate(input_files.keys()):
        print("Directory " + str(i) + ": " + dir + ", # of files: " +
              str(len(input_files[dir])) + ", list of files: " +
              str(input_files[dir]))

    all_data_files = []
    for dir in input_files.keys():
        for file in input_files[dir]:
            all_data_files += [dir + "/" + file]

    selection_keys = param_space.get_output_parameters(
    ) + param_space.get_timestamp_parameter()
    feasible_flag = True if (
        param_space.get_feasible_parameter() != [None]) else False
    concatenated_all_data_array = param_space.load_data_files(
        all_data_files,
        selection_keys_list=selection_keys,
        only_valid=feasible_flag)

    if len(next(iter(concatenated_all_data_array.values()))) == 0:
        return return_empty_images(application_name, input_files,
                                   number_of_bins, output_hvi_file_name,
                                   xlabel, ylabel)

    bounds = {}
    max_point = []
    standard_deviation_optimization_metrics = []
    max_min_difference = []
    # Get bounds of objective space
    for metric in optimization_metrics:
        X = np.array(concatenated_all_data_array[metric])

        standard_deviation = np.std(X, axis=0)
        standard_deviation_optimization_metrics.append(standard_deviation)
        X /= standard_deviation

        concatenated_all_data_array[metric] = X
        bounds[metric] = (min(concatenated_all_data_array[metric]),
                          max(concatenated_all_data_array[metric]))
        max_point.append(bounds[metric][1])
        max_min_difference.append(bounds[metric][1] - bounds[metric][0])
        print(
            "(min, max) = (%f, %f) for the metric %s. This is to compute the hypervolume."
            % (bounds[metric][0], bounds[metric][1], metric))

    total_volume = prod(max_min_difference)
    list_of_objectives = [
        concatenated_all_data_array[objective]
        for objective in param_space.get_optimization_parameters()
    ]
    reformatted_all_data = list(zip(*list_of_objectives))

    # Get dominated hypervolume for Pareto of all data observed
    hv_all_data = H(reformatted_all_data, max_point)
    print("The hypervolume of all the files concatenated: %d" % hv_all_data)

    ###################################################################################################################
    ########### Compute the HVI for each directory.
    ###################################################################################################################
    hvi = {}
    for dir in input_files:
        print("Compute HVI for %s" % dir)
        convert_in_seconds = 1000.0
        hvi[dir], bin_array_X[dir], number_or_runs_in_bins[dir] = compute_hvi(
            standard_deviation_optimization_metrics, input_files[dir], dir,
            total_volume, max_point, hv_all_data, param_space,
            convert_in_seconds, number_of_bins)

        # Round the floating point numbers to 1 decimal for clarity of visualization.
        bin_array_X[dir] = [round(float(i), 1) for i in bin_array_X[dir]]
        for file in hvi[dir]:
            for bin in hvi[dir][file]:
                hvi[dir][file][bin] = round(float(hvi[dir][file][bin]), 1)

    ###################################################################################################################
    ########### Plot all the HVIs (using box plots bin_array_X and hvi)
    ###################################################################################################################

    for dir in input_files:
        hvi_list_of_lists = []
        each_bin = defaultdict(list)
        for file in hvi[dir]:
            for bin in hvi[dir][file]:
                each_bin[bin].append(hvi[dir][file][bin])
        for bin in hvi[dir][file]:
            hvi_list_of_lists.append(
                each_bin[bin]
            )  # This is a list of bins and for each bin there is a list of hvi values for each file in that directory.

        # Print boxplot (one figure per directory).
        boxplot(bin_array_X[dir], hvi_list_of_lists, application_name,
                number_of_bins, xlabel, ylabel,
                str(dir + "/" + os.path.basename(dir) + "_boxplot" + ".pdf"))

        # Print lineplot (only one figure comparing all the directories).
        for hvi_list in hvi_list_of_lists:
            hvi_list_array = np.array(hvi_list)
            y_data_mean[dir].append(hvi_list_array.mean())
            y_data_median[dir].append(np.median(hvi_list_array))
            y_data_min[dir].append(np.min(hvi_list_array))
            y_data_max[dir].append(np.max(hvi_list_array))
            low, up = sms.DescrStatsW(hvi_list_array).tconfint_mean()
            y_data_lower[dir].append(low)
            y_data_upper[dir].append(up)

        for bin_number, bin_value in enumerate(y_data_lower[dir]):
            if not math.isnan(bin_value) and bin_value < 0:
                y_data_lower[dir][bin_number] = 0
        for bin_number, bin_value in enumerate(y_data_upper[dir]):
            if not math.isnan(bin_value) and bin_value < 0:
                y_data_upper[dir][bin_number] = 0

        print_stats_on_a_txt(
            dir, str(dir + "/" + os.path.basename(dir) + "_stats" + ".txt"),
            bin_array_X, number_or_runs_in_bins, y_data_mean, y_data_median,
            y_data_min, y_data_max, y_data_lower, y_data_upper)

    # Call the function to create plot
    lineplotCI(input_files,
               application_name,
               x_data=bin_array_X,
               y_data=y_data_mean,
               low_CI=y_data_lower,
               upper_CI=y_data_upper,
               xlabel=xlabel,
               ylabel=ylabel,
               title="Line plot with 95% confidence intervals",
               output_filename=output_hvi_file_name)
예제 #21
0
def HVI_from_files(real_pareto_file, parameters_file):
    """
    Compute hvi for a target Pareto front using the real Pareto front as reference.
    :param real_pareto_file: file containing the real Pareto front
    :param parameters_file: file containing the experiment scenario. Also used to find the target Pareto file.
    :return: the hvi of the target Pareto front
    """
    config = validate_json(parameters_file)
    param_space = space.Space(config)

    application_name = config["application_name"]
    test_pareto_file = config["output_pareto_file"]
    run_directory = config["run_directory"]
    if test_pareto_file == "output_pareto.csv":
        test_pareto_file = application_name + "_" + test_pareto_file
    test_pareto_file = deal_with_relative_and_absolute_path(
        run_directory, test_pareto_file)

    optimization_metrics = param_space.get_optimization_parameters()
    selection_keys = optimization_metrics + param_space.get_timestamp_parameter(
    )
    feasible_flag = True if (
        param_space.get_feasible_parameter() != [None]) else False
    exhaustive_branin_pareto, _ = param_space.load_data_file(
        real_pareto_file,
        selection_keys_list=selection_keys,
        only_valid=feasible_flag)
    test_pareto, _ = param_space.load_data_file(
        test_pareto_file,
        selection_keys_list=selection_keys,
        only_valid=feasible_flag)
    concatenated_all_data_array = concatenate_data_dictionaries(
        exhaustive_branin_pareto,
        test_pareto,
        selection_keys_list=selection_keys)

    standard_deviations, max_point = compute_std_and_max_point(
        concatenated_all_data_array, optimization_metrics)

    exhaustive_branin_pareto = normalize_with_std(exhaustive_branin_pareto,
                                                  standard_deviations,
                                                  optimization_metrics)
    test_pareto = normalize_with_std(test_pareto, standard_deviations,
                                     optimization_metrics)

    exhaustive_branin_pareto = [
        exhaustive_branin_pareto[objective]
        for objective in optimization_metrics
    ]
    exhaustive_branin_pareto = list(zip(*exhaustive_branin_pareto))

    test_pareto = [
        test_pareto[objective] for objective in optimization_metrics
    ]
    test_pareto = list(zip(*test_pareto))

    hv_exhaustive = H(exhaustive_branin_pareto, max_point)
    hv_test = H(test_pareto, max_point)
    hvi = hv_exhaustive - hv_test

    return hvi
예제 #22
0
def main(config, black_box_function=None, output_file=""):
    """
    Run design-space exploration using evolution.
    :param config: dictionary containing all the configuration parameters of this design-space exploration.
    :param black_box_function: The function hypermapper seeks to optimize
    :param output_file: a name for the file used to save the dse results.
    :return:
    """
    param_space = space.Space(config)

    run_directory = config["run_directory"]
    application_name = config["application_name"]
    hypermapper_mode = config["hypermapper_mode"]["mode"]
    if hypermapper_mode == "default":
        if black_box_function == None:
            print("Error: the black box function must be provided")
            raise SystemExit
        if not callable(black_box_function):
            print("Error: the black box function parameter is not callable")
            raise SystemExit

    optimization_metrics = config["optimization_objectives"]
    number_of_objectives = len(optimization_metrics)
    if number_of_objectives != 1:
        print(
            "the evolutionary optimization does not support multi-objective optimization. Exiting."
        )
        sys.exit()

    fitness_measure = optimization_metrics[0]
    population_size = config["evolution_population_size"]
    generations = config["evolution_generations"]
    mutation_rate = config["mutation_rate"]
    if mutation_rate > len(param_space.get_input_parameters()):
        print(
            "mutation rate cannot be higher than the number of parameters. Exiting."
        )
        sys.exit()
    if mutation_rate < 1:
        print(
            "mutation rate must be at least 1 for evolution to work. Exiting.")
        sys.exit()
    crossover = config["evolution_crossover"]
    regularize = config["regularize_evolution"]
    batch_size = config["batch_size"]
    if batch_size > population_size:
        print("population_size must be bigger than batch_size. Exiting.")
        sys.exit()
    elif batch_size < 2 and not crossover:
        print("batch_size cannot be smaller than 2. Exiting.")
        sys.exit()
    elif batch_size < 3 and crossover:
        print("batch_size must be at least 3 when using crossover. Exiting.")
        sys.exit()

    log_file = deal_with_relative_and_absolute_path(run_directory,
                                                    config["log_file"])
    sys.stdout.change_log_file(log_file)
    if hypermapper_mode == "client-server":
        sys.stdout.switch_log_only_on_file(True)

    if output_file == "":
        output_data_file = config["output_data_file"]
        if output_data_file == "output_samples.csv":
            output_data_file = application_name + "_" + output_data_file
    else:
        output_data_file = output_file

    absolute_configuration_index = 0
    fast_addressing_of_data_array = {}
    evolution_fast_addressing_of_data_array = {}
    evolution_data_array = defaultdict(list)

    beginning_of_time = param_space.current_milli_time()

    optimization_function_parameters = dict()
    optimization_function_parameters['hypermapper_mode'] = hypermapper_mode
    optimization_function_parameters['param_space'] = param_space
    optimization_function_parameters['beginning_of_time'] = beginning_of_time
    optimization_function_parameters['run_directory'] = run_directory
    optimization_function_parameters['black_box_function'] = black_box_function
    optimization_function_parameters[
        'evolution_data_array'] = evolution_data_array
    optimization_function_parameters[
        'fast_addressing_of_data_array'] = evolution_fast_addressing_of_data_array

    print("Starting evolution...")
    evolution_t0 = datetime.datetime.now()
    all_samples = evolution(population_size, generations, mutation_rate,
                            crossover, regularize, batch_size, fitness_measure,
                            param_space, fast_addressing_of_data_array,
                            run_objective_function,
                            optimization_function_parameters)

    print("Evolution finished after %d function evaluations" %
          (len(evolution_data_array[optimization_metrics[0]])))
    sys.stdout.write_to_logfile(
        ("Evolutionary search time %10.4f sec\n" %
         ((datetime.datetime.now() - evolution_t0).total_seconds())))

    with open(
            deal_with_relative_and_absolute_path(run_directory,
                                                 output_data_file), 'w') as f:
        w = csv.writer(f)
        w.writerow(list(evolution_data_array.keys()))
        tmp_list = [
            param_space.convert_types_to_string(j, evolution_data_array)
            for j in list(evolution_data_array.keys())
        ]
        tmp_list = list(zip(*tmp_list))
        for i in range(len(evolution_data_array[optimization_metrics[0]])):
            w.writerow(tmp_list[i])

    print("### End of the evolutionary search")
예제 #23
0
import space

if __name__ == '__main__':
    earth = dot.Dot()
    earth.mass = constants.EARTH.MASS

    moon = dot.Dot()
    moon.mass = constants.MOON.MASS
    moon.pos[0] = constants.MOON.PERIGEE_ORBIT_RADIUS
    moon.vel[1] = constants.MOON.MAX_ORBITAL_VELOCITY
    moon.vel[2] = 20

    earth.vel[
        1] = -constants.MOON.MAX_ORBITAL_VELOCITY * moon.mass / earth.mass

    test_space = space.Space(dots=[earth, moon])

    print()
    print(test_space)

    n_loops = 3
    n_iterations = 1000
    results = numpy.zeros(shape=(n_iterations, 12))
    dt = n_loops * constants.MOON.SIDEREAL_ROTATION_PERIOD / n_iterations
    for iteration in range(n_iterations):
        test_space.step(time=dt)
        results[iteration, 0:3] = moon.acc
        results[iteration, 3:6] = moon.vel
        results[iteration, 6:9] = moon.pos
        results[iteration, 9:12] = earth.pos
예제 #24
0
def deviation(dvx,dvy, dist):
    sp = space.Space()

    count = 0
    planetmass = 1.0 / G
    pl = planet.Planet(planetmass)
    sp.planet = pl

    unitmass = 1.0
    ut = unit.Unit(np.array([0.9,0.0,0.0]),np.array([0.0+dvx,1.0+dvy,0.0]), unitmass)
    sp.unit = ut

    t = 0

    angle = 0
    dt = 0.01 # simulation step
    pos0 = np.array([1,0.0,0.0])
    pos = ut.pos

    E0 = ut.getEnergy(planetmass)
    A0 = ut.vectorLenc(planetmass)
    U0 = ut.getPotentialEnergy(planetmass)
    KO = ut.getKinectEnergy()

#----------------Arrays of physical quantities ----------------
    arrE = []
    arrt = []
    arrT = []
    arrX = []
    arrY = []
    arrU = []
    arrK = []
    arrA = []
    arrM = []
    arrM_full = []
    arrMx = []
    arrMy = []
    arrMz = []
    arrAx = []
    arrAy = []
    arrAz = []
    arrAngle = []
    arrA_full = []
#------------------Simulation cycle-----------------------------
    n = 10 # number of circles
    while (t<(2*math.pi)*n): #one circle
        sp.step_rot(dt, dist, angle)
        count +=1
        t+=dt
        angle+= math.degrees(0.001/57)
        E = ut.getEnergy(planetmass)
        U = ut.getPotentialEnergy(planetmass)
        K = ut.getKinectEnergy()
        A = ut.vectorLenc(planetmass)
        M = ut.getMomentum()
        Angle = math.atan2(ut.vectorLenc(planetmass)[0],ut.vectorLenc(planetmass)[1])
        T = t / (2 * math.pi) # number of orbits done
#-----------------Filling the arrays----------------------------
        arrK.append(K)
        arrE.append(E)
        arrU.append(U)
        arrt.append(t)
        arrT.append(T)
        arrX.append(ut.pos[0])
        arrY.append(ut.pos[1])
        arrA.append(A)
        arrAx.append(A[0])
        arrAy.append(A[1])
        arrAz.append(A[2])
        arrA_full.append(A[0]+A[1]+A[2])
        arrMx.append(M[0])
        arrMy.append(M[1])
        arrMz.append(M[2])
        arrM_full.append(M[0]+M[1]+M[2])
        arrAngle.append(Angle)
#--------------------------------------------------------------
        #plt.plot(lnh, lnE)
#------------------	graph plotting-----------------------------
    plt.figure(1)
    plt.xlabel('Number of circles')
    plt.ylabel('Potential Energy')
    plt.title('Potential Energy of the spacecraft')
    plt.axis([0, n, 0, 2])
    plt.grid(True)
    plt.plot(arrT,arrU)
    #plt.plot(arrX,arrY)

    plt.figure(2)
    plt.xlabel('Number of circles')
    plt.ylabel('Angle')
    plt.grid(True)
    plt.plot(arrT, arrAngle)

    plt.figure(3)
    plt.xlabel('Number of circles')
    plt.ylabel('E')
    plt.title('Total Energy of the spacecraft')
    plt.axis([0, n, -0.7, -0.3])
    plt.grid(True)
    plt.plot(arrT, arrE)

    plt.figure(4)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Orbit of the spacecraft')
    plt.axis([-1.5, 1.5, -1.5, 1.5])
    plt.grid(True)
    plt.plot(arrX, arrY)

    plt.figure(5)
    plt.axis([0, n, -1, 2])
    plt.grid(True)
    plt.plot(arrT, arrA_full)
    plt.xlabel('Number of circles')
    plt.ylabel('A')
    plt.title('Laplace–Runge–Lenz vector state ')

    plt.figure(6)
    plt.axis([0, n, -1, 2])
    plt.grid(True)
    fig, ax = plt.subplots()
    ax.plot(arrT, arrAx, label ='x_component')
    ax.plot(arrT, arrAy, label ='y_component')
    ax.plot(arrT, arrAz, label ='z_component')
    plt.xlabel('Number of circles')
    plt.ylabel('A')
    plt.title('Laplace–Runge–Lenz vector components state ')
    legend = ax.legend(loc='upper right', shadow=True)

    frame = legend.get_frame()
    frame.set_facecolor('0.90')

    plt.figure(7)
    plt.axis([0, n, -1, 2])
    plt.grid(True)
    fig, ay = plt.subplots()
    ay.plot(arrT, arrMx, label='x_component')
    ay.plot(arrT, arrMy, label='y_component')
    ay.plot(arrT, arrMz, label='z_component')
    plt.xlabel('Number of circles')
    plt.ylabel('A')
    plt.title('Angular momentum  state ')
    legend = ay.legend(loc='upper right', shadow=True)
    frame = legend.get_frame()
    frame.set_facecolor('0.90')

    # Set the fontsize
    for label in legend.get_texts():
        label.set_fontsize('large')

    for label in legend.get_lines():
        label.set_linewidth(1.5)  # the legend line width

    plt.show()
#--------------------------------------------------------------

    M = ut.getMomentum()


    #delta = ((E - E0)**2) + vectors.squarelength(M - M0) + vectors.squarelength(A - A0)
    delta = ((E - E0) ** 2) + vectors.squarelength(A - A0)

    return delta
예제 #25
0
def main(parameters_file, list_of_pairs_of_files=[], image_output_file=None):
    """
    Plot the results of the previously run design space exploration.
    """

    show_samples = False

    filename, file_extension = os.path.splitext(parameters_file)
    if file_extension != ".json":
        print(
            "Error: invalid file name. \nThe input file has to be a .json file not a %s"
            % file_extension)
        exit(1)
    with open(parameters_file, 'r') as f:
        config = json.load(f)

    json_schema_file = 'scripts/schema.json'
    with open(json_schema_file, 'r') as f:
        schema = json.load(f)

    DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)
    DefaultValidatingDraft4Validator(schema).validate(config)

    application_name = config["application_name"]
    optimization_metrics = config["optimization_objectives"]
    feasible_output = config["feasible_output"]
    feasible_output_name = feasible_output["name"]
    run_directory = config["run_directory"]

    xlog = config["output_image"]["image_xlog"]
    ylog = config["output_image"]["image_ylog"]

    if "optimization_objectives_labels_image_pdf" in config["output_image"]:
        optimization_objectives_labels_image_pdf = config["output_image"][
            "optimization_objectives_labels_image_pdf"]
    else:
        optimization_objectives_labels_image_pdf = optimization_metrics

    # Only consider the files in the json file if there are no input files.
    if list_of_pairs_of_files == []:
        output_pareto_file = config["output_pareto_file"]
        if output_pareto_file == "output_pareto.csv":
            output_pareto_file = application_name + "_" + output_pareto_file
        output_data_file = config["output_data_file"]
        if output_data_file == "output_samples.csv":
            output_data_file = application_name + "_" + output_data_file
        list_of_pairs_of_files.append(
            (deal_with_relative_and_absolute_path(run_directory,
                                                  output_pareto_file),
             deal_with_relative_and_absolute_path(run_directory,
                                                  output_data_file)))

    if image_output_file != None:
        output_image_pdf_file = image_output_file
        filename = os.path.basename(image_output_file)
        path = os.path.dirname(image_output_file)
        if path == "":
            output_image_pdf_file_with_all_samples = "all_" + filename
        else:
            output_image_pdf_file_with_all_samples = path + "/" + "all_" + filename
    else:
        tmp_file_name = config["output_image"]["output_image_pdf_file"]
        if tmp_file_name == "output_pareto.pdf":
            tmp_file_name = application_name + "_" + tmp_file_name
        output_image_pdf_file = deal_with_relative_and_absolute_path(
            run_directory, tmp_file_name)

        if tmp_file_name[0] == "/":
            filename = os.path.basename(output_image_pdf_file)
            path = os.path.dirname(output_image_pdf_file)
            output_image_pdf_file_with_all_samples = path + "/" + "all_" + filename
        else:
            output_image_pdf_file_with_all_samples = str(run_directory + "/" +
                                                         "all_" +
                                                         tmp_file_name)

    str_files = ""
    for e in list_of_pairs_of_files:
        str_files += str(e[0] + " " + e[1] + " ")

    print("######### plot_dse.py ##########################")
    print("### Parameters file is %s" % parameters_file)
    print("### The Pareto and DSE data files are: %s" % str_files)
    print("### The first output pdf image is %s" % output_image_pdf_file)
    print("### The second output pdf image is %s" %
          output_image_pdf_file_with_all_samples)
    print("################################################")

    param_space = space.Space(config)

    xelem = optimization_metrics[0]
    yelem = optimization_metrics[1]
    handler_map_for_legend = {}
    xlabel = optimization_objectives_labels_image_pdf[0]
    ylabel = optimization_objectives_labels_image_pdf[1]

    x_max = float("-inf")
    x_min = float("inf")
    y_max = float("-inf")
    y_min = float("inf")

    print_legend = True
    fig = plt.figure()
    ax1 = plt.subplot(1, 1, 1)

    if xlog:
        ax1.set_xscale('log')
    if ylog:
        ax1.set_yscale('log')

    objective_1_max = objective_2_max = 1
    objective_1_is_percentage = objective_2_is_percentage = False
    if "objective_1_max" in config["output_image"]:
        objective_1_max = config["output_image"]["objective_1_max"]
        objective_1_is_percentage = True
    if "objective_2_max" in config["output_image"]:
        objective_2_max = config["output_image"]["objective_2_max"]
        objective_2_is_percentage = True

    input_data_array = {}
    fast_addressing_of_data_array = {}
    non_valid_optimization_obj_1 = defaultdict(list)
    non_valid_optimization_obj_2 = defaultdict(list)

    for file_pair in list_of_pairs_of_files:  # file_pair is tuple containing: (pareto file, DSE file)
        next_color = get_next_color()

        #############################################################################
        ###### Load data from files and do preprocessing on the data before plotting.
        #############################################################################
        for file in file_pair:
            print(("Loading data from %s ..." % file))
            input_data_array[file], fast_addressing_of_data_array[
                file] = param_space.load_data_file(file, debug)
            if input_data_array[file] == None:
                print("Error: no data found in input data file: %s. \n" %
                      file_pair[1])
                exit(1)
            if (xelem not in input_data_array[file]) or (
                    yelem not in input_data_array[file]):
                print(
                    "Error: the optimization variables have not been found in input data file %s. \n"
                    % file)
                exit(1)
            print(("Parameters are " +
                   str(list(input_data_array[file].keys())) + "\n"))
            input_data_array[file][xelem] = [
                float(input_data_array[file][xelem][i]) / objective_1_max
                for i in range(len(input_data_array[file][xelem]))
            ]
            input_data_array[file][yelem] = [
                float(input_data_array[file][yelem][i]) / objective_2_max
                for i in range(len(input_data_array[file][yelem]))
            ]

            if objective_1_is_percentage:
                input_data_array[file][xelem] = [
                    input_data_array[file][xelem][i] * 100
                    for i in range(len(input_data_array[file][xelem]))
                ]
            if objective_2_is_percentage:
                input_data_array[file][yelem] = [
                    input_data_array[file][yelem][i] * 100
                    for i in range(len(input_data_array[file][yelem]))
                ]

            x_max, x_min, y_max, y_min = compute_min_max_samples(
                input_data_array[file], x_max, x_min, xelem, y_max, y_min,
                yelem)

            input_data_array_size = len(input_data_array[file][list(
                input_data_array[file].keys())[0]])
            print("Size of the data file %s is %d" %
                  (file, input_data_array_size))

        file_pareto = file_pair[0]  # This is the Pareto file
        file_search = file_pair[1]  # This is the DSE file

        ######################################################################################################
        ###### Compute invalid samples to be plot in a different color (and remove them from the data arrays).
        ######################################################################################################
        if show_samples:
            i = 0
            for ind in range(len(input_data_array[file][yelem])):
                if input_data_array[file][feasible_output_name][i] == False:
                    non_valid_optimization_obj_2[file_search].append(
                        input_data_array[file][yelem][i])
                    non_valid_optimization_obj_1[file_search].append(
                        input_data_array[file][xelem][i])
                    for key in list(input_data_array[file].keys()):
                        del input_data_array[file][key][i]
                else:
                    i += 1

            label_is = get_last_dir_and_file_names(file_pareto)
            all_samples, = plt.plot(input_data_array[file_search][xelem],
                                    input_data_array[file_search][yelem],
                                    color=next_color,
                                    linestyle='None',
                                    marker='.',
                                    mew=0.5,
                                    markersize=3,
                                    fillstyle="none",
                                    label=label_is)
            plt.plot(input_data_array[file_pareto][xelem],
                     input_data_array[file_pareto][yelem],
                     linestyle='None',
                     marker='.',
                     mew=0.5,
                     markersize=3,
                     fillstyle="none")
            handler_map_for_legend[all_samples] = HandlerLine2D(numpoints=1)

        ################################################################################################################
        ##### Create a straight Pareto plot: we need to add one point for each point of the data in paretoX and paretoY.
        ##### We also need to reorder the points on the x axis first.
        ################################################################################################################
        straight_pareto_x = list()
        straight_pareto_y = list()
        if len(input_data_array[file_pareto][xelem]) != 0:
            data_array_pareto_x, data_array_pareto_y = (list(t) for t in zip(
                *sorted(
                    zip(input_data_array[file_pareto][xelem],
                        input_data_array[file_pareto][yelem]))))
            for j in range(len(data_array_pareto_x)):
                straight_pareto_x.append(data_array_pareto_x[j])
                straight_pareto_x.append(data_array_pareto_x[j])
                straight_pareto_y.append(data_array_pareto_y[j])
                straight_pareto_y.append(data_array_pareto_y[j])
            straight_pareto_x.append(
                x_max)  # Just insert the max on the x axis
            straight_pareto_y.insert(
                0, y_max)  # Just insert the max on the y axis

        label_is = "Pareto - " + get_last_dir_and_file_names(file_pareto)

        pareto_front, = plt.plot(straight_pareto_x,
                                 straight_pareto_y,
                                 label=label_is,
                                 linewidth=1,
                                 color=next_color)
        handler_map_for_legend[pareto_front] = HandlerLine2D(numpoints=1)

        label_is = "Invalid Samples - " + get_last_dir_and_file_names(
            file_search)
        if show_samples:
            non_valid, = plt.plot(non_valid_optimization_obj_1[file_search],
                                  non_valid_optimization_obj_2[file_search],
                                  linestyle='None',
                                  marker='.',
                                  mew=0.5,
                                  markersize=3,
                                  fillstyle="none",
                                  label=label_is)
            handler_map_for_legend[non_valid] = HandlerLine2D(numpoints=1)

    plt.ylabel(ylabel, fontsize=16)
    plt.xlabel(xlabel, fontsize=16)
    for tick in ax1.xaxis.get_major_ticks():
        tick.label.set_fontsize(
            14)  # Set the fontsize of the label on the ticks of the x axis
    for tick in ax1.yaxis.get_major_ticks():
        tick.label.set_fontsize(
            14)  # Set the fontsize of the label on the ticks of the y axis

    # Add the legend with some customizations
    if print_legend:
        lgd = ax1.legend(handler_map=handler_map_for_legend,
                         loc='best',
                         bbox_to_anchor=(1, 1),
                         fancybox=True,
                         shadow=True,
                         ncol=1,
                         prop={'size': 14})  # Display legend.

    font = {'size': 16}
    matplotlib.rc('font', **font)

    fig.savefig(output_image_pdf_file_with_all_samples,
                dpi=120,
                bbox_inches='tight')

    if objective_1_is_percentage:
        plt.xlim(0, 100)
    if objective_2_is_percentage:
        plt.ylim(0, 100)

    fig.savefig(output_image_pdf_file, dpi=120, bbox_inches='tight')
예제 #26
0
def main(configuration_file,
         data_dirs,
         labels=None,
         minimum=0,
         outfile=None,
         title=None,
         plot_log=False,
         unlog_y_axis=False,
         budget=None,
         out_dir=None,
         ncol=4,
         x_label=None,
         y_label=None,
         show_doe=True,
         expert_configuration=None):

    # Read json configuration file
    if not configuration_file.endswith('.json'):
        _, file_extension = splitext(configuration_file)
        print(
            "Error: invalid file name. \nThe input file has to be a .json file not a %s"
            % file_extension)
        raise SystemExit
    with open(configuration_file, 'r') as f:
        config = json.load(f)

    json_schema_file = 'scripts/schema.json'
    with open(json_schema_file, 'r') as f:
        schema = json.load(f)

    DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)
    try:
        DefaultValidatingDraft4Validator(schema).validate(config)
    except exceptions.ValidationError as ve:
        print("Failed to validate json:")
        print(ve)
        raise SystemExit

    param_space = space.Space(config)
    output_metric = param_space.get_optimization_parameters()[
        0]  # only works for mono-objective
    doe_size = config['design_of_experiment']['number_of_samples']
    feasibility_flag = param_space.get_feasible_parameter()[
        0]  # returns a list, we just want the name

    best = 0
    if minimum is not None:
        best = minimum
    application_name = config["application_name"]

    if budget is None:
        budget = float("inf")

    regrets = {}
    log_regrets = {}
    total_iterations = {}
    max_iters = float("-inf")
    for data_dir_idx, data_dir in enumerate(data_dirs):
        dir_regrets = []
        dir_log_regrets = []
        min_dir_iters = budget
        for file in listdir(data_dir):
            if not file.endswith('.csv'):
                print("Skipping:", file)
                continue
            full_file = join(data_dir, file)
            data_array = load_data(full_file)
            total_iters = min(len(data_array[output_metric]), budget)
            min_dir_iters = min(total_iters, min_dir_iters)
            max_iters = max(max_iters, total_iters)
            iterations = list(range(total_iters))
            simple_regret = []
            log_regret = []
            incumbent = float("inf")
            for idx in iterations:
                if feasibility_flag is not None:
                    if data_array[feasibility_flag][idx] == True:
                        incumbent = min(incumbent,
                                        data_array[output_metric][idx])
                else:
                    incumbent = min(incumbent, data_array[output_metric][idx])
                regret = incumbent - best
                simple_regret.append(regret)
                log_regret.append(np.log(regret))
            dir_regrets.append(np.array(simple_regret))
            dir_log_regrets.append(np.array(log_regret))

        for idx in range(len(dir_regrets)):
            dir_regrets[idx] = dir_regrets[idx][:min_dir_iters]
            dir_log_regrets[idx] = dir_log_regrets[idx][:min_dir_iters]

        regrets[data_dir] = np.array(dir_regrets)
        log_regrets[data_dir] = np.array(dir_log_regrets)
        total_iterations[data_dir] = list(range(min_dir_iters))

    mpl.rcParams.update({'font.size': 40})
    plt.rcParams["figure.figsize"] = [16, 12]
    linewidth = 6
    fig, ax = plt.subplots()
    colors = [
        "red", "green", "blue", "magenta", "yellow", "purple", "orange", "cyan"
    ]
    legend_elements = []
    if expert_configuration is not None:
        if plot_log:
            expert_configuration = np.log(expert_configuration)
        expert_data = [expert_configuration] * max_iters
        plt.plot(list(range(max_iters)),
                 expert_data,
                 color="black",
                 linewidth=linewidth,
                 linestyle="solid")

    for key_idx, key in enumerate(regrets.keys()):
        std = np.std(regrets[key], axis=0, ddof=1)
        log_std = np.std(log_regrets[key], axis=0, ddof=1)
        simple_means = np.mean(regrets[key], axis=0)
        log_means = np.log(simple_means)
        lower_bound = []
        upper_bound = []
        plot_means = simple_means
        plot_stds = std
        if plot_log:
            plot_means = log_means
            plot_stds = log_std

        for idx in range(plot_stds.shape[0]):
            lower_bound.append(plot_means[idx] - plot_stds[idx])
            upper_bound.append(plot_means[idx] + plot_stds[idx])

        next_color = colors[key_idx % len(colors)]
        plt.plot(total_iterations[key],
                 plot_means,
                 color=next_color,
                 linewidth=linewidth)
        plt.fill_between(total_iterations[key],
                         lower_bound,
                         upper_bound,
                         color=next_color,
                         alpha=.2)

        if labels is None:
            label = key
        else:
            label = labels[key_idx]

        legend_elements.append(
            Line2D([0], [0],
                   color=next_color,
                   label=label,
                   linewidth=linewidth))

    if expert_configuration is not None:
        legend_elements.append(
            Line2D([0], [0],
                   color="black",
                   linewidth=linewidth,
                   linestyle="solid",
                   label="Expert Configuration"))

    if plot_log and unlog_y_axis:
        locs, plt_labels = plt.yticks()
        plt_labels = [np.exp(float(item)) for item in locs]
        plt_labels = ["{0:,.2f}\n".format(item) for item in plt_labels]
        plt.yticks(locs, plt_labels)

    if show_doe:
        legend_elements.append(
            Line2D([0], [0],
                   color="black",
                   linewidth=linewidth,
                   linestyle="dashed",
                   label="Initialization"))
    plt.legend(handles=legend_elements,
               loc='center',
               bbox_to_anchor=(0.5, 1.08),
               fancybox=True,
               shadow=True,
               ncol=ncol,
               bbox_transform=plt.gcf().transFigure)

    if x_label is None:
        x_label = "Number of Evaluations"
    if y_label is None:
        if plot_log:
            y_label = "Log Regret"
        else:
            y_label = "Regret"
    plt.xlabel(x_label)
    plt.ylabel(y_label)

    if title is None:
        title = config["application_name"]
    plt.title(title, y=1)

    plt.xlim(0, )
    plt.axvline(x=doe_size,
                color="black",
                linewidth=linewidth,
                linestyle="dashed")

    if out_dir != "":
        if not out_dir.endswith("/"):
            out_dir += "/"
        os.makedirs(out_dir, exist_ok=True)
    if outfile is None:
        outfile = out_dir + application_name + "_regret.pdf"
    plt.savefig(outfile, bbox_inches='tight', dpi=300)
    plt.gcf().clear()

    return legend_elements
예제 #27
0
def main(config, black_box_function=None, output_file=""):
    """
    Run design-space exploration using random scalarizations.
    :param config: dictionary containing all the configuration parameters of this design-space exploration.
    :param output_file: a name for the file used to save the dse results.
    :return:
    """
    param_space = space.Space(config)

    run_directory = config["run_directory"]
    application_name = config["application_name"]
    hypermapper_mode = config["hypermapper_mode"]["mode"]

    if hypermapper_mode == "default":
        if black_box_function == None:
            print("Error: the black box function must be provided")
            raise SystemExit
        if not callable(black_box_function):
            print("Error: the black box function parameter is not callable")
            raise SystemExit

    optimization_metrics = config["optimization_objectives"]
    number_of_objectives = len(optimization_metrics)
    number_of_cpus = config["number_of_cpus"]
    local_search_random_points = config["local_search_random_points"]
    local_search_evaluation_limit = config["local_search_evaluation_limit"]
    if local_search_evaluation_limit == -1:
        local_search_evaluation_limit = float("inf")
    scalarization_key = config["scalarization_key"]
    scalarization_method = config["scalarization_method"]
    scalarization_weights = config["local_search_scalarization_weights"]
    if len(scalarization_weights) < len(optimization_metrics):
        print("Error: not enough scalarization weights. Received",
              len(scalarization_weights), "expected",
              len(optimization_metrics))
        raise SystemExit
    if sum(scalarization_weights) != 1:
        sys.stdout.write_to_logfile(
            "Weights must sum 1. Normalizing weights.\n")
        for idx in range(len(scalarization_weights)):
            scalarization_weights[idx] = scalarization_weights[idx] / sum(
                scalarization_weights)
        sys.stdout.write_to_logfile("New weights:" +
                                    str(scalarization_weights) + "\n")
    objective_weights = {}
    objective_limits = {}
    for idx, objective in enumerate(optimization_metrics):
        objective_weights[objective] = scalarization_weights[idx]
        objective_limits[objective] = [float("inf"), float("-inf")]

    exhaustive_search_data_array = None
    exhaustive_search_fast_addressing_of_data_array = None
    if (hypermapper_mode == 'exhaustive'):
        exhaustive_file = config["hypermapper_mode"]["exhaustive_search_file"]
        print("Exhaustive mode, loading data from %s ..." % exhaustive_file)
        exhaustive_search_data_array, exhaustive_search_fast_addressing_of_data_array = param_space.load_data_file(
            exhaustive_file, debug=False, number_of_cpus=number_of_cpus)

    enable_feasible_predictor = False
    if "feasible_output" in config:
        feasible_output = config["feasible_output"]
        feasible_output_name = feasible_output["name"]
        enable_feasible_predictor = feasible_output[
            "enable_feasible_predictor"]
        enable_feasible_predictor_grid_search_on_recall_and_precision = feasible_output[
            "enable_feasible_predictor_grid_search_on_recall_and_precision"]
        feasible_predictor_grid_search_validation_file = feasible_output[
            "feasible_predictor_grid_search_validation_file"]
        feasible_parameter = param_space.get_feasible_parameter()

    local_search_starting_points = config["local_search_starting_points"]

    debug = False

    log_file = deal_with_relative_and_absolute_path(run_directory,
                                                    config["log_file"])
    sys.stdout.change_log_file(log_file)
    if hypermapper_mode == "client-server":
        sys.stdout.switch_log_only_on_file(True)

    if output_file == "":
        output_data_file = config["output_data_file"]
        if output_data_file == "output_samples.csv":
            output_data_file = application_name + "_" + output_data_file
    else:
        output_data_file = output_file

    absolute_configuration_index = 0
    fast_addressing_of_data_array = {}
    local_search_fast_addressing_of_data_array = {}
    local_search_data_array = defaultdict(list)

    beginning_of_time = param_space.current_milli_time()

    optimization_function_parameters = {}
    optimization_function_parameters['hypermapper_mode'] = hypermapper_mode
    optimization_function_parameters['param_space'] = param_space
    optimization_function_parameters['beginning_of_time'] = beginning_of_time
    optimization_function_parameters['run_directory'] = run_directory
    optimization_function_parameters[
        'exhaustive_search_data_array'] = exhaustive_search_data_array
    optimization_function_parameters[
        'exhaustive_search_fast_addressing_of_data_array'] = exhaustive_search_fast_addressing_of_data_array
    optimization_function_parameters['black_box_function'] = black_box_function
    optimization_function_parameters['number_of_cpus'] = number_of_cpus
    optimization_function_parameters[
        'local_search_data_array'] = local_search_data_array
    optimization_function_parameters[
        'fast_addressing_of_data_array'] = local_search_fast_addressing_of_data_array
    optimization_function_parameters[
        'evaluation_limit'] = local_search_evaluation_limit
    optimization_function_parameters[
        'scalarization_weights'] = objective_weights
    optimization_function_parameters['objective_limits'] = objective_limits
    optimization_function_parameters[
        'scalarization_method'] = scalarization_method
    optimization_function_parameters[
        'enable_feasible_predictor'] = enable_feasible_predictor

    print("Starting local search...")
    local_search_t0 = datetime.datetime.now()
    all_samples, best_configuration = local_search(
        local_search_starting_points, local_search_random_points, param_space,
        fast_addressing_of_data_array, enable_feasible_predictor,
        run_objective_function, optimization_function_parameters,
        scalarization_key)

    print("Local search finished after %d function evaluations" %
          (len(local_search_data_array[optimization_metrics[0]])))
    sys.stdout.write_to_logfile(
        ("Local search time %10.4f sec\n" %
         ((datetime.datetime.now() - local_search_t0).total_seconds())))

    with open(
            deal_with_relative_and_absolute_path(run_directory,
                                                 output_data_file), 'w') as f:
        w = csv.writer(f)
        w.writerow(list(local_search_data_array.keys()))
        tmp_list = [
            param_space.convert_types_to_string(j, local_search_data_array)
            for j in list(local_search_data_array.keys())
        ]
        tmp_list = list(zip(*tmp_list))
        for i in range(len(local_search_data_array[optimization_metrics[0]])):
            w.writerow(tmp_list[i])

    print("### End of the local search.")
예제 #28
0
import space as _space
from space.time import Hour, Day, Cycle

space = _space.Space()
system = _space.System()
space.systems.append(system)
star = _space.Star()
system.stars.append(star)
planet = _space.Planet()
star.bodies.append(planet)

masterdam = _space.city.City("Masterdam")
planet.cities.append(masterdam)

import specification as _spec

cook = _spec.Profession(_space.city.agent.Population, 'cook')
farmer = _spec.Profession(_space.city.agent.Population, 'farmer')
worker = _spec.Profession(_space.city.agent.Population, 'worker')
engineer = _spec.Profession(_space.city.agent.Population, 'engineer')

workers = {
    cook: 2,
    farmer: 1,
    worker: 1,
    engineer: 1,
}

for type, amount in workers.iteritems():
    masterdam.actors.append(type.create(amount))
예제 #29
0
import matplotlib.pyplot as plt
from scipy import special as sp
from constants import eps_0, mu_0, c
import space
import source
import dielectric
import measurement

# Defining experiment parameters
J0 = 1
simulation_time = 4 * 10**(-9)
# PEC box parameters
x_length, y_length = 0.35, 0.35  # [m] (i.c. approximately 30 wavelengths)

# Initializing a space with a PEC bounding box
box = space.Space(x_length, y_length, simulation_time)
src = source.Gaussian_pulse(1 / 2 * x_length, 1 / 2 * y_length, J0,
                            4 * 10**(-10), 10**(-10))
box.set_source(src)

Delta_p = src.get_lambda_min(1) / 25
Delta_t = 1 / (3 * c * np.sqrt(2 / Delta_p**2))
box.define_discretization(Delta_p, Delta_p, Delta_t)
print(Delta_p, Delta_t)

# Measurement parameters
measurement_points = [(1 / 2 * x_length, 1 / 2 * y_length)]
measurement_titles = ["at source"]

# Getting measurments
box.add_measurement_points(measurement_points, measurement_titles)
예제 #30
0
    i = 0
    while True:
        t = time.time() - t0

        if i % (1 * FPS) == 0:
            space.add_particle(particle.Particle((0, 0),
                                                 (0, random.random() * 20.0),
                                                  collides=False))

        # if i % 16 == 0:
        #   FLOOR.collides = False

        # if i % 25 == 0:
        #    FLOOR.collides = True


        space.update()
        space.print_state()
        time.sleep(1.0/FPS)

        i += 1


if __name__ == "__main__":
    s = space.Space(9.81)

    s.add_particle(FLOOR)

    simulate_debug(s)