def _anneal(self, objfunc, schedule, probfunc): ''' our actual annealing method ''' k_current = 1 # get our initial state dim = len(objfunc.maxs) params = [(objfunc.mins[i], func.maxs[i] - func.mins[i]) for i in xrange(dim)] seeds = np.array(lhs([uniform]*dim, params, 1, True, np.identity(dim))).T[0] state = self._get_state(objfunc, [0.0]*dim, seeds) state = [s + objfunc.mins[i] for i, s in enumerate(state)] best = np.array(state) best_val = objfunc.eval(best) t_current = self.t_initial while t_current > self.tfinal: # run until temperature is low enough k_current += 1 t_current = schedule(k_current) state = self._get_state(objfunc, state, [1.0]*dim) current_val = objfunc.eval(state) if uniform.rvs() < probfunc(t_current, current_val, best_val, dim): best = state best_val = current_val return best
def _test_anneal(self, objfunc, schedule, probfunc, iterations): ''' our actual annealing method ''' bests = [] k_current = 1 # get our initial state dim = len(objfunc.maxs) params = [(objfunc.mins[i], objfunc.maxs[i] - objfunc.mins[i]) for i in xrange(dim)] seeds = np.array(lhs([uniform]*dim, params, 1, True, np.identity(dim))).T[0] state = self._get_state(objfunc, [0.0]*dim, seeds) state = [s + objfunc.mins[i] for i, s in enumerate(state)] best = np.array(state) start_val = best_val = objfunc.eval(best) t_current = self.t_initial bests.append(best_val) for _ in range(iterations): k_current += 1 t_current = schedule(k_current) state = self._get_state(objfunc, state, [1.0]*dim) current_val = objfunc.eval(state) if uniform.rvs() < probfunc(t_current, current_val, best_val, dim): best = state best_val = current_val bests.append(best_val) return np.array(bests)
def _generate_pop(self, size, func): ''' initializes our population ''' dim = len(func.maxs) params = [(func.mins[i], func.maxs[i] - func.mins[i]) for i in xrange(dim)] seeds = np.array(lhs([uniform]*dim, params, size, True, np.identity(dim))).T seeds.astype(np.float32) flies = [FireFly(func, self, np.array(seeds[i])) for i in xrange(size)] return flies
def _generate_pop(self, size, func): ''' initializes our population ''' dim = len(func.maxs) params = [(func.mins[i], func.maxs[i] - func.mins[i]) for i in xrange(dim)] seeds = np.array( lhs([uniform] * dim, params, size, True, np.identity(dim))).T seeds.astype(np.float32) flies = [FireFly(func, self, np.array(seeds[i])) for i in xrange(size)] return flies
def _generate_pop(self, psize, func): ''' initializes our population ''' mins = np.array(func.mins) maxs = np.array(func.maxs) lows = -np.abs(maxs - mins) hihs = np.abs(maxs - mins) # calculate our initial positions dim = len(func.maxs) params = [(mins[i], maxs[i] - mins[i]) for i in xrange(dim)] seeds = np.array(lhs([uniform]*dim, params, psize, True, np.identity(dim))).T seeds.astype(np.float32) # calculate our initial velocities v_seeds = np.array([uniform.rvs(lows[i], hihs[i], size=psize) for i in range(dim)]).T return [Particle(func, np.array(seeds[i]), np.array(v_seeds[i])) for i in xrange(psize)]
def _generate_pop(self, psize, func): ''' initializes our population ''' mins = np.array(func.mins) maxs = np.array(func.maxs) lows = -np.abs(maxs - mins) hihs = np.abs(maxs - mins) # calculate our initial positions dim = len(func.maxs) params = [(mins[i], maxs[i] - mins[i]) for i in xrange(dim)] seeds = np.array( lhs([uniform] * dim, params, psize, True, np.identity(dim))).T seeds.astype(np.float32) # calculate our initial velocities v_seeds = np.array( [uniform.rvs(lows[i], hihs[i], size=psize) for i in range(dim)]).T return [ Particle(func, np.array(seeds[i]), np.array(v_seeds[i])) for i in xrange(psize) ]
def test_correlation_structure(self): cm = numpy.array([[1,.8],[.8,1]]) c=lhs.lhs([st.norm,st.beta], [(50,1),(10,2)],2000,False, cm) r,p = st.spearmanr(c[0],c[1]) assert_almost_equal(r,0.8,1) assert p < 0.05
def test_correlation_structure(self): cm = numpy.array([[1, .8], [.8, 1]]) c = lhs.lhs([st.norm, st.beta], [(50, 1), (10, 2)], 2000, False, cm) r, p = st.spearmanr(c[0], c[1]) assert_almost_equal(r, 0.8, 1) assert p < 0.05