# Present stimuli based on sorting algorithm

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

1. Trial: A vs. B; Person chooses A
2. Trial: B vs. C; Person chooses B (transitivity gives us that for A vs. C, the person should choose A)
3. 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:

1. present first choice
2. read the result of the choice and feed it to the algorithm
3. the algorithm determines the next choice
4. 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 vs. words; Answer words
Trial: words vs. words; Answer words
Trial: words vs. words; Answer words

``````

or do you mean

``````words = ['A','B','C','D']
shuffle(words)

Trial: words vs. words; Participant chooses x (0 or 1)
Trial: words[x] vs. words; Participant chooses y (x or 2)
Trial: words[y] vs. words

``````

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']
``````
1. open first recursive step: seperate array
``````words1.1 = ['A','C'] and words1.2 = ['D','B']
``````
1. open second recursive step: seperate arrays again
``````words2.1 = ['A'] and words2.2 = ['C'] and words2.3 = ['D'] and words2.4 = ['B']
``````
1. Compare two stimuli
1. words2.1 = [‘A’] and words2.2 = [‘C’]: Person chooses A
2. words2.3 = [‘D’] and words2.4 = [‘B’]: Person chooses B
2. Merge the arrays from each choice and close second recursive step
``````words1.1 = ['A','C'] and words1.2 = ['B','D']
``````
1. Compare words from each array
1. [‘A’] and [‘B’]: Person chooses A
2. [‘C’] and [‘B’]: Person chooses B
3. [‘C’] and [‘D’]: Person chooses C
2. 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.