def plot_simulated(func_name: str, n_simulations: int, max_resources: int = 81, n_resources: Optional[int] = None,
                   shape_families: Tuple[ShapeFamily, ...] = (ShapeFamily(None, 0.9, 10, 0.1),),
                   init_noise: float = 0, scheduler: Type[ShapeFamilyScheduler] = RoundRobinShapeFamilyScheduler) \
        -> List[List[float]]:
    """plots the surface of the values of simulated loss functions at n_resources, by default is plot the losses at max
    resources, in which case their values would be 200-branin (if necessary aggressiveness is not disabled)

    :param func_name: optimization function name eg. branin, egg
    :param n_simulations: number of simulations
    :param max_resources: maximum number of resources
    :param n_resources: show the surface of the values of simulated loss functions at n_resources
                        Note that this is relative to max_resources
    :param shape_families: shape families
    :param init_noise: variance of initial noise
    :param scheduler: class not instance of a scheduler
    """
    simulator = OptFunctionSimulationProblem(func_name)
    if n_resources is None:
        n_resources = max_resources
    assert n_resources <= max_resources
    scheduler = scheduler(shape_families, max_resources, init_noise)

    loss_functions = []
    for i in range(n_simulations):
        evaluator: OptFunctionSimulationEvaluator = simulator.get_evaluator(
            *scheduler.get_family(), should_plot=True)
        evaluator.evaluate(n_resources=n_resources)
        loss_functions.append(evaluator.fs)

    return loss_functions
Exemple #2
0
    def plot_surface(  # pylint: disable=arguments-differ
            self,
            n_simulations: int,
            max_resources: int = 81,
            n_resources: Optional[int] = None,
            shape_families: Tuple[ShapeFamily,
                                  ...] = (ShapeFamily(None, 0.9, 10, 0.1), ),
            init_noise: float = 0) -> None:
        """plots the surface of the values of simulated loss functions at n_resources, by default is plot the losses at
        max resources, in which case their values would be 200-branin (if necessary aggressiveness is not disabled)

        :param n_simulations: number of simulations
        :param max_resources: maximum number of resources
        :param n_resources: show the surface of the values of simulated loss functions at n_resources
                            Note that this is relative to max_resources
        :param shape_families: shape families
        :param init_noise:
        """
        if n_resources is None:
            n_resources = max_resources
        assert n_resources <= max_resources
        scheduler = RoundRobinShapeFamilyScheduler(shape_families,
                                                   max_resources, init_noise)

        xs, ys, zs = [], [], []
        for _ in range(n_simulations):
            evaluator = self.get_evaluator(
                *scheduler.get_family(),
                should_plot=True)  # type: ignore  # FIXME
            xs.append(evaluator.arm.x)
            ys.append(evaluator.arm.y)
            z = evaluator.evaluate(n_resources=n_resources).fval
            zs.append(z)

        xs, ys, zs = [
            np.array(array, dtype="float64") for array in (xs, ys, zs)
        ]
        fig = plt.figure()
        ax = fig.gca(projection=Axes3D.name)
        surf = ax.plot_trisurf(xs, ys, zs, cmap="coolwarm", antialiased=True)
        fig.colorbar(surf, shrink=0.3, aspect=5)

        plt.show()
def set_master(n_workers: str = '2', eta: str = '3', max_iter: str = '81', _: str = ...) -> None:
    from autotune.benchmarks import OptFunctionSimulationProblem
    global PROBLEM
    PROBLEM = OptFunctionSimulationProblem('rastrigin')
    families_of_shapes_general = (
        ShapeFamily(None, 1.5, 10, 15, False),  # with aggressive start
        ShapeFamily(None, 0.5, 7, 10, False),  # with average aggressiveness at start and at the beginning
        ShapeFamily(None, 0.2, 4, 7, True),  # non aggressive start, aggressive end
        ShapeFamily(None, 1.5, 10, 15, False, 200, 400),  # with aggressive start
        ShapeFamily(None, 0.5, 7, 10, False, 200, 400),  # with average aggressiveness at start and at the beginning
        ShapeFamily(None, 0.2, 4, 7, True, 200, 400),  # non aggressive start, aggressive end

        # ShapeFamily(None, 0, 1, 0, False, 0, 0),  # flat
    )
    global MASTER
    MASTER = Master(n_workers=int(n_workers), eta=int(eta), sampler=TpeOptimiser, max_iter=int(max_iter),
                    min_or_max=min, is_simulation=True,
                    scheduler=UniformShapeFamilyScheduler(families_of_shapes_general, max_resources=81, init_noise=10))
# This will fetch the latest experiment on the following problem with the following optimization method

IS_SIMULATION = False

FILE_PATH = join_path(OUTPUT_DIR, "true_loss_functions.pkl")
MIN_LEN_THRESHOLD = 300
UNDERLYING_OPT_FUNCTION = 'branin'

if __name__ == "__main__":

    ax1, ax2, ax3, ax4 = get_suplots_axes_layout()

    if IS_SIMULATION:

        families_of_shapes = (
            ShapeFamily(None, 0.5, 2, 200),  # with aggressive start
        )

        interval_len = 1 / (1 + len(families_of_shapes))
        for i, fam in enumerate(families_of_shapes):
            simulated_loss_functions = plot_simulated(
                func_name=UNDERLYING_OPT_FUNCTION,
                n_simulations=10,
                max_resources=400,
                n_resources=400,
                shape_families=(fam, ),
                init_noise=1)
            plot_profiles(simulated_loss_functions, ax1, ax2, ax4,
                          interval_len * (i + 1), 13)

    else:
Exemple #5
0
fig.set_tight_layout(True)

# Query the figure's on-screen size and DPI. Note that when saving the figure to
# a file, we need to provide a DPI for that separately.
print('fig size: {0} DPI, size in inches {1}'.format(
    fig.get_dpi(), fig.get_size_inches()))

# Plot a scatter that persists (isn't redrawn) and the initial line.
# x = np.arange(0, 20, 0.1)
# ax.scatter(x, x + np.random.normal(0, 3.0, len(x)))
# line, = ax.plot(x, x - 5, 'r-', linewidth=2)

families_of_shapes_general = (
    # ShapeFamily(None, 1.5, 10, 15, False),  # with aggressive start
    # ShapeFamily(None, 0.72, 5, 0.15),
    ShapeFamily(None, 4.5, 3, 15),
)

ax.set_ylim([-200, 100])
ax.set_xlim([0, MAX_EPOCH])

problem = OptFunctionSimulationProblem('wave')
scheduler = RoundRobinShapeFamilyScheduler(shape_families=families_of_shapes_general,
                                           max_resources=MAX_EPOCH, init_noise=5)
# Update the line and the axes (with a new xlabel). Return a tuple of
# "artists" that have to be redrawn for this frame.
evaluator = problem.get_evaluator(*scheduler.get_family(), should_plot=True)


def update(i):
    label = 'timestep {0}'.format(i)
Exemple #6
0
            else:
                best_in_bracket = self.min_or_max(block.evaluations, key=self._get_optimisation_func_val)
                print(f'Finished bracket {block.bracket}:\n{block}\n',
                      best_in_bracket.evaluator.arm, best_in_bracket.optimisation_goals)

    def _get_optimisation_func_val(self, evaluation: Evaluation) -> float:
        return self.optimisation_func(evaluation.optimisation_goals)


if __name__ == '__main__':
    # INPUT_DIR = "D:/workspace/python/datasets/"
    # OUTPUT_DIR = "D:/workspace/python/datasets/output"
    # from autotune.benchmarks import MnistProblem
    # PROBLEM = MnistProblem(INPUT_DIR, OUTPUT_DIR)

    from autotune.benchmarks import OptFunctionSimulationProblem
    PROBLEM = OptFunctionSimulationProblem('rastrigin')
    families_of_shapes_general = (
        ShapeFamily(None, 1.5, 10, 15, False),  # with aggressive start
        ShapeFamily(None, 0.5, 7, 10, False),  # with average aggressiveness at start and at the beginning
        ShapeFamily(None, 0.2, 4, 7, True),  # non aggressive start, aggressive end
        ShapeFamily(None, 1.5, 10, 15, False, 200, 400),  # with aggressive start
        ShapeFamily(None, 0.5, 7, 10, False, 200, 400),  # with average aggressiveness at start and at the beginning
        ShapeFamily(None, 0.2, 4, 7, True, 200, 400),  # non aggressive start, aggressive end

        # ShapeFamily(None, 0, 1, 0, False, 0, 0),  # flat
    )
    master = Master(n_workers=2, eta=3, sampler=TpeOptimiser, max_iter=81, min_or_max=min, is_simulation=True,
                    scheduler=UniformShapeFamilyScheduler(families_of_shapes_general, max_resources=81, init_noise=10))
    master.run_optimisation(PROBLEM)