def __init__(self, optimizer, problems, num_runs, max_evaluations, weights=None): """ Create object instance. :param optimizer: Optimizer-class, e.g. PSO, MOL or DE. :param problems: List of instances of the Problem-class. :param num_runs: Number of optimization runs to perform for each problem. :param max_evaluations: Number of fitness evaluations for each optimization run. :param weights: List of weights for the problems to adjust their mutual importance. :return: Object instance. """ # Copy arguments to instance variables. self.optimizer = optimizer self.num_runs = num_runs self.max_evaluations = max_evaluations # Wrap the problems and weights. This is used for ranking the problems # which significantly speeds up the execution time, as explained below. if weights is None: # No weights were supplied so we just wrap the problems. self.problem_ranks = [_ProblemRank(problem) for problem in problems] else: # Wrap both the problems and weights. self.problem_ranks = [_ProblemRank(problem, weight) for problem, weight in zip(problems, weights)] # The MetaFitness-class is actually an optimization problem, # so init the parent-class. # The dimensionality of the search-space is the number of # control parameters for the optimizer, and the search-space # boundaries are the boundaries for the control parameters. Problem.__init__(self, name="MetaFitness", dim=optimizer.num_parameters, fitness_min=0.0, lower_bound=optimizer.parameters_lower_bound, upper_bound=optimizer.parameters_upper_bound)
eta_q= np.array([-0.250 , -0.250]) * 2 * np.pi parameter = [frequency,coupling,eta_q] QB = Qubits(qubits_parameter = parameter) ZZ = QB.E_eig[QB._findstate('11')]-QB.E_eig[QB._findstate('01')]-QB.E_eig[QB._findstate('10')]+QB.E_eig[QB._findstate('00')] # swarmops func = partial(getfid , QB = QB , parallel = False , limit = np.Infinity) lower_bound = [20 , 0.02 , (5.2-0.152) ] upper_bound = [150 , 0.40 , (5.2-0.148) ] lower_init=[20 , 0.1 , (5.2-0.152) ] upper_init=[90 , 0.2 , (5.2-0.148) ] problem = Problem(name="CNOT_OPT_"+str(index), dim=3, fitness_min=0.0, lower_bound=lower_bound, upper_bound=upper_bound, lower_init=lower_init, upper_init=upper_init, func=func) print('start') optimizer = SuSSADE parameters = [20, 0.3, 0.9 , 0.9 ] # Start a timer. timer = Timer() # Perform a single optimization run using the optimizer # where the fitness is evaluated in parallel. result = optimizer(parallel=True, problem=problem, max_evaluations=500, display_interval=1,
# P = [65.12955 , 0.034855 , 5.049587 , -0.135331 , 0.319984 , 0] # fid = getfid(P , parallel = True) # print(1-fid) # swarmops func = partial(getfid, parallel=False, limit=np.Infinity) lower_bound = [40, 0.02, (5.2 - 0.152), -np.pi, -np.pi] upper_bound = [160, 0.15, (5.2 - 0.148), np.pi, np.pi] lower_init = [40, 0.02, (5.2 - 0.152), -np.pi, -np.pi] upper_init = [160, 0.15, (5.2 - 0.148), np.pi, np.pi] problem = Problem(name="CNOT_OPT", dim=5, fitness_min=0.0, lower_bound=lower_bound, upper_bound=upper_bound, lower_init=lower_init, upper_init=upper_init, func=func) print('start') optimizer = SuSSADE parameters = [20, 0.3, 0.9, 0.9] # Start a timer. timer = Timer() # Perform a single optimization run using the optimizer # where the fitness is evaluated in parallel. result = optimizer(parallel=True, problem=problem,
# Max number of fitness evaluations. max_evaluations = dim * 10 # Number of fitness evaluations between printing of status messages. display_interval = 1 # Max-length of fitness trace. trace_len = 100 # Create object-instance for a benchmark problem. #problem = Problem.Ackley(dim=dim) #problem = Problem.Sphere(dim=dim) #problem = Problem.Griewank(dim=dim) #problem = Problem.Rastrigin(dim=dim) problem = Problem.Rosenbrock(dim=dim) #problem = Problem.Schwefel1_2(dim=dim) #problem = Problem.Schwefel2_21(dim=dim) #problem = Problem.Schwefel2_22(dim=dim) #problem = Problem.Step(dim=dim) #problem = Problem.QuarticNoise(dim=dim) #problem = Problem.Penalized1(dim=dim) #problem = Problem.Penalized2(dim=dim) # Example of how to wrap a function in a problem-object. if False: problem = Problem.Benchmark(name="Sphere Wrap Func", dim=dim, fitness_min=0.0, lower_bound=-100.0, upper_bound=100.0, lower_init=50.0, upper_init=100.0, func=sphere_func)