Hi,

I would like to implement an experiment in PsychoPy, however, I am not sure how to start my specific approach (which is based on a sorting algorithm). I need to ask participants to sort a number of word stimuli based on their preference. They should be presented with two words and choose which they like more. So far so good, getting this done would be easy.

Now, the choice the person gets presented next, is dependend on the previos answers. The sorting algorithm should use the response and determine the next pair to present. For example:

Words: A, B, C, D

- Trial: A vs. B; Person chooses A
- Trial: B vs. C; Person chooses B (transitivity gives us that for A vs. C, the person should choose A)
- Trial: C vs. D; Person chooses C (transitivity gives us all other choices)

The second and third trial are choosen by the algorithm, as the best next trials.

I already implemented the algorithm as functions in Python, I need to include the algorithm into PsychoPy. The order would be something like:

- present first choice
- read the result of the choice and feed it to the algorithm
- the algorithm determines the next choice
- present the next choice determined by the algorithm

and so on.

Ideally, the screen presenting the trial, and reading the answer should be done from within the sorting algortihm functions. The sorting algorithm also uses recursive elements.

Is any of that possible with coding in PsychoPy? If I can do anything to clearify my question please let me know.

Do you mean something like

```
words = ['A','B','C','D']
shuffle(words)
Trial: words[0] vs. words[1]; Answer words[0]
Trial: words[1] vs. words[2]; Answer words[1]
Trial: words[2] vs. words[3]; Answer words[2]
```

or do you mean

```
words = ['A','B','C','D']
shuffle(words)
Trial: words[0] vs. words[1]; Participant chooses x (0 or 1)
Trial: words[x] vs. words[2]; Participant chooses y (x or 2)
Trial: words[y] vs. words[3]
```

In essence yes, but the algorithm is more complex. It is seldom the case that actually the last chosen item will be presented again. The algorithm is similar to MergeSort. I try to make a short example of the algorithm:

```
words = ['A','C','D','B']
```

- open first recursive step: seperate array

```
words1.1 = ['A','C'] and words1.2 = ['D','B']
```

- open second recursive step: seperate arrays again

```
words2.1 = ['A'] and words2.2 = ['C'] and words2.3 = ['D'] and words2.4 = ['B']
```

- Compare two stimuli
- words2.1 = [‘A’] and words2.2 = [‘C’]: Person chooses A
- words2.3 = [‘D’] and words2.4 = [‘B’]: Person chooses B

- Merge the arrays from each choice and close second recursive step

```
words1.1 = ['A','C'] and words1.2 = ['B','D']
```

- Compare words from each array
- [‘A’] and [‘B’]: Person chooses A
- [‘C’] and [‘B’]: Person chooses B
- [‘C’] and [‘D’]: Person chooses C

- Merge the arrays from each choice and close first recursive step

```
words = ['A','B','C','D']
```

Therefore, the function is recursive and in essence

```
def MS(array):
# split arrays and sort each subarray recursivly by calling function MS
left = left_array
right = right_array
left_sorted = MS(left)
right_sorted = MS(right)
sorted_array = merge(left_sorted,right_sorted)
return sorted_array
```

Ideally, at each point choice is needed in the algorithm above, a screen with that choice is presented to the person. However, the number of repitions is not deterministic and also depends on the choices of the person.

I thought about your answer a little more. My problem is, that the number of trials is not determined and varies from person to person. Also the stimuli to be compared next are purely determined by the algorithm (no pattern like in your answer). I need to combine PsychoPy with the algorithm and present the screen and read the answers in appropiate places. I wonder how I can build such a variable routine.