def perform_priority_moves(state, interactive):
to_move = [pos for cell, pos in state.board.traverse() if cell.content and cell.content[TYPE] == ROBOT
and cell.content['move'] in PRIORITY_MOVES]
random.shuffle(to_move)
while len(to_move) > 0:
pos = to_move.pop()
robot = state.board.get_item(pos).content
if robot['move'] == FORWARD_TWO:
direction = robot[FACING]
elif robot['move'] == SIDESTEP_LEFT:
direction = turn_direction(robot[FACING], False)
elif robot['move'] == SIDESTEP_RIGHT:
direction = turn_direction(robot[FACING], True)
pos = perform_move_in_direction(state, pos, direction, to_move, interactive)
if pos != None and robot['move'] == FORWARD_TWO:
pos = perform_move_in_direction(state, pos, direction, to_move, interactive)
if pos != None:
if robot[CHARGES] > 0:
robot[CHARGES] -= 1
else:
robot[LIFE] -= 1
if robot[LIFE] == 0:
record_death(robot, 'malfunction', interactive)
python类shuffle()的实例源码
def perform_moves(state, interactive):
to_move = [cell.content for cell, pos in state.board.traverse() if cell.content and cell.content[TYPE] == ROBOT
and cell.content['move'] in [TURN_LEFT, TURN_RIGHT, U_TURN]]
for robot in to_move:
perform_turn(robot, robot['move'])
to_move = [pos for cell, pos in state.board.traverse() if cell.content and cell.content[TYPE] == ROBOT
and cell.content['move'] in [FORWARD, REVERSE]]
random.shuffle(to_move)
while len(to_move) > 0:
pos = to_move.pop()
robot = state.board.get_item(pos).content
if robot['move'] == FORWARD:
direction = robot[FACING]
elif robot['move'] == REVERSE:
direction = opposite_direction(robot[FACING])
perform_move_in_direction(state, pos, direction, to_move, interactive)
def process_train_data(self, input_file, onto_aware):
print >>sys.stderr, "Reading training data"
label_ind = []
tagged_sentences = []
for line in open(input_file):
lnstrp = line.strip()
label, tagged_sentence = lnstrp.split("\t")
if label not in self.label_map:
self.label_map[label] = len(self.label_map)
label_ind.append(self.label_map[label])
tagged_sentences.append(tagged_sentence)
# Shuffling so that when Keras does validation split, it is not always at the end.
sentences_and_labels = zip(tagged_sentences, label_ind)
random.shuffle(sentences_and_labels)
tagged_sentences, label_ind = zip(*sentences_and_labels)
print >>sys.stderr, "Indexing training data"
train_inputs = self.data_processor.prepare_paired_input(tagged_sentences, onto_aware=onto_aware,
for_test=False, remove_singletons=True)
train_labels = self.data_processor.make_one_hot(label_ind)
return train_inputs, train_labels
def split_dataset(dataset, split_ratio, mode):
if mode=='SPLIT_CLASSES':
nrof_classes = len(dataset)
class_indices = np.arange(nrof_classes)
np.random.shuffle(class_indices)
split = int(round(nrof_classes*split_ratio))
train_set = [dataset[i] for i in class_indices[0:split]]
test_set = [dataset[i] for i in class_indices[split:-1]]
elif mode=='SPLIT_IMAGES':
train_set = []
test_set = []
min_nrof_images = 2
for cls in dataset:
paths = cls.image_paths
np.random.shuffle(paths)
split = int(round(len(paths)*split_ratio))
if split<min_nrof_images:
continue # Not enough images for test set. Skip class...
train_set.append(ImageClass(cls.name, paths[0:split]))
test_set.append(ImageClass(cls.name, paths[split:-1]))
else:
raise ValueError('Invalid train/test split mode "%s"' % mode)
return train_set, test_set
def get_neighbors(words, word, window_size=2):
if type(word) == str:
idx = words.index(word)
elif type(word) == int:
idx = word
else:
raise Exception(" [!] Invalid type for word: %s" % type(word))
if idx < window_size:
ans = words[-(window_size - idx):] + words[:idx + window_size + 1]
elif idx >= len(words) - window_size:
ans = words[idx-window_size:] + words[:window_size + idx - len(words) + 1]
else:
ans = words[idx-window_size:idx+window_size+1]
for _ in xrange(15):
if random.random() < 0.1:
ans.append(random.choice(ans))
random.shuffle(ans)
return ans
def split(self, *splits):
"""docstring for Batcher"""
data_length = len(self.x)
indexes = range(data_length)
random.shuffle(indexes)
splits = [0] + list(splits)
splits_total = sum(splits)
return (
query(splits)
.scan()
.select(lambda n: int(data_length * n / splits_total))
.then(_window, n=2)
.select(lambda (start, end): np.array(indexes[start:end]))
.select(lambda split: Data(**{k: source[split,:] for (k, source) in self.sources.iteritems()}))
.to_list()
)
def batch_iter(data, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a dataset.
"""
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int(len(data)/batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
def train(self, sentences):
random.shuffle(sentences)
for s, a_sentence in enumerate(sentences):
words_and_tags = a_sentence.split('\n')
words = [wt.split(' ')[0] for wt in words_and_tags]
tags = [wt.split(' ')[1] for wt in words_and_tags]
# ?????0 1 2 3 4 5 6 7 8 9
for i in range(len(tags)):
if tags[i][0] == 'I':
if i == len(tags)-1 or tags[i+1] != tags[i]:
tags[i] = 'E-' + tags[i][-3:]
tags[i] = int(self.tags_dict[tags[i]])
self.model.train(words, tags)
if s % 5000 == 0:
print(' -----> ' + str(s // 5000) + '/5')
def make_list(args):
image_list = list_image(args.root, args.recursive, args.exts)
image_list = list(image_list)
if args.shuffle is True:
random.seed(100)
random.shuffle(image_list)
N = len(image_list)
chunk_size = (N + args.chunks - 1) / args.chunks
for i in xrange(args.chunks):
chunk = image_list[i * chunk_size:(i + 1) * chunk_size]
if args.chunks > 1:
str_chunk = '_%d' % i
else:
str_chunk = ''
sep = int(chunk_size * args.train_ratio)
sep_test = int(chunk_size * args.test_ratio)
if args.train_ratio == 1.0:
write_list(args.prefix + str_chunk + '.lst', chunk)
else:
if args.test_ratio:
write_list(args.prefix + str_chunk + '_test.lst', chunk[:sep_test])
if args.train_ratio + args.test_ratio < 1.0:
write_list(args.prefix + str_chunk + '_val.lst', chunk[sep_test + sep:])
write_list(args.prefix + str_chunk + '_train.lst', chunk[sep_test:sep_test + sep])
def _compute_max_qval_action_pair(self, state):
'''
Args:
state (State)
Returns:
(tuple) --> (float, str): where the float is the Qval, str is the action.
'''
# Grab random initial action in case all equal
best_action = random.choice(self.actions)
max_q_val = float("-inf")
shuffled_action_list = self.actions[:]
random.shuffle(shuffled_action_list)
# Find best action (action w/ current max predicted Q value)
for action in shuffled_action_list:
q_s_a = self.get_q_value(state, action)
if q_s_a > max_q_val:
max_q_val = q_s_a
best_action = action
return max_q_val, best_action
def _compute_max_qval_action_pair(self, state, q_func_id=None):
'''
Args:
state (State)
q_func_id (str): either "A", "B", or None. If None, computes avg of A and B.
Returns:
(tuple) --> (float, str): where the float is the Qval, str is the action.
'''
# Grab random initial action in case all equal
best_action = random.choice(self.actions)
max_q_val = float("-inf")
shuffled_action_list = self.actions[:]
random.shuffle(shuffled_action_list)
# Find best action (action w/ current max predicted Q value)
for action in shuffled_action_list:
q_s_a = self.get_q_value(state, action, q_func_id)
if q_s_a > max_q_val:
max_q_val = q_s_a
best_action = action
return max_q_val, best_action
def _batch_samples(self, sample):
"""Batch several samples together."""
# Batch and shuffle
if self.shuffle:
samples = tf.train.shuffle_batch(
sample,
batch_size=self.batch_size,
num_threads=self.nthreads,
capacity=self.capacity,
min_after_dequeue=self.min_after_dequeue)
else:
samples = tf.train.batch(
sample,
batch_size=self.batch_size,
num_threads=self.nthreads,
capacity=self.capacity)
return samples
def handle_other_peers(self, swarm, data):
"""Handle other_peers message when not using ALTO"""
# Shuffle the received members list
mem_copy = data['details']
random.shuffle(mem_copy)
for member in mem_copy:
if swarm._args.tcp:
self.add_tcp_member(swarm, member[0], member[1])
else:
# This is UDP
m = swarm.AddMember(member[0], member[1])
if isinstance(m, str):
pass
else:
m.SendHandshake()
def split_keys(profiles, bin_sites, random_state=1234):
"""Balanced split over binding/non-binding sequences."""
random.seed(random_state)
pos_keys = bin_sites.keys()
neg_keys = list(set(profiles.keys()) - set(pos_keys))
random.shuffle(pos_keys)
random.shuffle(neg_keys)
len_pos = len(pos_keys)
pos_keys1 = pos_keys[:len_pos / 2]
pos_keys2 = pos_keys[len_pos / 2:]
len_neg = len(neg_keys)
neg_keys1 = neg_keys[:len_neg / 2]
neg_keys2 = neg_keys[len_neg / 2:]
return [pos_keys1, pos_keys2, neg_keys1, neg_keys2]
def runmoore(x=78, y=18, m=225):
field = anonymine_fields.generic_field([x, y])
field.set_callback('input', output, None)
print(field)
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
print(solver.solve())
def runneumann(x=78, y=18, m=225):
field = anonymine_fields.generic_field([x, y], False)
field.set_callback('input', output, None)
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
print(solver.solve())
def runhex(x=39, y=18, m=112):
field = anonymine_fields.hexagonal_field(x, y)
field.set_callback('input', output, None)
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
print(solver.solve())
def gen_values(self, n, reversed = False, shuffled = False, gen_dupes = False):
if reversed:
keys = xrange(n-1,-1,-1)
else:
keys = xrange(n)
if shuffled:
keys = list(keys)
r = random.Random(1234827)
r.shuffle(keys)
if gen_dupes:
return itertools.chain(
itertools.izip(keys, xrange(0, 2*n, 2)),
itertools.islice(itertools.izip(keys, xrange(0, 2*n, 2)), 10, None),
)
else:
return itertools.izip(keys, xrange(0, 2*n, 2))
def testBsearch(self, dtype=dtype):
testarray = range(1,101)
random.shuffle(testarray)
a = numpy.array(testarray[:50], dtype)
b = numpy.array([0] + testarray[50:] + range(101,103), dtype)
a = numpy.sort(a)
self.assertEqual(mapped_struct.bsearch(a, 0), 0)
self.assertEqual(mapped_struct.bsearch(a, 101), len(a))
self.assertEqual(mapped_struct.bsearch(a, 102), len(a))
for x in a:
ix = mapped_struct.bsearch(a, x)
self.assertLess(ix, len(a))
self.assertEqual(a[ix], x)
self.assertTrue(mapped_struct.sorted_contains(a, x))
for x in b:
ix = mapped_struct.bsearch(a, x)
self.assertTrue(ix >= len(a) or a[ix] != x)
self.assertFalse(mapped_struct.sorted_contains(a, x))
def test_send_receive(self):
random.shuffle(self.swarm)
senders = self.swarm[:len(self.swarm)/2]
receivers = self.swarm[len(self.swarm)/2:]
for sender, receiver in zip(senders, receivers):
message = binascii.hexlify(os.urandom(64))
# check queue previously empty
self.assertFalse(bool(receiver.message_list()))
# send message
self.assertTrue(sender.message_send(receiver.dht_id(), message))
# check received
received = receiver.message_list()
self.assertTrue(sender.dht_id() in received)
messages = received[sender.dht_id()]
self.assertTrue(len(messages) == 1)
self.assertEqual(messages[0], message)
# check queue empty after call to message_list
self.assertFalse(bool(receiver.message_list()))
def test_ordering(self):
random.shuffle(self.swarm)
sender = self.swarm[0]
receiver = self.swarm[-1]
# send messages
message_alpha = binascii.hexlify(os.urandom(64))
message_beta = binascii.hexlify(os.urandom(64))
message_gamma = binascii.hexlify(os.urandom(64))
self.assertTrue(sender.message_send(receiver.dht_id(), message_alpha))
self.assertTrue(sender.message_send(receiver.dht_id(), message_beta))
self.assertTrue(sender.message_send(receiver.dht_id(), message_gamma))
# check received in order
received = receiver.message_list()
self.assertTrue(sender.dht_id() in received)
messages = received[sender.dht_id()]
self.assertEqual(messages[0], message_alpha)
self.assertEqual(messages[1], message_beta)
self.assertEqual(messages[2], message_gamma)
def test_json(self):
random.shuffle(self.swarm)
sender = self.swarm[0]
receiver = self.swarm[-1]
message = {
"test_object": {"foo": "bar"},
"test_array": [0, 1, 2, 3, 4, 5],
"test_integer": 42,
"test_float": 3.14,
"test_bool": True,
"test_null": None,
}
# send message
self.assertTrue(sender.message_send(receiver.dht_id(), message))
# check received
received = receiver.message_list()
self.assertTrue(sender.dht_id() in received)
messages = received[sender.dht_id()]
self.assertTrue(len(messages) == 1)
self.assertEqual(messages[0], message)
def test_multihop(self):
random.shuffle(self.swarm)
senders = self.swarm[:len(self.swarm) / 2]
receivers = self.swarm[len(self.swarm) / 2:]
for sender, receiver in zip(senders, receivers):
# receiver subscribes to topic
topic = "test_miltihop_{0}".format(binascii.hexlify(os.urandom(32)))
receiver.pubsub_subscribe(topic)
# wait until subscriptions propagate
time.sleep(SLEEP_TIME)
# send event
event = binascii.hexlify(os.urandom(32))
sender.pubsub_publish(topic, event)
# wait until event propagates
time.sleep(SLEEP_TIME)
# check all peers received the event
events = receiver.pubsub_events(topic)
self.assertEqual(events, [event])
def get_batches(data, batch_size, vocabulary, pos_vocabulary):
'''
Get batches without any restrictions on number of antecedents and negative candidates.
'''
random.seed(24)
random.shuffle(data)
data_size = len(data)
if data_size % float(batch_size) == 0:
num_batches = int(data_size / float(batch_size))
else:
num_batches = int(data_size / float(batch_size)) + 1
batches = []
for batch_num in range(num_batches):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
batch = pad_batch(data[start_index:end_index], vocabulary, pos_vocabulary)
batches.append(batch)
logging.info('Data size: %s' % len(data))
logging.info('Number of batches: %s' % len(batches))
return batches
def split(flags):
if os.path.exists(flags.split_path):
return np.load(flags.split_path).item()
folds = flags.folds
path = flags.input_path
random.seed(6)
img_list = ["%s/%s"%(path,img) for img in os.listdir(path)]
random.shuffle(img_list)
dic = {}
n = len(img_list)
num = (n+folds-1)//folds
for i in range(folds):
s,e = i*num,min(i*num+num,n)
dic[i] = img_list[s:e]
np.save(flags.split_path,dic)
return dic
def __init__(self, reader, partition, discretizer, normalizer,
batch_size, steps, shuffle):
self.reader = reader
self.partition = partition
self.discretizer = discretizer
self.normalizer = normalizer
self.batch_size = batch_size
if steps is None:
self.n_examples = reader.get_number_of_examples()
self.steps = (self.n_examples + batch_size - 1) // batch_size
else:
self.n_examples = steps * batch_size
self.steps = steps
self.shuffle = shuffle
self.chunk_size = min(1024, steps) * batch_size
self.lock = threading.Lock()
self.generator = self._generator()
def __init__(self, reader, discretizer, normalizer,
batch_size, steps, shuffle):
self.reader = reader
self.discretizer = discretizer
self.normalizer = normalizer
self.batch_size = batch_size
if steps is None:
self.n_examples = reader.get_number_of_examples()
self.steps = (self.n_examples + batch_size - 1) // batch_size
else:
self.n_examples = steps * batch_size
self.steps = steps
self.shuffle = shuffle
self.chunk_size = min(1024, steps) * batch_size
self.lock = threading.Lock()
self.generator = self._generator()
def _generator(self):
B = self.batch_size
while True:
if self.shuffle:
self.reader.random_shuffle()
remaining = self.n_examples
while remaining > 0:
current_size = min(self.chunk_size, remaining)
remaining -= current_size
(data, ts, labels, header) = read_chunk(self.reader, current_size)
data = preprocess_chunk(data, ts, self.discretizer, self.normalizer)
data = (data, labels)
data = common_utils.sort_and_shuffle(data, B)
for i in range(0, current_size, B):
yield (nn_utils.pad_zeros(data[0][i:i + B]),
np.array(data[1][i:i + B]))
def justify_line(line, width):
"""Stretch a line to width by filling in spaces at word gaps.
The gaps are picked randomly one-after-another, before it starts
over again.
"""
i = []
while 1:
# line not long enough already?
if len(' '.join(line)) < width:
if not i:
# index list is exhausted
# get list if indices excluding last word
i = range(max(1, len(line)-1))
# and shuffle it
random.shuffle(i)
# append space to a random word and remove its index
line[i.pop(0)] += ' '
else:
# line has reached specified width or wider
return ' '.join(line)
def botPick(self, ctx, bot, game):
# Has the bot pick their card
blackNum = game['BlackCard']['Pick']
if blackNum == 1:
cardSpeak = 'card'
else:
cardSpeak = 'cards'
i = 0
cards = []
while i < blackNum:
randCard = random.randint(0, len(bot['Hand'])-1)
cards.append(bot['Hand'].pop(randCard)['Text'])
i += 1
await self.typing(game)
# Make sure we haven't laid any cards
if bot['Laid'] == False and game['Judging'] == False:
newSubmission = { 'By': bot, 'Cards': cards }
game['Submitted'].append(newSubmission)
# Shuffle cards
shuffle(game['Submitted'])
bot['Laid'] = True
game['Time'] = currentTime = int(time.time())
await self.checkSubmissions(ctx, game, bot)