Ejemplo n.º 1
0
    def __getCandidates__(self, **kwargs) -> list:
        """
            Description:

                Returns a list of candidates for the specified algorithm.

            |\n
            |\n
            |\n
            |\n
            |\n

            Args:

                + optimizer = ( enum ) The optimzier
                    ~ Required

                + params    = ( dict ) The optimizer's parameters
                    ~ Required

                + initial   = ( list ) The initial candidate

                + region    = ( float ) The algorithm's current region
                    ~ Required if <optimizer="tro">

            |\n

            Returns

                + list      = ( list ) A list of new candidates
        """

        #   STEP 0: Local variables
        lCandidates = []

        #   STEP 1: Setup - Local variables

        #   STEP 2: Check if optimizer passed
        if ("optimizer" not in kwargs):
            #   STEP 3: Error handling
            raise Exception(
                "An error occured in SpongeBob.__getCandidates__() -> Step 2: No optimizer passed"
            )

        #   STEP 4: Check optimizer parameters passed
        if ("params" not in kwargs):
            #   STEP 5: Error handling
            raise Exception(
                "An error occured in SpongeBob.__getCandidates__() -> Step 4: No optimizer parameters passed"
            )

        #   STEP 6: Check if initial candidate passed
        if ("initial" not in kwargs):
            #   STEP 7: Error handling
            raise Exception(
                "An error occured in SpongeBob.__getCandidates__() -> Step 6: No initial candidate passed"
            )

        #   STEP 8: Check if optimizer is tro
        if (kwargs["optimizer"] == ga.TRO):
            #   STEP 9: Check if region passed
            if ("region" not in kwargs):
                #   STEP 10: Error handling
                raise Exception(
                    "An error occured in SpongeBob.__getCandidates__() -> Step 9: No region passed"
                )

            #   STEP 11: Iterate through the required number of candidates
            for _ in range(0, kwargs["params"]["candidates"]):
                #   STEP 12: Get temporary candidate
                lTmp = Helga.getShape(kwargs["initial"])

                #   STEP 13: Iterate through candidate
                for i in range(0, len(lTmp)):
                    #   STEP 14: Check if single point
                    if (type(lTmp[i]) == float):
                        #   STEP 16: Modify value using region and scalar
                        lTmp[i] = rn.gauss(
                            kwargs["initial"][i],
                            kwargs["params"]["scalar"] * kwargs["region"])

                        #   STEP 17: Check if value above upper limit
                        if (lTmp[i] > 1.0):
                            #   STEP 18: Limit value
                            lTmp[i] = 1.0

                        #   STEP 19: Check if value below lower limit
                        if (lTmp[i] < -1.0):
                            #   STEP 20: Limit value
                            lTmp[i] = -1.0

                    else:
                        #   STEP 21: Iterate through list
                        for j in range(0, len(lTmp[i])):
                            #   STEP 22: Modify value using region and sacalar
                            lTmp[i][j] = rn.gauss(
                                kwargs["initial"][i][j],
                                kwargs["params"]["scalar"] * kwargs["region"])

                            #   STEP 23: Check if value above upper limit
                            if (lTmp[i][j] > 1.0):
                                #   STEP 24: Limit value
                                lTmp[i][j] = 1.0

                            #   STEP 25: Check if value below lower limit
                            if (lTmp[i][j] < -1.0):
                                #   STEP 26: Limit value
                                lTmp[i][j] = -1.0

                #   STEP 22: Append new candidate to output list
                lCandidates.append(lTmp)

            #   STEP 23: Return
            return lCandidates

        #   STEP ??: Error handling
        raise Exception(
            "An error occured in SpongeBob.__getCandidates__(): Unimplemented optimizer passed"
        )
    def __getCandidates__(self, **kwargs) -> list:
        """
            Description:

                Returns a list of candidates for the specified algorithm.

            |\n
            |\n
            |\n
            |\n
            |\n

            Args:

                + optimizer     = ( enum ) The optimizer
                    ~ Required
                + params        = ( dict ) The optimizer's parameters
                    ~ Required
                + initial       = ( list ) The initial candidate
                    ~ Required

            |\n

            Returns:

                + list          = ( list ) A list of new candidates

        """

        #   STEP 0: Local variables
        lCandidates             = []
        lShape                  = None

        #   STEP 1: Setup - Local variables
        lShape                  = Helga.getShape(kwargs["initial"])

        #   region STEP 2->??: PSO Candidate generation

        #   STEP 2: Check if PSO
        if (kwargs["optimizer"] == sw.PSO):
            #   STEP 3: Append the initial candidate to the candidate list
            lCandidates.append(kwargs["initial"])

            #   STEP 4: Iterate through remaining required candidates
            for _ in range(1, kwargs["params"]["candidates"]):
                #   STEP 5: Get new empty candidate
                lTmp = cp.deepcopy(lShape)

                #   STEP 6: Iterate through candidate list
                for i in range(0, len(lTmp)):
                    #   STEP 7: Check if single data point
                    if (type(lTmp[i]) == float):
                        #   STEP 8: Set value
                        lTmp[i] = rn.uniform(-1.0, 1.0)

                    else:
                        #   STEP 9: Iterate through list
                        for j in range(0, len(lTmp[i])):
                            #   STEP 10: Set value
                            lTmp[i][j] = rn.uniform(-1.0, 1.0)

                #   STEP 11: Append the candidate to the output list
                lCandidates.append(lTmp)

            #   STEP 12: Return
            return lCandidates

        #   STEP ??: Error handling
        raise Exception("An error occured in Sarah.__getCandidates__(): Unimplemented optimizer passed")