def predict(self, item, **kwargs):
        """ Generates a prediction based on a given task item.
        Can make a prediction with verify and single-choice tasks.
        The current parameter assignment will be used for the prediction.
        Parameters
        ----------
        item : ccobra.data.Item
            Task item container. Holds information about the task, domain,
            response type and response choices.

        Returns
        -------
        list(str)
            Single-choice relational response in list representation (e.g.,
            ['A', 'left', 'B']) or a verification relational response in Boolean
            representation.

        """
        # initialize the spatial model
        spatial_model = main_module_param.MainModule()
        rel_prob = deepcopy(item.task) # get the problem premises
        # convert the premises to the form [A, relation, B] to be used by the spatial model
        for rel_prem in rel_prob:
            relation = rel_prem[0]
            rel_prem[0] = rel_prem[1]
            rel_prem[1] = relation
        # checks for the response type to choose an appropriate function from the
        # spatial model and a correct conversion of the question premise format.
        if item.response_type == "single-choice":
            # for single choice problems, the format of the question premises
            # is different than for the other problem_types.
            rel_questions = deepcopy(item.choices)
            for rel_pre in rel_questions:
                rel_pr = rel_pre[0] # unwrap the list of the actual premise
                relation = rel_pr[0]
                rel_pr[0] = rel_pr[1]
                rel_pr[1] = relation
                rel_pre = rel_pr
                rel_prob.append(rel_pr)
            # calls an apropriate function from the spatial model that will return
            # the given answer with the correct format for the evaluation in the framework.
            answer = spatial_model.interpret_spatial2exp_parameters(
                rel_prob, deepcopy(self.parameter_assignment))
            self.previous_model_ans.append(answer)
            return answer
        # for all verification problems the standard function will be called from the
        # spatial model. The format of the question premises is different, tha's
        # why the conversion is different aswell.
        rel_questions = deepcopy(item.choices[0]) # get the question premise
        #print("item choices: ", item.choices)
        for rel_pre in rel_questions:
            relation = rel_pre[0]
            rel_pre[0] = rel_pre[1]
            rel_pre[1] = relation
            rel_prob.append(rel_pre)
        answer = spatial_model.interpret_spatial_parameters_old(
            rel_prob, deepcopy(self.parameter_assignment))
        self.previous_model_ans.append(answer)
        return answer
    def predict(self, item, parameters=None, **kwargs):
        """ Generates a prediction based on a given task item.
        Can make a prediction with verify and single-choice tasks.
        The prediction will be made with the spatial model with individualizations.
        The spatial model takes a list of activation parameters. With these parameters,
        the spatial model will perform the task and return an appropriate answer.
        Depending on the task type, different answer types are returned(see below)
        Parameters
        ----------
        item : ccobra.data.Item
            Task item container. Holds information about the task, domain,
            response type and response choices.

        parameters : list
            The list with the activation values for all the individualizations
            in the model. (e.g., [[False, False, False, False, False, False],
            [False, False, False, False, False]]).

        Returns
        -------
        list(str)
            Single-choice relational response in list representation (e.g.,
            ['A', 'left', 'B']) or a verification relational response in Boolean
            representation.

        """
        if parameters is None:  # no parameters were given, use the current general assignment.
            parameters = self.parameter_assignment
        # initialize the spatial model
        spatial_model = main_module_param.MainModule()
        rel_prob = deepcopy(item.task)  # get the problem premises
        # convert the premises to the form [A, relation, B] to be used by the spatial model
        for rel_prem in rel_prob:
            relation = rel_prem[0]
            rel_prem[0] = rel_prem[1]
            rel_prem[1] = relation
        # checks for the response type to choose an appropriate function from the
        # spatial model and a correct conversion of the question premise format.
        if item.response_type == "single-choice":
            # for single choice problems, the format of the question premises
            # is different.
            rel_questions = deepcopy(item.choices)
            for rel_pre in rel_questions:
                rel_pr = rel_pre[0]  # unwrap the list of the actual premise
                relation = rel_pr[0]
                rel_pr[0] = rel_pr[1]
                rel_pr[1] = relation
                rel_pre = rel_pr
                rel_prob.append(rel_pr)
            # calls an apropriate function from the spatial model that will return
            # the given answer with the correct format for the evaluation in the framework.
            answer = spatial_model.interpret_spatial2exp_parameters(
                rel_prob, deepcopy(parameters))
            # store the answer given by the model for later use in the adapt function.
            self.previous_model_ans.append(answer)
            return answer
        # for all verification problems the standard function will be called from the
        # spatial model. The format of the question premises is different, that's
        # why the conversion is different.
        rel_questions = deepcopy(item.choices[0])  # get the question premise
        for rel_pre in rel_questions:
            relation = rel_pre[0]
            rel_pre[0] = rel_pre[1]
            rel_pre[1] = relation
            rel_prob.append(rel_pre)
        answer = spatial_model.interpret_spatial_parameters(
            rel_prob, deepcopy(parameters))
        # store the answer given by the model for later use in the adapt function.
        self.previous_model_ans.append(answer)
        return answer
예제 #3
0
    def predict(self, item, **kwargs):
        """ Generates a prediction based on a given task item.
        Can make a prediction with verify and single-choice tasks.
        The predict function will use the category parameters only for the problems
        of the 'premiseorder' data. For each of the three problem types, the
        corresponding active individualizations will be applied on the problem
        or used in the spatial model when processing the problems. There is a
        parameter to trigger the inversion of the answer given by the model as
        well. This will not affect the way the model is build.
        The answers for the processed problems will be stored for later use in the
        adapt function.
        Parameters
        ----------
        item : ccobra.data.Item
            Task item container. Holds information about the task, domain,
            response type and response choices.

        Returns
        -------
        list(str)
            Single-choice relational response in list representation (e.g.,
            ['All', 'managers', 'clerks']).

        """
        # variable to make the model answer wrong/invert the answer
        invert_ans = False
        # initialize the spatial model
        spatial_model = main_module_param.MainModule()
        rel_prob = self.convert_premises(deepcopy(item.task)) # get the problem premises
        #print("item task: ", rel_prob)
        # convert the premises to the form [A, relation, B] to be used by the spatial
        #print("converted item task: ", rel_prob)
        # checks for the response type to choose an appropriate function from the
        # spatial model and a correct conversion of the question premise format.
        if item.response_type == "single-choice": # No change for single-choice problems
            # the categorization cannot be used for single choice problems
            print("problem cannot be processed by categories model")
            return None
        ##### Problem conversion #####
        # for all verification problems the standard function will be called from the
        # spatial model. The format of the question premises is different, tha's
        # why the conversion is different aswell.
        rel_questions = deepcopy(item.choices[0]) # get the question premise
        for rel_pre in rel_questions:
            relation = rel_pre[0]
            rel_pre[0] = rel_pre[1]
            rel_pre[1] = relation
            rel_prob.append(rel_pre)
        ##### Categorization/decide which cat_param to use ######
        # parameters for problem type 1, 2 and 3 in order.
        if len(item.choices) == 1 and len(rel_prob) == 4:
            # determine the problemtype
            # check for problemtype 3; if the 3rd premise contains C and B, it has to be type 3
            if (rel_prob[2][0] == "B" and rel_prob[2][2] == "C") or (
                    rel_prob[2][0] == "C" and rel_prob[2][2] == "B"):
                self.previous_problem_type.append(3)
                # check for the parameters activated by the categorization
                if self.cat_params[2][0]:
                    # A or D first category; combine should be done wrong!
                    # change the 3rd premise in the problem/ other option:
                    # activate the corresponding parameter of the model
                    # check if the first premise contains A/B or D/C!
                    if (rel_prob[0][0] == "D" and rel_prob[0][2] == "C") or (
                            rel_prob[0][0] == "C" and rel_prob[0][2] == "D"):
                        # combination should be wrong then
                        # invert the relation of the third premise to
                        # change the combination process
                        rel_prob[3] = self.invert_premise(rel_prob[3])
                elif self.cat_params[2][3]:
                    invert_ans = True
                elif self.cat_params[2][1]:
                    # the relation of the task premises should be inverted
                    # to generate wrong results.
                    # this is only if the L relation is worse than the
                    # R relation, invert the L into R
                    if rel_prob[0][1] == "L": #check if the relation is L
                        #print("used cat3 param2")
                        rel_prob[0][1] = "Right"
                        rel_prob[1][1] = "Right"
                        rel_prob[2][1] = "Right"
                elif self.cat_params[2][2]:
                    # the relation of the task premises should be
                    # inverted to generate wrong results.
                    # this is only if the R relation is worse than
                    # the L relation, invert the R into L
                    if rel_prob[0][1] == "R": #check if the relation is R
                        #print("used cat3 param3")
                        rel_prob[0][1] = "Left"
                        rel_prob[1][1] = "Left"
                        rel_prob[2][1] = "Left"
            # check for problemtype 2; if the first premise contains
            # C and B, it has to be type 2
            elif (rel_prob[0][0] == "B" and rel_prob[0][2] == "C") or (
                    rel_prob[0][0] == "C" and rel_prob[0][2] == "B"):
                self.previous_problem_type.append(2)
                # check for the parameters activated by the categorization
                # if the relation of task and question is not the same and
                # the answers are wrong then invert the relation of the
                # question premise, since one of the two different relations
                # lead to a mistake in understanding the premises.
                if self.cat_params[1][0]:
                    # if activated, simply invert question relation
                    if rel_prob[0][1] != rel_prob[3][1]: # check if the relations are different
                        rel_prob[3] = self.invert_premise(rel_prob[3])
                elif self.cat_params[1][1]:
                    invert_ans = True
            else: # Since the problem is not type 3 and 2, it has to be type 1
                self.previous_problem_type.append(1)
                # check for the parameters activated by the categorization
                # if the problem starts with A/B, the results are sometimes better
                # this parameter inverts the relations of the task, if
                # activated and model starts with C/D. The second parameter
                # is the same the other way around with A/B being worse than C/D.
                if self.cat_params[0][0]: # if the ab category is better than the cd category
                    if (rel_prob[0][0] == "C" and rel_prob[0][2] == "D"
                       ) or (rel_prob[0][0] == "D" and rel_prob[0][2] == "C"):
                        # first premise has d and c in it, invert the
                        # relations of the task premises to change the result
                        rel_prob = self.invert_premises(rel_prob)
                elif self.cat_params[0][1]:
                    # The other way around for the first category. If the
                    # vp can solve problems better with C/D as the first
                    # premise and has problems with A/B.
                    # also activates the parameter to change the combination.
                    if (rel_prob[0][0] == "A" and rel_prob[0][2] == "B"
                       ) or (rel_prob[0][0] == "B" and rel_prob[0][2] == "A"):
                        # invert the relations of the premises,
                        # this way the answer will be changed as well
                        rel_prob = self.invert_premises(rel_prob)
                elif self.cat_params[0][2]:
                    invert_ans = True
            answer = spatial_model.interpret_spatial_parameters(
                rel_prob, deepcopy(self.parameter_assignment))
            self.previous_correct_ans.append(answer) # store the given answer
            if invert_ans:
                return not answer
            return answer