def EndRandom(self):
# This function is responsible for returning the RNG to the same state
# as when it entered the AI
random.setstate(self.RNG_State)
python类setstate()的实例源码
def createAiPlayerName(self, female, seed):
state = random.getstate()
random.seed(seed)
if female:
first_name_array = PLocalizer.PirateNames_FirstNamesFemale
else:
first_name_array = PLocalizer.PirateNames_FirstNamesMale
last_name_prefix_array = PLocalizer.PirateNames_LastNamePrefixesGeneric
last_name_suffix_array = PLocalizer.PirateNames_LastNameSuffixesGeneric
string = ''
string = string + self.randomArraySelection(first_name_array) + ' '
string = string + self.randomArraySelection(last_name_prefix_array)
string = string + self.randomArraySelection(last_name_suffix_array)
random.setstate(state)
return string
def test_fuzz(i):
orig_state = random.getstate()
random.seed(123456789 + i) # Some arbitrary, but deterministic number.
exch = make_exchange()
random.setstate(orig_state)
check_exchange(exch)
text_report([exch], six.BytesIO())
html_report([exch], six.BytesIO())
def reinit_random_state():
global initial_random_state
random.setstate(initial_random_state)
def make_random_strat():
"""Makes a random pure strategy."""
seed = random.randrange(0, 2 ** 31)
def random_strat(score, opponent_score):
# Save the state of the random generator, so strategy calls don't
# impact dice rolls.
state = random.getstate()
random.seed(hash((score, opponent_score, seed)))
roll = random.randrange(0, 11)
random.setstate(state)
return roll
return random_strat
def downsample(self, fraction_to_retain, random_seed, verbose=False):
random_state = random.getstate()
random.seed(random_seed)
try:
number_to_retain = \
max(int(round(float(len(self._scenarios)*fraction_to_retain))), 1)
random_list=random.sample(range(len(self._scenarios)), number_to_retain)
scenario_bundle_list = []
for i in xrange(number_to_retain):
scenario_bundle_list.append(self._scenarios[random_list[i]]._name)
if verbose:
print("Downsampling scenario tree - retained %s "
"scenarios: %s"
% (len(scenario_bundle_list),
str(scenario_bundle_list)))
self.compress(scenario_bundle_list) # do the downsampling
finally:
random.setstate(random_state)
#
# returns the root node of the scenario tree
#
def simplified_data(num_train, num_dev, num_test):
rndstate = random.getstate()
random.seed(0)
trees = loadTrees('train') + loadTrees('dev') + loadTrees('test')
#filter extreme trees
pos_trees = [t for t in trees if t.root.label==4]
neg_trees = [t for t in trees if t.root.label==0]
#binarize labels
binarize_labels(pos_trees)
binarize_labels(neg_trees)
#split into train, dev, test
print len(pos_trees), len(neg_trees)
pos_trees = sorted(pos_trees, key=lambda t: len(t.get_words()))
neg_trees = sorted(neg_trees, key=lambda t: len(t.get_words()))
num_train/=2
num_dev/=2
num_test/=2
train = pos_trees[:num_train] + neg_trees[:num_train]
dev = pos_trees[num_train : num_train+num_dev] + neg_trees[num_train : num_train+num_dev]
test = pos_trees[num_train+num_dev : num_train+num_dev+num_test] + neg_trees[num_train+num_dev : num_train+num_dev+num_test]
random.shuffle(train)
random.shuffle(dev)
random.shuffle(test)
random.setstate(rndstate)
return train, dev, test
def penis(self, *, user : discord.Member):
"""Detects user's penis length
This is 100% accurate."""
state = random.getstate()
random.seed(user.id)
dong = "8{}D".format("=" * random.randint(0, 30))
random.setstate(state)
await self.bot.say("Size: " + dong)
def setup_data(self, path):
"""Read and iteratively yield data to an agent."""
print('loading: ' + path)
questions = []
y = []
# open data file with labels
# (path will be provided to setup_data from opt['datafile'] defined above)
with open(path) as labels_file:
tsv_reader = csv.reader(labels_file, delimiter='\t')
for row in tsv_reader:
if len(row) != 3:
print('Warn: expected 3 columns in a tsv row, got ' + str(row))
continue
y.append(['??' if row[0] == '1' else '???'])
questions.append(row[1] + '\n' + row[2])
episode_done = True
if not y:
y = [None for _ in range(len(questions))]
indexes = range(len(questions))
if self.datatype_strict != 'test':
random_state = random.getstate()
random.setstate(self.random_state)
kf_seed = random.randrange(500000)
kf = KFold(self.opt.get('bagging_folds_number'), shuffle=True,
random_state=kf_seed)
i = 0
for train_index, test_index in kf.split(questions):
indexes = train_index if self.datatype_strict == 'train' else test_index
if i >= self.opt.get('bagging_fold_index', 0):
break
self.random_state = random.getstate()
random.setstate(random_state)
# define iterator over all queries
for i in indexes:
# get current label, both as a digit and as a text
# yield tuple with information and episode_done? flag
yield (self.question + "\n" + questions[i], y[i]), episode_done
def evaluate(self):
act = self.actionName
if self.syncState:
possible = False
for sInp in self.inputs:
if self.neurons[sInp].isCurrent:
possible = True
break
if not possible or len(self.valueInputs) == 0:
self.finalValue = 0
self.finalValueCalcd = True
return
sm = self.brain.sim.syncManager
userid = self.brain.userid
for inp in self.valueInputs:
vals = self.neurons[inp].evaluate()
for key, v in vals.items():
if self.settings["RandomInput"]:
val = v + (self.settings["ValueDefault"] *
v * random.random())
else:
val = v + (v * self.settings["ValueDefault"])
if val > 0:
if self.isGroup():
acNm = self.actionName
for act in self.brain.sim.actionGroups[acNm[1:-1]]:
sm.tell(userid, key, act, val, self.name)
else:
sm.tell(userid, key, self.actionName,
val, self.name)
(state, action), pairedAgent = sm.getResult(userid)
if state == self.name:
self.finalValue = 1
self.action = action
else:
self.finalValue = 0
self.finalValueCalcd = True
elif self.isGroup():
State.evaluate(self)
acNm = self.actionName
state = random.getstate()
if not self.randomActionFromGroup:
random.seed(hash(self.brain.userid))
self.action = random.choice(
self.brain.sim.actionGroups[acNm[1:-1]])
random.setstate(state)
else:
State.evaluate(self)
self.action = self.actionName
def set_state(state):
"""
Given a dictionary representing the state of an evolutionary run, set all
aspects of the system to re-create that state. The state includes the
current population, the current random state, the parameters dictionary,
the stats dictionary, and all lists in the utilities.stats.trackers module.
Sets all aspects of the system and then returns a population of
individuals at the current generation.
:param state: The complete state of a run.
:return: A population of individuals.
"""
from algorithm.parameters import params
from utilities.algorithm.initialise_run import set_param_imports
from stats.stats import stats
from utilities.stats import trackers
from time import time
# Set random state.
random.setstate(state['random_state'])
# Set stats.
for stat in state['stats']:
stats[stat] = state['stats'][stat]
# Set trackers.
for tracker in state['trackers']:
setattr(trackers, tracker, state['trackers'][tracker])
# Set parameters.
for param in state['params']:
params[param] = state['params'][param]
# Set correct param imports for specified function options, including
# error metrics and fitness functions.
set_param_imports()
# Set time adjustment to account for old time.
stats['time_adjust'] = time() - state['time']
return state['individuals']
def sgd(f, x0, step, iterations, postprocessing = None, useSaved = False, PRINT_EVERY = 10):
""" Stochastic Gradient Descent """
ANNEAL_EVERY = 20000
if useSaved:
start_iter, oldx, state = load_saved_params()
if start_iter > 0:
x0 = oldx;
step *= 0.5 ** (start_iter / ANNEAL_EVERY)
if state:
random.setstate(state)
else:
start_iter = 0
x = x0
if not postprocessing:
postprocessing = lambda x: x
expcost = None
for iter in xrange(start_iter + 1, iterations + 1):
cost = None
cost, grad = f(x)
x -= step * grad
x = postprocessing(x)
if iter % PRINT_EVERY == 0:
if not expcost:
expcost = cost
else:
expcost = .95 * expcost + .05 * cost
print "iter %d: %f" % (iter, expcost)
if iter % SAVE_PARAMS_EVERY == 0 and useSaved:
save_params(iter, x)
if iter % ANNEAL_EVERY == 0:
step *= 0.5
return x
def gradcheck_naive(f, x):
""" Gradient check for a function f.
Arguments:
f -- a function that takes a single argument and outputs the
cost and its gradients
x -- the point (numpy array) to check the gradient at
"""
rndstate = random.getstate()
random.setstate(rndstate)
fx, grad = f(x) # Evaluate function value at original point
h = 1e-4 # Do not change this!
# Iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
ix = it.multi_index
# Try modifying x[ix] with h defined above to compute
# numerical gradients. Make sure you call random.setstate(rndstate)
# before calling f(x) each time. This will make it possible
# to test cost functions with built in randomness later.
### YOUR CODE HERE:
old_value = x[ix]
random.setstate(rndstate)
x[ix] = old_value + h
fxh_left, _ = f(x)
random.setstate(rndstate)
x[ix] = old_value - h
fxh_right, _ = f(x)
numgrad = (fxh_left - fxh_right) / (2 * h)
x[ix] = old_value
### END YOUR CODE
# Compare gradients
reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))
if reldiff > 1e-5:
print("Gradient check failed.")
print("First gradient error found at index %s" % str(ix))
print("Your gradient: %f \t Numerical gradient: %f" % (
grad[ix], numgrad))
return
it.iternext() # Step to next dimension
print("Gradient check passed!")
def gradcheck_naive(f, x):
""" Gradient check for a function f.
Arguments:
f -- a function that takes a single argument and outputs the
cost and its gradients
x -- the point (numpy array) to check the gradient at
"""
rndstate = random.getstate()
random.setstate(rndstate)
fx, grad = f(x) # Evaluate function value at original point
h = 1e-4 # Do not change this!
# Iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
ix = it.multi_index
# Try modifying x[ix] with h defined above to compute
# numerical gradients. Make sure you call random.setstate(rndstate)
# before calling f(x) each time. This will make it possible
# to test cost functions with built in randomness later.
### YOUR CODE HERE:
old_xix = x[ix]
x[ix] = old_xix + h
random.setstate(rndstate)
fp = f(x)[0]
x[ix] = old_xix - h
random.setstate(rndstate)
fo = f(x)[0]
numgrad = (fp - fo)/ (2*h)
x[ix] = old_xix
### END YOUR CODE
# Compare gradients
reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))
if reldiff > 1e-5:
print ("Gradient check failed.")
print ("First gradient error found at index %s" % str(ix))
print ("Your gradient: %f \t Numerical gradient: %f" % (
grad[ix], numgrad))
return
it.iternext() # Step to next dimension
print("Gradient check passed!")
def create_random_bundles(self,
scenario_tree_instance,
num_bundles,
random_seed):
random_state = random.getstate()
random.seed(random_seed)
try:
num_scenarios = len(self._scenarios)
sequence = list(range(num_scenarios))
random.shuffle(sequence)
scenario_tree_instance.Bundling[None] = True
next_scenario_index = 0
# this is a hack-ish way to re-initialize the Bundles set of a
# scenario tree instance, which should already be there
# (because it is defined in the abstract model). however, we
# don't have a "clear" method on a set, so...
scenario_tree_instance.del_component("Bundles")
scenario_tree_instance.add_component("Bundles", Set(ordered=True))
for i in xrange(1, num_bundles+1):
bundle_name = "Bundle"+str(i)
scenario_tree_instance.Bundles.add(bundle_name)
# ditto above comment regarding del_component/add_component
scenario_tree_instance.del_component("BundleScenarios")
scenario_tree_instance.add_component("BundleScenarios",
Set(scenario_tree_instance.Bundles,
ordered=True))
bundles = []
for i in xrange(num_bundles):
bundle_name = "Bundle"+str(i+1)
tmp = Set(ordered=True)
tmp.construct()
scenario_tree_instance.BundleScenarios[bundle_name] = tmp
bundles.append(scenario_tree_instance.BundleScenarios[bundle_name])
scenario_index = 0
while (scenario_index < num_scenarios):
for bundle_index in xrange(num_bundles):
if (scenario_index == num_scenarios):
break
bundles[bundle_index].add(
self._scenarios[sequence[scenario_index]]._name)
scenario_index += 1
self._construct_scenario_bundles(scenario_tree_instance)
finally:
random.setstate(random_state)
#
# a utility function to pretty-print the static/non-cost
# information associated with a scenario tree
#
def gradcheck_naive(f, x):
"""
Gradient check for a function f
- f should be a function that takes a single argument and outputs the cost and its gradients
- x is the point (numpy array) to check the gradient at
"""
rndstate = random.getstate()
random.setstate(rndstate)
fx, grad = f(x) # Evaluate function value at original point
h = 1e-4
# Iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
ix = it.multi_index
### try modifying x[ix] with h defined above to compute numerical gradients
### make sure you call random.setstate(rndstate) before calling f(x) each time, this will make it
### possible to test cost functions with built in randomness later
### YOUR CODE HERE:
x_ = x.copy()
random.setstate(rndstate)
x_[ix] += h
fx1, _ = f(x_)
random.setstate(rndstate)
x_ = x.copy()
x_[ix] -= h
fx2 , _ = f(x_)
numgrad = (fx1 - fx2) / (2 * h)
### END YOUR CODE
# Compare gradients
reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))
if reldiff > 1e-5:
print "Gradient check failed."
print "First gradient error found at index %s" % str(ix)
print "Your gradient: %f \t Numerical gradient: %f" % (grad[ix], numgrad)
return
it.iternext() # Step to next dimension
print "Gradient check passed!"