Example #1
0
    def __init__(self, mode, bounds, fit, nhawks, int_transform='nearest_int', ncores=1, seed=None):
        self.seed = seed
        if seed:
            random.seed(seed)
            np.random.seed(seed)

        assert mode == 'min' or mode == 'max', "Mode must be 'max' or 'min'."
        self.mode = mode
        self.fit = fit
        self.int_transform=int_transform
        self.ncores = ncores
        self.nhawks = nhawks
        self.dim = len(bounds)
        self.bounds = bounds
        
        #infer variable types 
        self.var_type = np.array([bounds[item][0] for item in bounds])
        
        #mir-grid
        if "grid" in self.var_type:
            self.grid_flag=True
            self.orig_bounds=bounds  #keep original bounds for decoding
            print('--debug: grid parameter type is found in the space')
            self.bounds, self.bounds_map=encode_grid_to_discrete(self.bounds) #encoding grid to int
            #define var_types again by converting grid to int
            self.var_type = np.array([self.bounds[item][0] for item in self.bounds])
        else:
            self.grid_flag=False
            self.bounds = bounds
        
        self.lb = np.array([self.bounds[item][1] for item in self.bounds])
        self.ub = np.array([self.bounds[item][2] for item in self.bounds])
Example #2
0
 def __init__(self, mode, bounds, fit, nbats=50, fmin=0, 
              fmax=1, A=0.5, r0=0.5, alpha=1.0, gamma=0.9, 
              levy='False', int_transform='nearest_int', ncores=1, seed=None):
     
     if seed:
         random.seed(seed)
         np.random.seed(seed)
     
     assert ncores <= nbats, '--error: ncores ({}) must be less than or equal to nbats ({})'.format(ncores, nbats)
     assert nbats >= 5, '--error: number of bats must be more than 5 for this algorithm'
     
     #--mir
     self.mode=mode
     if mode == 'min':
         self.fit=fit
     elif mode == 'max':
         def fitness_wrapper(*args, **kwargs):
             return -fit(*args, **kwargs) 
         self.fit=fitness_wrapper
     else:
         raise ValueError('--error: The mode entered by user is invalid, use either `min` or `max`')
     
     self.int_transform=int_transform
     if int_transform not in ["nearest_int", "sigmoid", "minmax"]:
         raise ValueError('--error: int_transform entered by user is invalid, must be `nearest_int`, `sigmoid`, or `minmax`')
         
     self.bounds=bounds
     self.ncores = ncores
     self.nbats=nbats
     
     self.fmax=fmax
     self.fmin=fmin
     self.A=A
     self.alpha=alpha
     self.gamma=gamma
     self.r0=r0
     self.levy_flight=levy
     
     #infer variable types 
     self.var_type = np.array([bounds[item][0] for item in bounds])
     
     #mir-grid
     if "grid" in self.var_type:
         self.grid_flag=True
         self.orig_bounds=bounds  #keep original bounds for decoding
         print('--debug: grid parameter type is found in the space')
         self.bounds, self.bounds_map=encode_grid_to_discrete(self.bounds) #encoding grid to int
         #define var_types again by converting grid to int
         self.var_type = np.array([self.bounds[item][0] for item in self.bounds])
     else:
         self.grid_flag=False
         self.bounds = bounds
     
     self.dim = len(bounds)
     self.lb=np.array([self.bounds[item][1] for item in self.bounds])
     self.ub=np.array([self.bounds[item][2] for item in self.bounds])
Example #3
0
    def __init__(self,
                 mode,
                 bounds,
                 fit,
                 lambda_=60,
                 mu=30,
                 cxmode='cx2point',
                 alpha=0.5,
                 cxpb=0.6,
                 mutpb=0.3,
                 smin=0.01,
                 smax=0.5,
                 clip=True,
                 ncores=1,
                 seed=None):
        if seed:
            random.seed(seed)
        self.seed = seed
        self.bounds = bounds
        self.nx = len(bounds.keys())
        #--mir
        self.mode = mode
        if mode == 'max':
            self.fit = fit
        elif mode == 'min':

            def fitness_wrapper(*args, **kwargs):
                return -fit(*args, **kwargs)

            self.fit = fitness_wrapper
        else:
            raise ValueError(
                '--error: The mode entered by user is invalid, use either `min` or `max`'
            )

        self.ncores = ncores
        self.smin = smin
        self.smax = smax
        self.cxpb = cxpb
        self.mutpb = mutpb
        self.alpha = alpha
        self.mu = mu
        self.lambda_ = lambda_
        self.cxmode = cxmode
        self.clip = clip
        if not self.cxmode in ['cx2point', 'blend']:
            raise ValueError(
                '--error: the cxmode selected (`{}`) is not available in ES, either choose `cx2point` or `blend`'
                .format(self.cxmode))

        assert self.mu <= self.lambda_, "mu (selected population) must be less than lambda (full population)"
        assert (
            self.cxpb + self.mutpb
        ) <= 1.0, "The sum of the cxpb and mutpb must be smaller or equal to 1.0"
        assert self.ncores >= 1, "Number of cores must be more than or equal 1"

        self.lb = []
        self.ub = []
        self.datatype = []

        #infer variable types
        self.datatype = np.array([bounds[item][0] for item in bounds])

        #mir-grid
        if "grid" in self.datatype:
            self.grid_flag = True
            self.orig_bounds = bounds  #keep original bounds for decoding
            print('--debug: grid parameter type is found in the space')
            self.bounds, self.bounds_map = encode_grid_to_discrete(
                self.bounds)  #encoding grid to int
            #define var_types again by converting grid to int
            self.datatype = np.array(
                [self.bounds[item][0] for item in self.bounds])
        else:
            self.grid_flag = False
            self.bounds = bounds

        self.lb = np.array([self.bounds[item][1] for item in self.bounds])
        self.ub = np.array([self.bounds[item][2] for item in self.bounds])
Example #4
0
    def __init__ (self, mode, bounds, fit, npop, mu=None, #general parameters
                  memory_size=None, alpha_init=0.1, alpha_end=1, alpha_backdoor=0.1, #replay parameters
                  Tmax=10000, chi=0.1, #SA parameters
                  cxpb=0.7, mutpb=0.1,  #ES parameters
                  c1=2.05, c2=2.05, speed_mech='constric', #PSO parameters
                  ncores=1, seed=None): #misc parameters
        
        #--------------------
        #General Parameters
        #--------------------
        if seed:
            random.seed(seed)
            np.random.seed(seed)
        
        self.bounds=bounds
        #--mir
        self.mode=mode
        if mode == 'max':
            self.FIT=fit
        elif mode == 'min':
            self.FIT = globalize(lambda x: fit(x))  #use the function globalize to serialize the nested fit
        else:
            raise ValueError('--error: The mode entered by user is invalid, use either `min` or `max`')
        
        self.ncores=ncores
        self.NPOP=npop
        self.pso_flag=True
        self.ncores=ncores
        if ncores <= 3:
            self.NCORES=1
            self.PROC=False
        else:
            self.PROC=True
            if self.pso_flag:
                self.NCORES=int(ncores/3)
            else:
                self.NCORES=int(ncores/2)
        # option for first-level parallelism       
        #self.PROC=True
        self.SEED=seed
        
        #--------------------
        #Experience Replay
        #--------------------
        self.MODE='prior';  self.ALPHA0=alpha_init;   self.ALPHA1=alpha_end
        #--------------------
        # SA hyperparameters
        #--------------------
        self.TMAX=Tmax;  self.CHI=chi;  self.REPLAY_RATE=alpha_backdoor
        
        #--------------------
        # ES HyperParameters
        #--------------------
        if mu:    
            assert mu < npop, '--error: The value of mu ({}) MUST be less than npop ({})'.format(mu, npop)
            self.MU=mu
        else:
            self.MU=int(npop/2)
        
        self.CXPB=cxpb;  self.MUTPB=mutpb;   self.INDPB=1.0
        
        #--------------------
        # PSO HyperParameters
        #--------------------
        self.C1=c1;   self.C2=c2;   self.SPEED_MECH=speed_mech
        
        #-------------------------------
        #Memory Supply for each method
        #-------------------------------
        self.ES_MEMORY=self.MU
        self.SA_MEMORY=self.NCORES
        self.PSO_MEMORY=self.NPOP-self.MU
        #--------------------
        # Fixed/Derived parameters 
        #--------------------
        self.nx=len(bounds)  #all
        self.memory_size=memory_size
        
        self.COOLING='fast' #SA
        self.TMIN=1  #SA
        self.LAMBDA=self.NPOP #ES
        self.NPAR=self.NPOP #PSO
        self.SMIN = 1/self.nx #ES
        self.SMAX = 0.5  #ES
        self.v0=0.1 #constant to initialize PSO speed, not very important

        #infer variable types 
        self.datatype = np.array([bounds[item][0] for item in bounds])
        
        #mir-grid
        if "grid" in self.datatype:
            self.grid_flag=True
            self.orig_bounds=bounds  #keep original bounds for decoding
            print('--debug: grid parameter type is found in the space')
            self.bounds, self.bounds_map=encode_grid_to_discrete(self.bounds) #encoding grid to int
            #define var_types again by converting grid to int
            self.datatype = np.array([self.bounds[item][0] for item in self.bounds])
        else:
            self.grid_flag=False
            self.bounds = bounds
        
        self.lb = np.array([self.bounds[item][1] for item in self.bounds])
        self.ub = np.array([self.bounds[item][2] for item in self.bounds])
Example #5
0
    def __init__(self, method, fit, bounds, mode='max', episode_length=50):

        if method not in ['ppo', 'a2c', 'acer', 'acktr', 'dqn', 'neat']:
            raise ValueError(
                '--error: unknown RL method is provided, choose from: ppo, a2c, acer, acktr, dqn, neat'
            )
        self.episode_length = episode_length
        self.var_type = np.array([bounds[item][0] for item in bounds])
        self.nx = len(bounds)
        self.method = method

        #mir-grid
        if "grid" in self.var_type:
            self.grid_flag = True
            self.orig_bounds = bounds  #keep original bounds for decoding
            print('--debug: grid parameter type is found in the space')
            self.bounds, self.bounds_map = encode_grid_to_discrete(
                bounds)  #encoding grid to int
            #define var_types again by converting grid to int
            self.var_type = np.array(
                [self.bounds[item][0] for item in self.bounds])
        else:
            self.grid_flag = False
            self.bounds = bounds

        self.lb = np.array([self.bounds[item][1] for item in self.bounds])
        self.ub = np.array([self.bounds[item][2] for item in self.bounds])

        #for PPO/A2C/ACKTR gaussian policy, keep action range normalized, then map it back
        #Recommended---> -1/1 or -2/2
        self.lb_norm = -1
        self.ub_norm = 1
        if all([item == 'int' for item in self.var_type]):
            if method in ['ppo', 'a2c', 'acktr', 'neat']:
                self.action_space = MultiDiscrete([100, 100, 100, 100, 100])
                self.observation_space = Box(low=self.lb,
                                             high=self.ub,
                                             dtype=int)
                self.map_flag = False
            elif method in ['acer', 'dqn']:
                self.discrete_map = convert_multidiscrete_discrete(self.bounds)
                self.action_space = Discrete(len(self.discrete_map))
                self.observation_space = Box(low=self.lb,
                                             high=self.ub,
                                             dtype=int)
                self.map_flag = False
        else:
            if method in ['ppo', 'a2c', 'acktr', 'neat']:
                self.action_space = Box(low=self.lb_norm,
                                        high=self.ub_norm,
                                        shape=(self.nx, ))
                self.observation_space = Box(low=self.lb, high=self.ub)
                self.map_flag = True
            elif method in ['acer', 'dqn']:
                self.map_flag = False
                raise Exception(
                    '--error: the method {} does not support continious spaces, please use ppo, a2c, or acktr'
                    .format(method))

        #--mir
        self.mode = mode
        if mode == 'max':
            self.fit = fit
        elif mode == 'min':
            self.fit = globalize(lambda x: fit(
                x))  #use the function globalize to serialize the nested fit
        else:
            raise ValueError(
                '--error: The mode entered by user is invalid, use either `min` or `max`'
            )

        self.reset()
        self.done = False
        self.counter = 0
        self.index = 0