11-16-2022, 11:48
|
#2 (permalink)
|
No Life Poster
Join Date: Sep 2004 Location: London Age: 43
Posts: 520
Member: 83090
Status: Offline
Sonork: 1626857 Thanks Meter: 52 | Really? Nobody knows? I guess this is fortunate for me. Quote:
import numpy as np
import time
n = int(input("How many treatments are in the set (n)? "))
k = int(input("How many treatmewnts are included in each block (k)? "))
_lambda = int(input("How many observations of each pair do you want (lambda)? "))
m = int(input("How many designs do you want the algorithm to generate before picking the best one (m)? "))
t0 = time.time()
with open('ExperimentDesign.csv','w') as outFile:
line = 'Block,'
for stimulus in range(k):
line = line + 'Treat{},'.format(stimulus+1)
line = line[:-1] + '\n'
outFile.write(line)
for i in range(m):
comparisons = np.tril(np.ones((n,n),dtype=int),-1) + np.triu(np.ones((n,n),dtype=int),1)
comparisons = _lambda * comparisons
current_design = np.zeros((1,k),dtype=int)
while comparisons.sum() > 0:
current_block = np.zeros(k,dtype=int)
for stim in range(k):
degree = np.sum(comparisons,axis=0)
links = np.zeros(n, dtype=int)
for s in range(n):
for sl in range(stim):
links[s] += comparisons[s,current_block[sl]]
links[s] += comparisons[current_block[sl],s]
for s in range(stim):
links[current_block[s]] = -1
idx = np.argwhere(links == np.amax(links))
if len(idx) == 1:
current_block[stim] = idx;
else:
idx2 = np.argwhere(degree == np.amax(degree[idx]))
idx3 = np.intersect1d(idx,idx2)
current_block[stim] = idx3[np.random.randint(len(idx3))]
for s in range(stim):
comparisons[current_block[stim],current_block[s]] = np.maximum(comparisons[current_block[stim],current_block[s]]-1,0)
comparisons[current_block[s],current_block[stim]] = np.maximum(comparisons[current_block[s],current_block[stim]]-1,0)
current_design = np.append(current_design, current_block.reshape((1,k)), axis=0)
current_design = current_design[1:]
if i == 0:
best_design = current_design
else:
if current_design.shape[0] < best_design.shape[0]:
best_design = current_design
for trial in range(best_design.shape[0]):
line = '{},'.format(trial+1)
for stimulus in range(k):
line = line + '{},'.format(best_design[trial,stimulus]+1)
line = line[:-1] + '\n'
outFile.write(line)
print('Saved as ExperimentDesign.csv.')
print('Executed in {:.2f} seconds'.format(time.time() - t0))
| |
| |