(deftemplate oav
    (multislot o (default nil))
    (multislot a (default nil))
    (multislot v (default nil))
)
(assert (oav (o _) (a curpos) (v 123)))
(assert (oav (o _) (a curpos) (v 123 543 343)))

(domain
    (name brainswap)
    (operator (!swap ?mind-1 ?body-1 ?mind-2 ?body-2)
        ; precondition, We can only swap two people if we haven't already swapped them.
        (
            ;(mindbody (mind ?mind-1) (body ?body-1))
            (oav (o ?1) (a mind) (v ?mind-1))
            (oav (o ?1) (a body) (v ?body-1))
            
            ;(mindbody (mind ?mind-2) (body ?body-2))
            (oav (o ?2) (a mind) (v ?mind-2))
            (oav (o ?2) (a body) (v ?body-2))
            
            (test (neq ?mind-1 ?mind-2))
            (test (neq ?body-1 ?body-2))
            (not (swapped ?mind-1 ?body-1 ?mind-2 ?body-2))
            (not (swapped ?mind-1 ?body-2 ?mind-2 ?body-1))
            (not (swapped ?mind-2 ?body-2 ?mind-1 ?body-1))
            (not (swapped ?mind-2 ?body-1 ?mind-1 ?body-2))
        )
        (
            (branch)
            (del (mindbody (mind ?mind-1) (body ?body-1)))
            (del (mindbody (mind ?mind-2) (body ?body-2)))
            (add (swapped ?mind-1 ?body-1 ?mind-2 ?body-2))
            (add (mindbody (mind ?mind-1) (body ?body-2)))
            (add (mindbody (mind ?mind-2) (body ?body-1)))
        )
    )
    (goal-fitness
        ( ; preconditions
            (search-depth ?D)
        )
        (+
            (* 0.9
                ; Calculate ratio of entities with a correct mindbody correlation.
                ; i.e. count(entities with a matching mindbody fact)/count(all entities)
                (/
                    (/ (length (find-all-facts ((?e entity)(?m mindbody)) (and (eq ?e:mind ?m:mind) (eq ?e:body ?m:body)))) 2)
                    (length (find-all-facts ((?e entity)) TRUE))
                )
            )
            (* 0.1 (/ 1 (+ ?D 1)))
        )
    )
)