def __helpOP(self) -> None:
        """
        """

        #   STEP 0: Local variables
        dMenu = {}

        #   STEP 1: Setup - Local variables
        dMenu = self.__cf.data["help"]["help op"]

        #   STEP 2: User Output
        print(
            "Irene (H-OP) {" + Helga.time() +
            "} - The following options exist in the Antenna Optimization menu."
        )

        for i in range(0, dMenu["items"]):
            print(dMenu[str(i)])

        print("")

        #   STEP 3: Wait for user to continue
        input("\t> Continue")
        os.system("cls")

        #   STEP 4: Return
        print("Irene (OP) {" + Helga.time() +
              "} - How would you like to continue?")
        return
    def main(self):
        """
        """

        #   STEP 0: Local variables
        #   STEP 1: Setup - local variables
        #   STEP 2: User Output
        print(
            "Irene (Main) {" + Helga.time() +
            "} - Welcome. How would you like to start the optimization process?"
        )

        #   STEP 3: User Input
        while (True):
            ui = Rae.getUserInput(self.__enum, menu="main")
            os.system("cls")

            if (ui == 0):
                self.__newGeometry()

            elif (ui == 1):
                self.__importGeometry()

            elif (ui == 2):
                print("Irene (C-Editor) {" + Helga.time() +
                      "} - This functionality is not implemented yet")  #TODO

            elif (ui == 3):
                self.__helpMain()

            elif (ui == 4):
                print("Irene (Exit) {" + Helga.time() + "} - Bye.")
                break

        return
Example #3
0
    def __getUI(self, _lMenu: list, _bBreakOnInvalid: bool) -> int:
        """
        """

        #   STEP 0: Local variables
        #   STEP 1: Setup - Local Variables
        #   STEP 2: User Input
        while (True):
            #   STEP 3: Output menu
            for i in range(0, len(_lMenu)):
                print("\t" + str(i) + ": " + _lMenu[i])

            print("")

            #   STEP ..: Wait for user input
            ui = input("\t>")
            os.system("cls")

            #   STEP 4: Verify input to be type integer
            try:
                #   STEP 5: Try to cast to int
                ui = int(ui)

                #   STEP 6: Check that the int is in range
                if ((ui >= 0) and (ui < len(_lMenu))):
                    #   STEP 7: Return
                    return ui

                elif (_bBreakOnInvalid):
                    return -1

                else:
                    #   STEP 8: Not in range
                    print("Rae {" + Helga.time() + "} - Invalid Input")

            except:
                if (_bBreakOnInvalid):
                    break

                print("Rae (Menu-UI) {" + Helga.time() + "} - Invalid Input")

        return -1

    #
    #   endregion

    #
    #endregion


#
#endregion
    def __getParams(self) -> list:
        """
        """

        #   STEP 0: Local variables
        cfTmp = Conny()

        lMenu = []
        lOut = []

        #   STEP 1: Setup - Local variables
        cfTmp.load(self.__enum.value)

        lMenu = cfTmp.data["menus"]["params"]

        #   STEP 2: Iterate through params
        for i in range(0, lMenu["items"]):
            #   STEP 3: Check if substrate
            if (i == 2):
                lOut.append(self.__getSubstrate())

            elif (i == 3):
                lOut.append(self.__getSubstrateHeight())

            else:
                print("Irene (NG) {" + Helga.time() + "} - Please define (" +
                      lMenu[str(i)] + ") {cm}")

                #   STEP 4: User input
                while (True):
                    #   STEP 5: Wait for input
                    ui = input("\t>")
                    os.system("cls")

                    #   STEP 6: Verify input
                    try:
                        #   STEP 7: Cast to float
                        ui = float(ui)

                        lOut.append(ui)
                        break

                    except:
                        print("Irene (NG) {" + Helga.time() +
                              "} - Invalid Input")

        return lOut
    def __getSubstrateHeight(self) -> float:
        """
        """

        #   STEP 0: Local variables
        lTmpMenu = self.__cf.data["menus"]["substrate heights"]

        #   STEP 1: Setup - Local variables
        #   STEP 2: User Output
        print(
            "Irene (NG) {" + Helga.time() +
            "} - Please choose one of the following default substrate heights")

        #   STEP 3: User Input
        while (True):
            #   STEP 4: Output menu
            for i in range(0, lTmpMenu["items"]):
                print("\t" + str(i) + ": " + str(lTmpMenu[str(i)]) + " {mm}")

            print("")

            #   STEP 5: Wait for input
            ui = input("\t>")
            os.system("cls")

            #   STEP 6: Verify input
            try:
                #   STEP 7: Cast to int
                ui = int(ui)

                #   STEP 8: Verify range
                if ((ui >= 0) and (ui < lTmpMenu["items"])):
                    return lTmpMenu[str(ui)]

                else:
                    print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

            except:
                print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

        return 0.0
    def __getFreq(self) -> list:
        """
        """

        #   STEP 0: Local variables
        cfTmp = Conny()

        lMenu = []
        lOut = []

        #   STEP 1: Setup - Local variables
        cfTmp.load(self.__enum.value)

        lMenu = cfTmp.data["menus"]["frequency"]

        #   STEP 2: Get frequency menu
        for i in range(0, lMenu["items"]):
            #   STEP 3: User output
            print("Irene (NG) {" + Helga.time() + "} - Please define (" +
                  lMenu[str(i)] + ")")

            #   STEP 4: user input
            while (True):
                #   STEP 5: Wait for input
                ui = input("\t>")
                os.system("cls")

                #   STEP 6: Verify input
                try:
                    #   STEP 7: Cast to float
                    ui = float(ui)

                    lOut.append(ui)
                    break

                except:
                    print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

        #   STEP 8: Return
        return lOut
Example #7
0
    def main(self):
        """
        """

        #   STEP 0: Local variables
        #   STEP 1: Setup - Local variables

        #   STEP 2: User Output
        print("Heimi (Main) {" + Helga.time() +
              "} - Hi. What program would you like to run?")

        #   STEP 3: User Input
        while (True):
            ui = Rae.getUserInput(self.__enum, menu="main")
            os.system("cls")

            if (ui == 0):
                #   STEP 4: If OMA then send to Irene
                print("Heimi (Main.OMA) + {" + Helga.time() +
                      "} - Transferring control to Irene. See you soon.")

                irene = Irene()
                irene.main()

                print("Hiemi (Main.OMA) + {" + Helga.time() +
                      "} - Welcome back.")

            elif (ui == 1):
                #   STEP 5: Ha, jokes on you. There is no help for you
                print("Heimi (Main.Help) {" + Helga.time() +
                      "} - Ummmm, really? Lol")

            elif (ui == 2):
                #   STEP 6: Exit program
                print("Heimi (Main.Exit) {" + Helga.time() +
                      "} - Exiting program.")
                break

        #   STEP 7: Return
        return
Example #8
0
    def __train_srgHandler__(self, _eExit, _eGlobal, _eTR, _qTR) -> None:
        """
            Description:

                Iteratively trains surrogates until it either finds a surrogate
                that meets the minimum accuracy requirements or until it has
                trained the maximum amount of surrogates.

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

            Arguments:

                + _eExit    = ( mp.Event() ) Global exit event

                + _eGlobal  = ( mp.Event() ) Global thread result event

                + _eTR      = ( mp.Event() ) Surrogate training result event

                + _qTR      = ( mp.Queue() ) Surrogate training result queue
        """

        #   STEP 0: Local variables
        dArgs                   = _qTR.get()[0]

        vData                   = dArgs["data"]

        lAccuracy               = []
        lFitness                = []
        lResults                = []

        fBest_Fitness           = np.inf
        iBest_Index             = 0

        print("\t{" + Helga.time() + "} - Starting surrogate training\t\t-> ", end="")

        #   STEP 1: Iterate through max surrogates
        for i in range(0, dArgs["max"]):
            #   STEP 2: Get new surrogate
            dSRG    = self.__getSurrogate__(region=dArgs["region"])

            vSRG    = dSRG["surrogate"]

            #   STEP 3 Do necessary pre-training
            vSRG.bShowOutput            = False
            vSRG.bUse_NoiseInjection    = True
            vSRG.bUse_L1                = True
                
            #   STEP 4: Train surrogate
            fTmp_Fitness    = None

            fTmp_Fitness    = vSRG.trainSet(cp.deepcopy(vData), advanced_training=False, compare=False)
            fTmp_Fitness    = fTmp_Fitness["fitness"]

            #   STEP 5: Get accuracy and fitness
            fTmp_Accuracy   = vSRG.getAccuracy(data=vData, size=vData.getLen(), full_set=True)

            fTmp_Fitness    = fTmp_Fitness * ( 1.1 - fTmp_Accuracy["percent accuracy"] )

            #   STEP 6: Append to output lists
            lAccuracy.append( fTmp_Accuracy["percent accuracy"] )
            lFitness.append( fTmp_Fitness )
            lResults.append( vSRG )

            #   STEP 7: Check if fittest surrogate so far
            if ((fTmp_Fitness < fBest_Fitness) and (fTmp_Accuracy["percent accuracy"] == 1.0)):
                #   STEP 8: Update - Local variables
                fBest_Fitness   = fTmp_Fitness
                iBest_Index     = i

                #   STEP 9: User output - minimal
                print("!", end="")

            #   STEP 10: Check if 100p accuracy
            elif (fTmp_Accuracy["percent accuracy"] == 1.0):
                #   STEP 11: Minimal output
                print(":", end="")

            #   STEP 12: Not 100p but best fitness
            elif (fTmp_Fitness < fBest_Fitness):
                #   STEP 13: Update - Local variables
                fBest_Fitness   = fTmp_Fitness
                iBest_Index     = i

                #   STEP 14: User output - minimal
                print("#", end="")

            #   STEP 15: Bad surrogate
            else:
                #   STEP 16: User outptu - minimal
                print(".", end="")

            #   STEP 17: Check if fitness below required and min surrogates generated 
            if ((fBest_Fitness < dArgs["acc"]) and (i >= dArgs["min"])):
                #   STEP 18: Exit loop early
                break

            #   STEP 19: Check if exit even
            if (_eExit.is_set()):
                #   STEP 20: Exit loop early
                break

        print("")
        
        #   STEP 21: Swap best surrogate to index = 0
        vTmp_Swap               = lAccuracy[0]
        lAccuracy[0]            = lAccuracy[iBest_Index]
        lAccuracy[iBest_Index]  = vTmp_Swap
        
        vTmp_Swap               = lFitness[0]
        lFitness[0]             = lFitness[iBest_Index]
        lFitness[iBest_Index]   = vTmp_Swap

        vTmp_Swap               = lResults[0]
        lResults[0]             = lResults[iBest_Index]
        lResults[iBest_Index]   = vTmp_Swap

        #   STEP 22: Populate output dictionary
        dOut    = {
            "accuracy": lAccuracy,
            "fitness":  lFitness,
            "results":  lResults
        }

        #   STEP 23: Put output in queue
        _qTR.put([dOut])

        #   STEP 24: Set events
        _eTR.set()
        _eGlobal.set()

        #   STEP 25: Return
        return
Example #9
0
    def trainAndMap(self, **kwargs) -> dict:
        """
            Description:

                Uses multi-threading to train and map multiple surrogate models
                simultaneously.

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

            Arguments:

                + data  = ( vars ) Data container instance that contains the
                    dataset required for training and mapping

                + region    = ( float ) The region within which random 
                    generation is allowed
                    ~ Required

                + rand  = ( bool ) A flag that specifies whether or not random
                    parameters are allowed for the training and mapping process

                + remap = ( bool ) A flag to indicate if the results of the
                    mapping should be un-normalized via the dataset

            |\n

            Returns:

                + dOut = ( dict ) A dictionary containing the best surrogate
                    result and the fitness of the results
                    ~ "result"  = ( dict )
                    ~ "fitness" = ( dict )
                    
            |\n

            ToDo:

                + Add start arg
        """

        #   STEP 0: Local variables

        #   STEP 1: Setup - Local variables

        #   region STEP 2->10: Error checking

        #   STEP 2: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 3: Error handling
            raise Exception("An error occured in Golem.trainAndMap() -> Step 2: No data arg passed")

        #   STEP 4: Check if region arg passed
        if ("region" not in kwargs):
            #   STEP 5: Error handling
            raise Exception("An error occured in Golem.trainAndMap() -> Step 4: No region arg passed")

        #   STEP 6: Check if rand arg passed
        if ("rand" in kwargs):
            #   STEP 7: Update - Class var
            self.bSRG_Random = kwargs["rand"]

        #
        #   endregion

        #   STEP 9: Train surrogates
        self.__train_srgOverseer__(data=kwargs["data"], region=kwargs["region"])

        if  ( True ):
            print("\t\t- Fitness: " + str(self.__lSRG_FItness[0]), "Accuracy: " + str(self.__lSRG_Accuracy[0]), sep="\t", end="\n\n")

        #   STEP 10: Map surrogates
        self.__map_srgOverseer__(data=kwargs["data"])

        #   STEP 11: Check for remapping
        if ("remap" in kwargs):
            #   STEP 12: Check remapping state
            if (kwargs["remap"]):
                #   STEP 13: Remap
                self.vBest = kwargs["data"].remap( candidate=cp.deepcopy( self.vBest ) )

                #   STEP 14: Loop through best candidates
                for i in range(0, len( self.lMap_Results )):
                    #   STEP 15: Outsource - Remapping
                    self.lMap_Results[i] = kwargs["data"].remap( candidate=cp.deepcopy( self.lMap_Results[i] ))

        #   STEP 16: User output
        if ( self.bShowOutput ):
            print("\n\t~ Train and Map results:")
            print("\t\t- Candidate: ", Helga.round(self.lMap_Results[0], 2))
            print("\t\t- Fitness: ", round(self.lMap_Fitness[0], 2), end="\n\n")

        #   STEP 17: Populate output dict
        dOut = {
            "result":   self.vBest,
            "fitness":  self.vFitness
        }

        #   STEP 15: Return
        return dOut
Example #10
0
    def __trainSurrogate__(self, **kwargs) -> vars:
        """
            Description:
            
                Trains the passed surrogate using thread techniques. If an optimizer is specified,
                that surrogate will be more likely to be used during the threaded training 
                process. However, if no optimizer is specified then all optimizers will have the 
                same probability of being used. 

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

            Args:

                + surrogate   = ( vars ) A surrogate class instance
                    ~ Required

                + data        = ( vars ) Data container
                    ~ Required

                + password    = ( int ) Surrogate password
                    ~ Required

                + optimizer   = ( enum ) The enum of the optimizer to be used
                    ~ Default = { Random }

            |\n

            Returns:

                + surrogate   = ( vars ) The optimized surrogate instance
        """
        
        #   STEP 0: Local variables
        eGlobal                 = None
        eGlobal_Exit            = None

        eUI_Event               = None
        qUI_Queue               = None
        tUI_Thread              = None

        lUO_Lock                = None

        lThread_Data            = []
        lThread_Results         = []
        iThread                 = 8
        iThread_ID              = 0

        fFittest                = np.inf
        iFittest                = 0

        #   STEP 1: Setup - Local variables

        #   region STEP 2->9: Error checking

        #   STEP 2: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 3: Error handling
            raise Exception("An error occured in Hermione.__trainSurrogate__() -> Step 2: No data arg passed")

        #   STEP 4: Check if surrogate arg passed
        if ("surrogate" not in kwargs):
            #   STEP 5: Error handling
            raise Exception("An error occured in Hermione.__trainSurrogate__() -> Step 4: No surrogate arg passed")
        
        #   STEP 6: Check if optimizer arg passed
        if ("optimizer" not in kwargs):
            #   STEP 7: Error handling
            raise Exception("An error occured in Hermione.__trainSurrogate__() -> Step 6: No optimizer arg passed")
        
        #   STEP 8: Check if password arg passed
        if ("password" not in kwargs):
            #   STEP 9: Error handling
            raise Exception("An error occured in Hermione.__trainSurrogate__() -> Step 8: No password arg passed")
        
        #
        #   endregion

        #   region STEP 10->12: Setup - Global variables

        #   STEP 10: Setup - Global variables
        eGlobal                 = mp.Event()
        eGlobal.clear()

        eGlobal_Exit            = mp.Event()
        eGlobal_Exit.clear()

        #   STEP 11: Setup - UI Thread
        eUI_Event               = mp.Event()
        qUI_Queue               = mp.Queue()

        tUI_Thread              = tr.Thread(target=self.__threadUI__, args=(eGlobal_Exit, eGlobal, eUI_Event, qUI_Queue, ))
        tUI_Thread.daemon       = True
        #tUI_Thread.start()

        lUO_Lock                = mp.RLock()

        #   STEP 12: Setup - Thread data list
        for _ in range(0, 4):
            lThread_Data.append(None)

        #
        #   endregion

        #   STEP 13: User output
        if (self.bShowOutput):
            #   STEP 14: Acquire lock
            lUO_Lock.acquire()

            #   STEP 15: print
            print("Hermione (train-thread) {" + Helga.time() + "} - Starting threaded surrogate training.\n")

            #   STEP 16: Release
            lUO_Lock.release()

        #   region STEP 17->46: Training process

        #   STEP 17: Loop bish
        while (True):

            #   region STEP 18->36: Thread creation

            #   STEP 18: Iterate through training events
            for i in range(0, len(lThread_Data)):
                #   STEP 19: Check if event is currently None
                if (lThread_Data[i] == None):
                    #   STEP 20: Create a new event
                    eTmp_Event          = mp.Event()
                    eTmp_Event.clear()

                    #   STEP 21: Create new thread dictionary
                    dTmp_Thread = {
                        "surrogate":    cp.deepcopy(kwargs["surrogate"]),
                        "data":         cp.deepcopy(kwargs["data"]),
                        "optimizer":    kwargs["optimizer"],

                        "id":           iThread_ID,
                        "password":     kwargs["password"],
                        "thread":       i
                    }

                    #   STEP 22: Create a new queue
                    qTmp_Queue          = mp.Queue()
                    qTmp_Queue.put([dTmp_Thread])
                    
                    #   STEP 23: Create new thread
                    tTmp_Thread         = mp.Process(target=self.__threadTrain__, args=(eGlobal_Exit, eTmp_Event, qTmp_Queue, lUO_Lock, ))
                    tTmp_Thread.daemon  = True
                    tTmp_Thread.start()
                    
                    #   STEP 24: Set thread variable
                    lThread_Data[i]     = [tTmp_Thread, eTmp_Event, qTmp_Queue]

                    #   STEP 25: Set training flag and icnrement thread id
                    iThread_ID          += 1

                #   STEP 26: Event not None
                else:
                    #   STEP 27: Check if thread has exited
                    if (lThread_Data[i][1].is_set() == True):
                        #   STEP 28: Clear event
                        lThread_Data[i][1].clear()

                        #   STEP 29: Append data
                        lThread_Results.append( lThread_Data[i][2].get()[0] )

                        #   STEP 30: Check if max threads not reached
                        if (iThread_ID < iThread):
                            #   STEP 31: Create new thread dictionary
                            dTmp_Thread = {
                                "surrogate":    cp.deepcopy(kwargs["surrogate"]),
                                "data":         cp.deepcopy(kwargs["data"]),
                                "optimizer":    kwargs["optimizer"],

                                "id":           iThread_ID,
                                "password":     kwargs["password"],
                                "thread":       i
                            }

                            lThread_Data[i][2].put( [dTmp_Thread] )

                            #   STEP 32: Create a new thread
                            tTmp_Thread         = mp.Process(target=self.__threadTrain__, args=(eGlobal_Exit, lThread_Data[i][1], lThread_Data[i][2], lUO_Lock, ))
                            tTmp_Thread.daemon  = True
                            tTmp_Thread.start()

                            #   STEP 33: Set thread var
                            lThread_Data[i][0]  = tTmp_Thread
                            
                            #   STEP 34: Increment thread id
                            iThread_ID          += 1

            #   STEP 35: Check if 16 threads reached
            if ( len( lThread_Results ) == iThread):
                #   STEP 36: Exit loop
                break

            #
            #   endregion

            #   region STEP 37->46: Ui support

            #   STEP 37: Check if ui thread is set
            if (eUI_Event.is_set() == True):
                #   STEP 38: Clear event
                eUI_Event.clear()

                #   STEP 39: Check if ui output == "stop"
                if (qUI_Queue.get()[0] == "exit"):
                    #   STEP 40: Set thread joining event
                    eGlobal_Exit.set()
                    #tUI_Thread.join()

                    #   STEP 41: Loop through training threads
                    for i in range(0, len( lThread_Data )):
                        #   STEP 42: Check if thread is still training
                        if (lThread_Data[i][0].is_alive() == True):
                            #   STEP 43: Join thread
                            lThread_Data[i][0].join()

                            #   STEP 44: Save results
                            lThread_Results.append( lThread_Data[i][2].get()[0] )

                    #   STEP 45: Reset thread joining event
                    eGlobal_Exit.clear()

                    #   STEP 46: Exit loop
                    break

            #
            #   endregion

        #
        #   endregion

        #   STEP 47: Iterate through results
        for i in range(0, len( lThread_Results )):
            #   STEP 48: Check if fitter than current best
            if (lThread_Results[i]["fitness"] < fFittest):
                #   STEP 49: Set new best candidate
                fFittest    = lThread_Results[i]["fitness"]
                iFittest    = i

        #   STEP 50: Return
        return lThread_Results[iFittest]
Example #11
0
    def __threadMap__(self, _eExit, _eTr, _qTr, _lUO) -> None:
        """
            Description:

                This function outsources the mapping of the surrogate to the
                appropriate optimization handler after picking the optimizer
                to use.

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

            Parameters:

                + _eGlobal_Exit  = ( mp.Event() ) Event signalling global exit
                    for threads and processes

                + _eTr      = ( mp.Event() ) Event signalling process
                    completion

                + _qTr      = ( mp.Queue() ) The queue onto which the process
                    results should be returned

                + _lUO      = ( mp.RLock() ) The lock for commong user output

            |\n

            Returns:

                + dOut  = ( dict )
                    ~ "result"  = ( list ) The list of surrogate inputs that
                        yielded the best results

                    ~ "fitness" = ( float ) The fitness of the best results
        """
        
        #   STEP 0: Local variables
        dArgs                   = _qTr.get()[0]
        dResults                = None

        iThread_ID              = Helga.ticks()
        iThread_AppID           = dArgs["thread"]

        iSwarms_Active          = 0
        iGA_Active              = 0

        iOptimizers_Active      = 0

        #   region STEP 1->15: Map using provided optimizer

        #   STEP 1: Check if not random optimizer
        if (rn.uniform(0.0, 1.0) > 0.3):
            #   STEP 2: Check if optimizer is GA
            if (ga.isEnum(dArgs["optimizer"])):
                #   STEP 3: User output
                if (self.bShowOutput):
                    #   STEP 4: Get lock
                    _lUO.acquire()

                    #   STEP 5: Populate strings list for threaded output
                    print("\t- Assigning SpongeBob to mapping")
                    print("\t- Optimizer: " + str(dArgs["optimizer"]))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 6: Release lock
                    _lUO.release()

                #   STEP 7: Create new mapper
                sb          = SpongeBob()

                #   STEP 8: Outsource mapping
                dResults    = sb.mapSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], optimizer=dArgs["optimizer"])

            #   STEP 9: Check if swarm
            if (sw.isEnum(dArgs["optimizer"])):
                #   STEP 10: User output
                if (self.bShowOutput):
                    #   STEP 11: Get lock
                    _lUO.acquire()

                    #   STEP 12: Populate strings list for threaded output
                    print("\t- Assigning Sarah to mapping")
                    print("\t- Optimizer: " + str(dArgs["optimizer"]))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 13: Release lock
                    _lUO.release()

                #   STEP 14: Create new mapper
                sh          = Sarah()

                #   STEP 15: Outsource mapping
                dResults    = sh.mapSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], optimizer=dArgs["optimizer"])

        #
        #   endregion

        #   region STEP 16->34: Map using random optimizer

        #   STEP 16: Using random optimizer for mapping
        else:
            #   STEP 17: Update - Local variables
            iSwarms_Active      = sw.getNumActiveSwarms()
            iGA_Active          = ga.getNumActiveGAs()

            iOptimizers_Active  = iSwarms_Active + iGA_Active

            #   STEP 18: Choose a random optimizer
            iTmp_Optimizer  = rn.randint(0, iOptimizers_Active - 1)

            #   STEP 19: Check if swarm:
            if (iTmp_Optimizer < iSwarms_Active):
                #   STEP 20: Get optimizer enum
                eTmp_Optimizer  = sw.getActiveSwarms()[iTmp_Optimizer]

                #   STEP 21: User output
                if (self.bShowOutput):
                    #   STPE 22: Acquire lock
                    _lUO.acquire()

                    #   STEP 23: Populate output strings
                    print("\t- Assigning Sarah to training")
                    print("\t- Optimizer: " + str(eTmp_Optimizer))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 24: Release lock
                    _lUO.release()

                #   STEP 25: Create new mapper
                sh          = Sarah()

                #   STEP 26: Outsource
                dResults    = sh.mapSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], optimizer=eTmp_Optimizer)

            #   STEP 27: Then ga
            else:
                #   STEP 28: Get optimizer enum
                eTmp_Optimizer  = ga.getActiveGAs()[iTmp_Optimizer - iSwarms_Active]

                #   STEP 29: User output
                if (self.bShowOutput):
                    #   STEP 30: Acquired lock
                    _lUO.acquire()

                    #   STEP 31: Populate output strings
                    print("\t- Assigning SpongeBob to training")
                    print("\t- Optimizer: " + str(eTmp_Optimizer))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 32: Release lock
                    _lUO.release()

                #   STEP 33: Create new mapper
                sb          = SpongeBob()

                #   STEP 34: Outsource mapping
                dResults    = sb.mapSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], optimizer=eTmp_Optimizer)

        #
        #   endregion
        
        #   Step 35: User output
        if (self.bShowOutput):
            #   STEP 36: Get lock
            _lUO.acquire()

            #   STEP 37: Create output strings
            print("\t\t\t\t\t- Thread: " + str(iThread_AppID) +  " - <" + str( round( 100.0 * dResults["fitness"], 3 ) ) + ">\n")

            #   STEP 38: Release lock
            _lUO.release()
        
        #   STEP 39: Set results
        _qTr.put([dResults])
        
        #   STEP 40: Set exit event
        _eTr.set()

        #   STEP 41: Return
        return
Example #12
0
    def mapSurrogate(self, **kwargs) -> dict:
        """
            Description:

                Maps the passed surrogate using the passed dataset.

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

            Arguments:

                + data  = ( vars ) A Data container instance containing the
                    dataset to be used for mapping
                    ~ Required

                + surrogate = ( vars ) A surrogate model to map
                    ~ Required

                + optimizer = ( enum ) The enum of the optimizer to be used
                    during the mapping process
                    ~ Default   = PSO

                + thread    = ( bool ) A flag that indicates if threading 
                    should be used to map the surrogate
                    ~ Default   = False
        """

        #   STEP 0: Local variables
        eOptimizer              = sw.PSO
        bThreading              = False

        #   STEP 1: Setup - Local variables
        
        #   region STEP 2->5: Error checking

        #   STEP 2: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 3: Error handling
            raise Exception("An error occured in Hermione.mapSurrogate() -> Step 2: No data arg passed")

        #   STEP 4: Check if surrogate arg passed
        if ("surrogate" not in kwargs):
            #   STEP 5: Error handling
            raise Exception("An error occured in Hermion.mapSurrogate() -> Step 4: No surrogate arg passed")
        
        #
        #   endregion

        #   region STEP 6->11: Setup - Local variables

        #   STEP 6: Check if threading arg passed
        if ("threading" in kwargs):
            #   STEP 7: Check threading status
            if (kwargs["threading"] == True):
                #   STEP 8: Update - Local variables
                bThreading  = True

        #   STEP 10: Check if optimizer arg passed
        if ("optimizer" in kwargs):
            #   STEP 11: Update - Local variables
            eOptimizer = kwargs["optimizer"]

        #
        #   endregion

        #   STEP 12: Check if optimizer is GA
        if (ga.isEnum(eOptimizer)):
            #   STEP 13: Check threading status
            if (bThreading):
                #   STEP 14: User output
                if (self.bShowOutput):
                    print("Hermione (map-srg) {" + Helga.time() + "} - Starting threaded surrogate mapping")

                #   STEP 15: Outsource and return
                return self.__mapSurrogate__(surrogate=kwargs["surrogate"], data=kwargs["data"], optimizer=eOptimizer)
            
            #   STEP 16: Not threaded
            else:
                #   STEP 17: User output
                if (self.bShowOutput):
                    print("Hermione (map-srg) {" + Helga.time() + "} - Starting surrogate mapping")

                #   STEP 18: Create new mapper
                spongebob   = SpongeBob()

                #   STEP 19: Outsource and return
                return spongebob.mapSurrogate(surrogate=kwargs["surrogate"], data=kwargs["data"], optimizer=eOptimizer)

        #   STEP 20: Check if optimizer is swarm
        elif (sw.isEnum(eOptimizer)):
            #   STPE 21: Check threading status
            if (bThreading):
                #   STEP 22: User output
                if (self.bShowOutput):
                    print("Hermione (map-srg) {" + Helga.time() + "} - Starting threaded surrogate mapping")
                
                #   STEP 23: Outsource and return
                return self.__mapSurrogate__(surrogate=kwargs["surrogate"], data=kwargs["data"], optimizer=eOptimizer)

            #   STEP 24: Not threaded
            else:
                #   STEP 25: User output
                if (self.bShowOutput):
                    print("Hermione (map-srg) {" + Helga.time() + "} - Starting surrogate mapping")

                #   STEP 26: Create new swarm handler
                sarah = Sarah()

                #   STEP 27: Outsource and return
                return sarah.mapSurrogate(surrogate=kwargs["surrogate"], data=kwargs["data"], optimizer=eOptimizer)
        
        #   STEP 29: Unidentified optimizer - Error handling
        print("Initial error: ", eOptimizer)
        raise Exception("An error occured in Natalie.mapSurrogate() -> Step 29: Unidentified optimizer")
    def mapSurrogate(self, **kwargs) -> dict:
        """
            Description:

                Maps the passed surrogate using the specified optimizer.

            |\n
            |\n
            |\n
            |\n
            |\n
            
            Arguments:

                + surrogate = ( vars ) The surrogate that requires mapping
                    ~ Required

                + data  = ( vars ) A Data container that contains the data
                    for the mapping process
                    ~ Required

                + optimizer = ( enum ) The optimizer to be used during the
                    mapping process
                    ~ Required
        """

        #   STEP 0: Local variables

        #   STEP 1: Setup - Local variables

        #   region STEP 2->7: Error checking

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

        #   STEP 4: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 5: Error handling
            raise Exception(
                "An error occured in SpongeBob.mapSurrogate() -> Step 4: No data arg passed"
            )

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

        #
        #   endregion

        #   STEP 8: Check if TRO
        if (kwargs["optimizer"] == ga.TRO):
            #   STEP 10: User output
            if (self.bShowOutput):
                print("SpongeBob (map-srg) -> (map-srg-TRO) {" + Helga.time() +
                      "}")

            #   STEP 11: Outsource to tro and return
            return self.__troMapping__(surrogate=kwargs["surrogate"],
                                       data=kwargs["data"])

        #   STEP 12: Unrecognized optimizer - Error handling
        raise Exception(
            "An error occured in SpongeBob.mapSurrogate() -> Step 12: Unrecognized optimizer"
        )
    def __importGeometry(self) -> None:
        """
        """

        #   STEP 0: Local variables
        cfTmp = Conny()
        sPath = None

        lOut = []

        #   STEP 1: Setup - Local variables
        #   STEP 2: User Output
        print("Irene (IG) {" + Helga.time() +
              "} - Please specify the antenna geometry .json file to use.")

        #   STEP 3: User Input
        while (True):
            #   STEP 4: Wait for input
            ui = input("\t>")
            os.system("cls")

            #   STEP ..: Check for import cancel
            if (ui == "cancel"):
                print("Irene (Main) {" + Helga.time() +
                      "} - How would you like to continue?")
                return

            #   STEP ..: Check for help requres
            if (ui == "help"):
                self.__helpIG()

            else:

                #   STEP 5: Check if input contains \\ or /
                if (("\\" in ui) or ("/" in ui)):
                    #   STEP 6: Assume full path - check existence
                    if (os.path.exists(ui)):
                        sPath = ui
                        break

                    else:
                        print("Irene (NG) {" + Helga.time() +
                              "} - Invalid Input")

                else:
                    #   STEP 7: Check in ConfigFiles
                    sTmpPath = os.path.abspath(
                        ".") + "\\Data\\ConfigFiles\\" + ui

                    if (os.path.exists(sTmpPath)):
                        sPath = sTmpPath
                        break

                    #   STEP 8: Check Exports
                    sTmpPath = os.path.abspath(
                        ".") + "\\Data\\Exports\\Antennas\\" + ui

                    if (os.path.exists(sTmpPath)):
                        sPath = sTmpPath
                        break

                    #   STEP 9: User Output
                    print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

        #   STEP 10: Load the json file
        cfTmp.load(sPath)

        #   STEP 11: Append the data
        lOut.append(cfTmp.data["frequency"]["lower"])
        lOut.append(cfTmp.data["frequency"]["upper"])
        lOut.append(cfTmp.data["params"]["length"])
        lOut.append(cfTmp.data["params"]["width"])
        lOut.append(cfTmp.data["params"]["substrate height"])
        lOut.append(cfTmp.data["params"]["permitivitty"])

        #   STEP 12: Optimize using parameters
        self.__optimize(lOut, parent="import")

        #   STEP 13: User Output
        print("Irene (Main) {" + Helga.time() +
              "} - How would you like to continue?")

        #   STEP 14: Return
        return
                for j in range(0, len(lCandidate[i])):
                    #   STEP 10: Check if value over upper limit
                    if (lCandidate[i][j] > 1.0):
                        #   STEP 11: Limit value
                        lCandidate[i][j] = 1.0

                    #   STEP 12: Check if value below lower limit
                    elif (lCandidate[i][j] < -1.0):
                        #   STEP 13: Limit value
                        lCandidate[i][j] = -1.0

        #   STEP 14: Return
        return lCandidate

    #
    #   endregion

    #
    #endregion


#
#endregion

#region Testing

if (__name__ == "__main__"):
    Helga.nop()

#
#   endregion
    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 __psoTraining__(self, **kwargs) -> dict:
        """
            Description:

                Trains the passed surrogate using Particle-Swarm Optimization.

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

            Args:
            
                + surrogate     = ( vars ) The surrogate instance to be trained
                    ~ Required
                + data          = ( vars ) Data container
                    ~ Required
                + password      = ( int/float ) The surrogate password
                    ~ Required
            
            |\n

            Returns:

                + dictionary        = ( dict ) A dict instance containing
                    ~ surrogate     = ( vars ) The trained surrogate
                    ~ iterations    = ( int ) The training iterations
                    ~ scalar        = ( float ) The surrogate intstance's scalar
        """

        #   STEP 0: Local variables

        surrogate               = None
        password                = None

        dPsoParams              = None

        swarm                   = None

        dTrainingData           = None
        dTestingData            = None

        lCandidates             = []
        lFitness                = []

        #   region STEP 1->6: Error checking

        #   STEP 1: Check if surrogate arg passed
        if ("surrogate" not in kwargs):
            #   STEP 2: Error handling
            raise Exception("An error occured in Sarah.__psoTraining__() -> Step 1: No surorgate arg passed")

        #   STEP 3: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 4: Error handling
            raise Exception("An error occured in Sarah.__psoTraining__() -> Stpe 3: No data arg passed")

        #   STEP 5: Check if password arg passed
        if ("password" not in kwargs):
            #   STEP 6: Error handling
            raise Exception("An error occured in Sarah.__psoTraining__() -> Step 5: No password arg passed")

        #
        #   endregion

        #   region STEP 7->17: Setup - Local variables

        #   STEP 7: Init algorithm parameters
        dPsoParams      = self.__getParams__(optimizer=sw.PSO)

        #   STEP 8: Init datasets
        dTestingData    = kwargs["data"].splitData()

        dTrainingData   = dTestingData["training"]
        dTestingData    = dTestingData["testing"]

        #   STEP 9: Set surrogate and password variables
        surrogate       = kwargs["surrogate"]
        password        = kwargs["password"]

        #   STEP 10: Init candidate list
        lCandidates     = self.__getCandidates__(optimizer=sw.PSO, params=dPsoParams, initial=surrogate.getWeights(password=password))

        #   STEP 11: Init fitness list
        lFitness        = self.__getFitness__(type="surrogate", candidates=lCandidates, surrogate=surrogate, data=dTestingData, password=password) 

        #   STEP 12: Init swarm
        swarm           = SwarmChan(dPsoParams["candidates"])

        swarm.initPsoPositions( lCandidates )
        swarm.initPsoFitness( lFitness )
        swarm.initPsoParams( dPsoParams["phi1"], dPsoParams["phi2"], dPsoParams["eta"] )
        
        #   STEP 13: Get rand
        fTmp            = rn.uniform(0.0, 1.0)

        #   STEP 14: Check if L1
        if (fTmp < 0.65):
            #   STEP 15: Set - L1
            surrogate.bUse_L1  = True

        #   STEP 16: Check if L2
        elif  (fTmp < 0.85):
            #   STPE 17: Set - L2
            surrogate.bUse_L2   = True

        #
        #   endregion

        #   STEP 18: User Output
        if (self.bShowOutput):
            print("Sarah (train-srg-pso) {" + Helga.time() + "} - Starting Particle-Swarm Optimization\n")

        #   STEP 19: Perform number of iterations
        for i in range(0, dPsoParams["iterations"] + 1):
            #   STEP 20: Reset variables
            lFitness    = []
            lCandidates = []

            #   region STEP 5->14: Training process

            #   STEP 5: Perform swarming
            swarm.pso()

            #   STEP 6: Iterate through all particles
            for j in range(0, dPsoParams["candidates"]):
                #   STEP 7: Append particle to list
                swarm.lParticles[j].lCurrPosition = self.__limit_candidate_to_trust_region__(candidate=swarm.lParticles[j].lCurrPosition)
                lCandidates.append(swarm.lParticles[j].lCurrPosition)

            #   STEP 8: Get updated fitness values
            lFitness = self.__getFitness__(type="surrogate", candidates=lCandidates, surrogate=surrogate, data=dTestingData, password=password)

            #   STEP 9: Set new particle fitness
            swarm.setParticleFitness(lFitness)

            #   STEP 10: Set surrogate weights to best candidate
            surrogate.setWeights(weights=swarm.lBestSolution, password=password)

            #   STEP 11: Perform default training
            for j in range(0, dPsoParams["iterations-def"]):
                #   STEP 12: Get random data sample
                dDNR = dTrainingData.getRandDNR(noise=True)

                #   STEP 13: Perform propagation
                surrogate.propagateForward(data=dDNR["in"], password=password)
                surrogate.propagateBackward(data=dDNR["out"], password=password)

            #   STEP 14: Update best candidate
            swarm.lBestSolution = surrogate.getWeights(password=password)

            #
            #   endregion

        #   STEP 24: Get accuracy as percentage
        dHold   = surrogate.getAccuracy(data=kwargs["data"], size=kwargs["data"].getLen(), full_set=True)
        iAcc    = dHold["accurate samples"]
        fAcc    = dHold["percent accuracy"]

        #   STEP 23: User Output
        if (self.bShowOutput):
            #   STEP 25: Print output
            print("\tSarah (train-srg-pso) {" + Helga.time() + "} - Particle-Swarm Optimization Unsuccessful")
            print("\t\tTotal iterations: " + str(i))
            print("\t\tAccurate Samples: " + str(iAcc))
            print("\t\tPercent Accuracy: " + str(round(fAcc * 100.0, 2)) + "%\n")
            
        #   STEP 26: Populate output dictionary
        dOut = {
            "accuracy":     iAcc,
            "algorithm":    "pso",
            "iterations":   -i,
            "scalar":       dPsoParams["scalar"],
            "surrogate":    surrogate
        }

        #   STEP 27: Check that iAcc > 0
        if (iAcc <= 0):
            dOut["inverse accuracy"] = np.inf

        else:
            dOut["inverse accuracy"] = float(dHold["iterations"] / iAcc)

        #   STEP 28: Return
        return dOut
    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")
    def __newGeometry(self) -> None:
        """
        """

        #   STEP 0: Local variables
        lParams = None
        fParent = None

        #   STEP 1: Setup - Local variables
        #   STEP 2: User Output
        print(
            "Irene (NG) {" + Helga.time() +
            "} - Would you like to specify the frequency range or the parameters of the new antenna?"
        )

        #   STEP 3: User Input
        while (True):
            print("\t0: Frequency Range")
            print("\t1: Parameters")
            print("\t2: ~ Help")
            print("\t3: ~ Exit")
            print("")

            #   STEP 4: Wait for user input
            ui = input("\t>")
            os.system("cls")

            #   STEP 5: Verify input
            try:
                #   STEP 6: Cast to int
                ui = int(ui)

                #   STEP 7: Verify range
                if (ui == 0):
                    #   STEP 8: Get frequency range
                    lParams = self.__getFreq()
                    fParent = "frequency"

                    break

                elif (ui == 1):
                    #   STEP 9: Get geometry parameters
                    lParams = self.__getParams()
                    fParent = "params"

                    break

                elif (ui == 2):
                    self.__helpNG()

                elif (ui == 3):
                    print("Irene (Main) {" + Helga.time() +
                          "} - How would you like to continue?")
                    return

                else:
                    print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

            except Exception as ex:
                print(ex)
                print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

        #   STEP 10: Energize
        self.__optimize(lParams, parent=fParent)

        #   STEP 10: User Output
        print("Irene (Main) {" + Helga.time() +
              "} - How would you like to continue?")

        #   STEP 11: Return
        return
Example #20
0
    def __map_srgOverseer__(self, **kwargs) -> None:
        """
            Description:

                Maps the fittest surrogate as well as all the other surrogates
                whose fitness were within the required range
            
            |\n
            |\n
            |\n
            |\n
            |\n

            Arguments:

                + data  = ( vars ) A Data container containing the dataset to
                    be used during the mapping process
                    ~ Required
        """

        #   STEP 0: Local variables
        optimizer               = Hermione()
        optimizer.bShowOutput   = False

        self.vBest              = None
        self.vFitness           = np.inf

        fTmp_Fitness            = np.inf

        #   STEP 1: Setup - Local variables
        self.lMap_Results     = []
        self.lMap_Fitness     = []
        
        #   STEP 2: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 3: Error handling
            raise Exception("An error occured in Golem.__map_srgOverseer__() -> Step 2: No data arg passed")

        #   STEP 5: If optimizer output
        if (optimizer.bShowOutput):
            print("")

        else:
            print("\t{" + Helga.time() + "} - Starting surrogate mapping\t\t\t-> ", end="")

        try:

            #   STEP 6: Loop through surrogates
            for i in range(0, len(self.__lSRG)):
                #   STEP 7: Setup - Scope varibales
                dTmp_MapResults     = None

                #   STEP 8: Best candidate
                if (i == 0):
                    #   STEP 9: Outsource threaded mapping
                    dTmp_MapResults = optimizer.mapSurrogate(threading=False, data=kwargs["data"], surrogate=self.__lSRG[i])

                #   STEP 10: Else if accuracy = 100%
                elif ((self.__lSRG_Accuracy[i] == 1.0) or (self.__lSRG_FItness[i] == self.__lSRG_FItness[0])):
                    #   STEP 11: Outsource mapping
                    dTmp_MapResults = optimizer.mapSurrogate(threading=False, data=kwargs["data"], surrogate=self.__lSRG[i])

                #   STPE 12: CHeck if there are results
                if (dTmp_MapResults != None):
                    #   STEP 13: Append to results
                    self.lMap_Results.append(dTmp_MapResults["result"])
                    self.lMap_Fitness.append(dTmp_MapResults["fitness"])

                    #   STEP 14: Check - Optimizer output status
                    if (optimizer.bShowOutput == False):
                        #   STEP 15: Check if new results are best
                        if (dTmp_MapResults["fitness"] < fTmp_Fitness):
                            #   STEP 16: Update - Local variables
                            fTmp_Fitness    = dTmp_MapResults["fitness"]

                            #   STEP 17: User output
                            print("!", end="")

                        #   STEP 18: Not new best
                        else:
                            #   STEP 19: User output
                            print(".", end="")

            #   STEP 14: Setup - Best results
            self.vBest      = self.lMap_Results[0]
            self.vFitness   = self.lMap_Fitness[0]

        except Exception as ex:
            print("Initial error: ", ex)
            print("An error occured in Golem.__map_srgOverseer__()")

        #   STEP 15: Return
        return
    def __getSubstrate(self) -> float:
        """
        """

        #   STEP 0: Local variables
        uiChoice = None

        #   STEP 1: Setup - Local variables
        #   STEP 2: User Output
        print(
            "Irene (NG) {" + Helga.time() +
            "} - Would you like to specify the substrate by name per permitivitty?"
        )

        #   STEP 3: User Input
        while (True):
            print("\t0: By Name")
            print("\t1: By permitivitty")
            print("")

            #   STEP 4: Wait for user input
            ui = input("\t>")
            os.system("cls")

            #   STEP 5: Validate input
            try:
                #   STEP 6: Cast to int
                ui = int(ui)

                #   STEP 7: Verify range
                if ((ui == 0) or (ui == 1)):
                    uiChoice = ui
                    break

                else:
                    print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

            except:
                print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

        #   STEP 8: If by value
        if (uiChoice == 1):
            #   STEP 9: User output
            print(
                "Irene (NG) {" + Helga.time() +
                "} - Please choose one of the following default peremitvitties"
            )

            #   STEP 10: User input
            while (True):
                #   STEP 11: Output menu
                lTmpMenu = self.__cf.data["menus"]["substrate values"]
                for i in range(0, lTmpMenu["items"]):
                    print("\t" + str(i) + ": " + str(lTmpMenu[str(i)]))

                print("")

                #   STEP 12: Wait for user input
                ui = input("\t>")
                os.system("cls")

                #   STEP 13: Verify input
                try:
                    #   STEP 14: Cast to int
                    ui = int(ui)

                    #   STEP 15: Verify range
                    if ((ui >= 0) and (ui < lTmpMenu["items"])):
                        return lTmpMenu[str(ui)]

                    else:
                        print("Irene (NG) {" + Helga.time() +
                              "} - Invalid Input")

                except:
                    print("Irene (NG) {" + Helga.time() + "} - Invalid Input")

        #   STEP 16: By name
        elif (ui == 0):
            #   STEP 16: User Output
            print("Irene (NG) {" + Helga.time() +
                  "} - Please choose on of the following default substrates")

            #   STEP 17: User input
            while (True):
                #   STEP 18: Output menu
                lTmpMenu = self.__cf.data["menus"]["substrate names"]
                for i in range(0, lTmpMenu["items"]):
                    print("\t" + str(i) + ": " + lTmpMenu[str(i)])

                print("")

                #   STEP 19: Wait for user input
                ui = input("\t>")
                os.system("cls")

                #   STEP 20: Verify input
                try:
                    #   STEP 21: Casst to int
                    ui = int(ui)

                    #   STEP 22: Verify range
                    if ((ui >= 0) and (ui < lTmpMenu["items"])):
                        return self.__cf.data["menus"]["substrate values"][str(
                            ui)]

                    else:
                        print("Irene0 (NG) {" + Helga.time() +
                              "} - Invalid Input")

                except:
                    print("Irene1 (NG) {" + Helga.time() + "} - Invalid Input")

        return 0.0
    def __psoMapping__(self, **kwargs) -> dict:
        """
            Description:

                Maps the passed surrogate using Particle-Swarm Optimization.

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

            Arguments:

                + surrogate = ( vars) The surrogate instance to be mapped
                    ~ Required

                + data  = ( vars ) A Data container that contains the data
                    for the mapping process
                    ~ Required
        """

        #   STEP 0: Local variables
        vData                   = None
        vSRG                    = None
        vSwarm                  = None

        dPSO_Params             = None

        lCandidates             = []
        lFitness                = []

        #   STEP 1: Setup - Local variables

        #   region STEP 2->5: Error checking
        
        #   STEP 2: Check if surrogate arg passed
        if ("surrogate" not in kwargs):
            #   STEP 3: Error handling
            raise Exception("An error occured in Sarah.__psoMapping__() -> Step 2: No surrogate arg passed")

        #   STEP 4: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 5: Error handling
            raise Exception("An error occured in Sarah.__psoMapping__() -> Step 4: No data arg passed")
        
        #
        #   endregion
        
        #   region STEP 6->11: Setup - Local variables

        #   STEP 6: Update - Local variables
        vData   = kwargs["data"]
        vSRG    = kwargs["surrogate"]

        #   STEP 7: Get PSO params
        dPSO_Params     = self.__getParams__(optimizer=sw.PSO)["mapping"]
        
        #   STEP 8: Get initial candidate
        iTmp_Candidate  = vData.getInputWidth()
        lTmp_Candidate  = []

        for _ in range(0, iTmp_Candidate):
            lTmp_Candidate.append(0.0)

        lCandidates     = self.__getCandidates__(optimizer=sw.PSO, params=dPSO_Params, initial=lTmp_Candidate)

        vData.reset()
        for i in range(0, vData.getLen()):
            lCandidates.append(vData.getRandDNR()["in"])

        dPSO_Params["candidates"] = len(lCandidates)

        #   STEP 9: Loop through candidates
        for i in range(0, len(lCandidates)):
            #   STPE 10: Get candidate fitness
            lFitness.append( vSRG.getPointOutput( lCandidates[i] ) )


        #   STEP 11: Setup - Swarm chan
        vSwarm  = SwarmChan(dPSO_Params["candidates"])

        vSwarm.initPsoPositions(lCandidates)
        vSwarm.initPsoFitness(lFitness)
        vSwarm.initPsoParams(dPSO_Params["phi1"], dPSO_Params["phi2"], dPSO_Params["eta"])

        #
        #   endregion
        
        #   STEP 12: User output
        if (self.bShowOutput):
            print("Sarah (map-srg-pso) {" + Helga.time() + "} - Starting Particle-Swarm Optimization mapping")

        #   STEP 13: Iterate
        for i in range(0, dPSO_Params["iterations"] + 1):
            #   STEP 14: Setup - Local variables
            lCandidates = []
            lFitness    = []

            #   STEP 15: Perform swarming
            vSwarm.pso()

            #   STEP 16: Iterate through candidates
            for j in range(0, dPSO_Params["candidates"]):
                #   STPE 17: Get particle fitness
                vSwarm.lParticles[j].lCurrPosition = self.__limit_candidate_to_trust_region__(candidate=vSwarm.lParticles[j].lCurrPosition)
                lFitness.append( vSRG.getPointOutput( vSwarm.lParticles[j].lCurrPosition ) )

            #   STEP 18: Update swarm fitness
            vSwarm.setParticleFitness(lFitness)

        #   STEP 19: User output
        if (self.bShowOutput):
            print("Sarah (map-srg-PSO) {" + Helga.time() + "} - Particle-Swarm Optimzation mapping completed")
            print("\tTotal Iterations: " + str(i))

        #   STEP 18: Populate output dictionary
        dOut    = {
            "result":       vSwarm.lBestSolution,
            "fitness":      vSwarm.fBestSolution,
            "iterations":   i
        }

        #   STEP 19: Return
        return dOut
    def __optimize(self, _lParams: list, **kwargs) -> None:
        """
        """

        #   STEP 0: Local variables
        fOffset = None
        bDefault = None

        #   STEP 1: Setup - Local variables
        #   STEP 2: Check if parent was params
        if (kwargs["parent"] == "params"):
            #   STEP 3: User Output
            print(
                "Irene (OP) {" + Helga.time() +
                "} - Would you like to specify the offset for the band edges from the center frequency?"
            )

            #   STEP 4: User Input
            while (True):
                #   STEP 5: Output menu options
                print("\t0: Yes")
                print("\t1: No")
                print("")

                #   STEP 6: Wait for user input
                ui = input("\t>")
                os.system("cls")

                #   STEP 7: Verify input
                try:
                    #   STEP 8: Cast to int
                    ui = int(ui)

                    #   STEP 9: Verify range
                    if (ui == 0):
                        #   STEP 10: User Output
                        print("Irene (OP) {" + Helga.time() +
                              "} - Please specify the offset in Hz")

                        #   STEP 11: User Input
                        while (True):
                            #   STEP 11: Wait for user input
                            ui = input("\t>")
                            os.system("cls")

                            #   STEP 12: Verify input
                            try:
                                #   STEP 13: Cast to float
                                ui = float(ui)

                                fOffset = ui
                                break

                            except:
                                print("Irene (OP) {" + Helga.time() +
                                      "} - Invalid Input")

                        break

                    if (ui == 1):
                        fOffset = 100.0
                        break

                    else:
                        print("Irene (OP) {" + Helga.time() +
                              "} - Invalid Input")

                except:
                    print("Irene (OP) {" + Helga.time() + "} - Invalid Input")

        #   STEP 2: User Output
        print(
            "Irene (OP) {" + Helga.time() +
            "} - Would you like to use default configurations for this project?"
        )

        #   STEP 3: Get some more user input
        while (True):
            print("\t0: Use Default Configurations")
            print("\t1: Don't Use Default Configurations")
            print("")

            #   STEP 4: Wait for user input
            ui = input("\t>")
            os.system("cls")

            #   STEP 5: Verify input
            try:
                #   STEP 6: Cast to int
                ui = int(ui)

                #   STEP 7: Verify range
                if (ui == 0):
                    bDefault = True
                    break

                elif (ui == 1):
                    bDefault = False
                    break

                else:
                    print("Irene (OP) {" + Helga.time() + "} - Invalid Input")

            except:
                print("Irene (OP) {" + Helga.time() + "} - Invalid Input")

        #   STEP 8: Init Natalie
        nat = Natalie(_lParams,
                      bDefault,
                      parent=kwargs["parent"],
                      offset=fOffset)

        #   STEP 9: User Output
        print("Irene (OP) {" + Helga.time() +
              "} - How would you like to continue?")

        #   STPE 10: User Input
        while (True):
            #   STEP 11: Output menu options
            print("\t0: Start Optimization Process")
            print("\t1: ~ Edit Nat config")
            print("\t2: ~ Help")
            print("\t3: ~ Exit")
            print("")

            #   STEP 12: Wait for user input
            ui = input("\t>")
            os.system("cls")

            #   STEP 13: Verify input
            try:
                #   STEP 14: Cast to int
                ui = int(ui)

                #   STEP 15: Verify range
                if (ui == 0):
                    print(
                        "Irene (OP) {" + Helga.time() +
                        "} - Starting Optimization Process, this could take a while."
                    )
                    break

                elif (ui == 1):
                    nat.configEditor()
                    print("Irene (OP) {" + Helga.time() +
                          "} - How would you like to continue?")

                elif (ui == 2):
                    self.__helpOP()

                elif (ui == 3):
                    return

                else:
                    print("Irene (OP) + {" + Helga.time() +
                          "} - Invalid Input")

            except:
                print("Irene (OP) + {" + Helga.time() + "} - Invalid Input")

        nat.optimizeAntenna()

        return
    def trainSurrogate(self, **kwargs) -> dict:
        """
            Description:

                Trains the passed surrogate using the specified optimizer.

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

            Args:

                + surrogate   = ( vars ) The surrogate instance to be trained
                    ~ Required

                + data        = ( vars ) Data container
                    ~ Required

                + password    = ( int ) The surrogate password
                    ~ Required

                + optimizer   = ( enum ) The optimizer to user during training
                    ~ Required

            |\n

            Returns:

                + dictionary    = ( dict )
                    ~ iterations    = ( int ) Number of training iterations
                    ~ surrogate     = ( vars ) The trained surrogate
                    ~ scalar        = ( float ) The surrogate scalar
        """

        #   STEP 0: Local variables
        #   STEP 1: Setup - Local variables

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

        #   STEP 4: Check if data container passed
        if ("data" not in kwargs):
            #   STEP 5: Error handling
            raise Exception(
                "An error occured in SpongeBob.trainSurrogate() -> Step 4: No data container passed"
            )

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

        #   STEP 8: Check if optimizer passed
        if ("optimizer" not in kwargs):
            #   STEP 9: Error handlign
            raise Exception(
                "An error occured in SpongeBob.trainSurrogate() -> Step 8: No optimizer passed"
            )

        #   STEP 10: Check if tro
        if (kwargs["optimizer"] == ga.TRO):
            #   STEP 11: User Output
            if (self.bShowOutput):
                print("SpongeBob (train-srg) -> (train-srg-tro) {" +
                      Helga.time() + "}")

            #   STEP 12: Outsource tro optimization and return
            return self.__troTraining__(surrogate=kwargs["surrogate"],
                                        data=kwargs["data"],
                                        password=kwargs["password"])

        else:
            #   STEP ??: Error handling
            raise Exception(
                "An error occured in SpongeBob.trainSurrogate(): Unimplemented optimizer passed"
            )

        #   STEP ??: Return
        return None
    def main(self, _iIterations: int, _iWaitPeriod):
        """
        """
        #   STEP -1: Global variables
        global teUInputEvent
        global tTest

        #   STEP 0: Local variables
        sFileName = Helga.ticks()
        lData = []
        iCount = 0

        #   STEP 1: Setup - Global variables
        tTest = thread.Thread(target=self.__userInput)
        tTest.daemon = True
        tTest.start()

        #   STEP ..: Setup - local variables

        #   STEP 2: We out here looping
        while (True):
            #   STEP 3: Perform the result acquisition
            print("\tDAVID - Gathering data (" + str(iCount + 1) + " / " +
                  str(_iIterations) + ")")
            lData = self.__theTHING(lData, sFileName)
            iCount = iCount + 1

            #   STEP 4: Check for user input
            if (teUInputEvent.isSet() == True):
                #   STEP 4.1: Get global varialbes
                global sUserInput

                #   STEP 4.2: Check if input was to stop
                if (sUserInput == "stop"):
                    #   STEP 4.2.1: Clear variables and end loop
                    sUserInput = ""
                    teUInputEvent.clear()

                    break

                else:
                    #   STEP 4.2.2: Clear variables and restart thread (no additional commands atm)
                    sUserInput = ""
                    teUInputEvent.clear()

                    tTest.run()

            #   STEP 5: Check if iterations have been reached

            if ((_iIterations > 0) and (iCount >= _iIterations)):
                #   STEP 5.1: iteration condition achieved
                break

            #   STEP 6: Wait the set amount of time
            if ((_iWaitPeriod > 0) and (_iWaitPeriod <= 10)):
                t.sleep(_iWaitPeriod)

        #   STEP 7: Average data
        #lData = self.__averageData(lData, iCount)

        #   STEP 8: Write the data to file and ???
        self.__saveData(lData, sFileName, iCount)

        #   STEP 9: GTFO
        return
    def __troTraining__(self, **kwargs) -> dict:
        """
            Description:

                Trains the passed surrogate using Trust-Region Optimization.

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

            Args:
            
                + surrogate = ( vars ) The surrogate instance to be trained
                    ~ Required

                + data      = ( vars ) Data container
                    ~ Required

                + password  = ( int / float ) The surrogate instance's password
                    ~ Required

            |\n

            Returns:

                + dictionary        = ( dict ) A dict instance containing
                    ~ surrogate     = ( vars ) The trained surrogate
                    ~ iterations    = ( int ) The training iterations
                    ~ scalar        = ( float ) The surrogate intstance's scalar

        """

        #   STEP 0: Local variables
        surrogate = None
        password = None

        dTroParams = None

        garry = None

        dTestingData = None

        lCandidates = []
        lFitness = []

        #   region STEP 1->6: Error checking

        #   STEP 1: Check if surrogate arg passed
        if ("surrogate" not in kwargs):
            #   STEP 2: Error handling
            raise Exception(
                "An error occured in SpongeBob.__troTraining__() -> Step 1: NO surrogate arg passed"
            )

        #   STEP 3: Check if data arg passed
        if ("data" not in kwargs):
            #   STEP 4: Error handling
            raise Exception(
                "An error occured in SpongeBob.__troTraining__() -> Step 3: No data arg passed"
            )

        #   STEP 5: Check if password arg passed
        if ("password" not in kwargs):
            #   STEP 6: Error handling
            raise Exception(
                "An error occured in SpongeBob.__troTraining__() -> Step 5: No password arg passed"
            )

        #
        #   endregion

        #   region STEP 7->13: Setup - Local variables

        #   STEP 7: Init algorithm parameters
        dTroParams = self.__getParams__(optimizer=ga.TRO)

        #   STEP 8: Init datasets
        dTestingData = kwargs["data"].splitData()

        dData_Train = dTestingData["training"]
        dData_Test = dTestingData["testing"]

        #   STEP 9: Init surrogate and password variables
        surrogate = kwargs["surrogate"]
        password = kwargs["password"]

        #   STEP 10: Init surrogate activation funcitons

        #   STEP 11: Init candidate list
        lCandidates.append(surrogate.getWeights(password=password))

        #   STEP 12: Init fitness list
        lFitness = self.__getFitness__(type="surrogate",
                                       candidates=lCandidates,
                                       surrogate=surrogate,
                                       data=dData_Test,
                                       password=password)

        #   STEP 13: Init genetic algorithm
        garry = Garry(dTroParams["candidates"])

        garry.initTroParticles(candidates=lCandidates)
        garry.initTroFitness(fitness=lFitness)
        garry.initTroParams(region=dTroParams["region"])

        #   STPE 14: Check if L1
        fTmp = rn.uniform(0.0, 1.0)

        #   STEP 15: Check if L1
        if (fTmp < 0.65):
            #   STEP 16: Set - L1
            surrogate.bUse_L1 = True

        #   STEP 17: Check if L2
        elif (fTmp < 0.85):
            #   STEP 18: Set - L2
            surrogate.bUse_L2 = True

        #
        #   endregion

        #   STEP 14: User Output
        if (self.bShowOutput):
            print("SpongeBob (train-srg-tro) {" + Helga.time() +
                  "} - Starting Trust-Region Optimization\n")

        #   STEP 15: Perform specified number of iterations
        for i in range(0, dTroParams["iterations"] + 1):
            #   STEP 16: Clear necesarry variables
            lCandidates = []
            lFitness = []

            #   STEP 5: Populate candidate list
            lCandidates = self.__getCandidates__(
                optimizer=ga.TRO,
                params=dTroParams,
                initial=garry.lTroBest[0].lCurrPosition,
                region=float(garry.iTroRegion / dTroParams["region"]))

            #   STEP 6: Get candidate list fitness
            for j in range(0, len(lCandidates)):
                #   STEP 7: Set surrogate weights
                surrogate.setWeights(weights=lCandidates[j], password=password)

                #   STEP 8: Append candidate fitness
                lFitness.append(surrogate.getAFitness(data=dData_Test))

            #   STEP 9: Update garry
            garry.setPopulation(candidates=lCandidates)
            garry.setFitness(fitness=lFitness)

            #   STEP 10: Set surrogate weight to best candidate
            surrogate.setWeights(weights=garry.vBestSolution.lCurrPosition,
                                 password=password)

            #   STEP 11: Perform default training
            for j in range(0, dTroParams["iterations-def"]):
                #   STEP 12: Get random data sample
                dDNR = dData_Train.getRandDNR(noise=True)

                #   STEP 13: Perform propagation
                surrogate.propagateForward(data=dDNR["in"], password=password)
                surrogate.propagateBackward(data=dDNR["out"],
                                            password=password)

            #   STEP 14: Update garry
            garry.vBestSolution.lCurrPosition = surrogate.getWeights(
                password=password)
            garry.fBestSolution = surrogate.getAFitness(data=dData_Test)

            #   STEP 15: Perform trust-region optimization
            garry.tro()

            #   STEP 16: Check if region is still okay
            if (garry.iTroRegion <= 1):
                #   STEP 17: Exit loop
                break

        #   STEP 26: Get accuracy as percentage
        dHold = surrogate.getAccuracy(data=kwargs["data"],
                                      size=kwargs["data"].getLen(),
                                      full_set=True)
        iAcc = dHold["accurate samples"]
        fAcc = dHold["percent accuracy"]

        #   STEP 27: User Output
        if (self.bShowOutput):
            #   STEP 28: Print output
            if (fAcc >= dTroParams["requirement"]):
                print("SpongeBob (train-srg-tro) {" + Helga.time() +
                      "} - Trust-Region Optimization successful")
                print("\tTotal Iterations: " + str(i))
                print("\tAccurate Samples: " + str(iAcc))
                print("\tPercent Accuracy: " + str(round(fAcc * 100.0, 2)) +
                      "%\n")

            else:
                print("\tSpongeBob (train-srg-tro) {" + Helga.time() +
                      "} - Trust-Region Optimization Unsuccessful")
                print("\t\tTotal iterations: " + str(i))
                print("\t\tAccurate Samples: " + str(iAcc))
                print("\t\tPercent Accuracy: " + str(round(fAcc * 100.0, 2)) +
                      "%\n")

        #   STEP 29: Populate output dictionary
        dOut = {
            "accuracy": iAcc,
            "algorithm": "tro",
            "iterations": -i,
            "scalar": dTroParams["scalar"],
            "surrogate": surrogate
        }

        #   STEP 31: Check that iAcc > 0
        if (iAcc <= 0):
            dOut["inverse accuracy"] = np.inf

        else:
            dOut["inverse accuracy"] = float(dHold["iterations"] / iAcc)

        #   STEP 30: Return
        return dOut
Example #27
0
    def __threadTrain__(self, _eExit, _eTr, _qTr, _lUO) -> None:
        """
            Description:
            
                This fucntion outsources the training of the surrogate to the appropriate
                optimization handler after finding the optimizer to use.

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

            Parameters:

                + _eGlobal_Exit  = ( mp.Event() ) Event signalling global exit
                    for threads and processes

                + _eTr      = ( mp.Event() ) Event signalling process
                    completion

                + _qTr      = ( mp.Queue() ) The queue onto which the process
                    results should be returned

                + _lUO      = ( mp.RLock() ) The lock for commong user output

            |\n

            Returns:

                + dict        = ( dict )
                    ~ surrogate   = ( vars ) The trained surrogate
                    ~ fitness     = ( float ) The overall fitness of the trained surrogate
        """

        #   STEP 0: Local variables
        dArgs                   = _qTr.get()[0]
        dResults                = None

        iThread_ID              = Helga.ticks()
        iThread_AppID           = dArgs["thread"]

        iSwarms_Active          = 0
        iGA_Active              = 0

        iOptimizers_Active      = 0

        #   region STEP 1->15: Train using provided optimizer

        #   STEP 1: Check if not random optimizer
        if (rn.uniform(0.0, 1.0) > 0.3):
            #   STEP 2: Check if optimizer is GA
            if (ga.isEnum(dArgs["optimizer"])):
                #   STEP 3: User output
                if (self.bShowOutput):
                    #   STEP 4: Get lock
                    _lUO.acquire()

                    #   STEP 5: Print output
                    print("\t- Assigning SpongeBob to training")
                    print("\t- Optimizer: " + str(dArgs["optimizer"]))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 6: Release lock
                    _lUO.release()

                #   STEP 7: Create new optimizer
                sb = SpongeBob()

                #   STEP 8: Outsoruce training
                dResults = sb.trainSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], password=dArgs["password"], optimizer=dArgs["optimizer"])

            #   STEP 9: Check if swarm
            elif (sw.isEnum( dArgs["optimizer"] )):
                #   STEP 10: User Output
                if (self.bShowOutput):
                    #   STEP 11: Get lock
                    _lUO.acquire()

                    #   STEP 12: Print strings
                    print("\t- Assigning Sarah to training")
                    print("\t- Optimizer: " + str(dArgs["optimizer"]))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 13: Release lock
                    _lUO.release()

                #   STEP 14: Create new optimizer
                sarah = Sarah()

                #   STEP 15: Outsource training
                dResults = sarah.trainSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], password=dArgs["password"], optimizer=dArgs["optimizer"])

        #
        #   endregion

        #   region STEP 16->34: Random training

        #   STEP 16: Use random
        else:
            #   STEP 17: Update - Local variables
            iSwarms_Active      = sw.getNumActiveSwarms()
            iGA_Active          = ga.getNumActiveGAs()

            iOptimizers_Active  = iSwarms_Active + iGA_Active

            #   STEP 18: Random a handler
            iTmp_Optimizer      = rn.randint(0, iOptimizers_Active - 1)

            #   STEP 19: if swarm
            if (iTmp_Optimizer < iSwarms_Active):
                #   STEP 20: Get new swarm enum
                eTmp_Optimzier  = sw.getActiveSwarms()[iTmp_Optimizer]

                #   STEP 21: User Output
                if (self.bShowOutput):
                    #   STEP 22: Get lock
                    _lUO.acquire()

                    #   STEP 23: Print output
                    print("\t- Assigning Sarah to training")
                    print("\t- Optimizer: " + str(eTmp_Optimzier))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 24: Release lock
                    _lUO.release()

                #   STEP 25: Create new optimizer
                sarah       = Sarah()

                #   STEP 26: Outsource training
                dResults    = sarah.trainSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], password=dArgs["password"], optimizer=eTmp_Optimzier)

            #   STEP 27: Then ga
            else:
                #   STEP 28: Get new ga enum
                eTmp_Optimizer = ga.getActiveGAs()[iTmp_Optimizer - iSwarms_Active]

                #   STEP 29: User Output
                if (self.bShowOutput):
                    #   STEP 30: Acquire lock
                    _lUO.acquire()

                    #   STEP 31: Print output
                    print("\t- Assigning SpongeBob to training")
                    print("\t- Optimizer: " + str(eTmp_Optimizer))
                    print("\t- Thread ID: " + str(iThread_ID))
                    print("\t- Application Thread ID: " + str(iThread_AppID))
                    print("\t- Time: " + Helga.time() + "\n")

                    #   STEP 32: Release lock
                    _lUO.release()

                #   STEP 33: Create new optimizer
                sb          = SpongeBob()

                #   STEP 34: Outsource training
                dResults    = sb.trainSurrogate(surrogate=dArgs["surrogate"], data=dArgs["data"], password=dArgs["password"], optimizer=eTmp_Optimizer)
        
        #
        #   endregion

        #   STEP 35: Get surrogate fitness
        fTmpFitness = dResults["surrogate"].getAFitness(data=dArgs["data"])
        fTmpFitness = fTmpFitness * dResults["inverse accuracy"]
        
        #   STEP 36: User Output
        if (self.bShowOutput):
            #   STEP 37: Get lock
            _lUO.acquire()

            #   STEP 38: Print output
            print("\t\t\t\t\t- Thread: " + str(iThread_AppID) +  " - <" + str(dResults["accuracy"]) + "  :  " + str(round(fTmpFitness, 2)) + ">")
            print("\t\t\t\t\t- Time: " + Helga.time() + "\n")

            #   STEP 39: release lock
            _lUO.release()

        #   STEP 40: Populate output dictionary
        dOut = {
            "accuracy":     dResults["accuracy"],
            "algorithm":    dResults["algorithm"],
            "fitness":      fTmpFitness,
            "iterations":   dResults["iterations"],
            "inverse accuracy": dResults["inverse accuracy"],
            "scalar":       dResults["scalar"],
            "surrogate":    dResults["surrogate"]
        }

        #   STEP 41: Set training results
        _qTr.put([dOut])

        #   STEP 42: Set training finished result
        _eTr.set()

        #   STEP 43: Return
        return

    #
    #   endregion

    #
    #endregion

#
#endregion

#region Testing

#
#endregion
    def __troMapping__(self, **kwargs) -> dict:
        """
            Description:

                Maps the passed surrogate using Trust-Region Optimization.

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

            Arguments:

                + surrogate = ( vars ) The surrogate instance to be mapped
                    ~ Required

                + data  = ( vars ) A Data container that contains the dataset
                    to be used during the mapping process
        """

        #   STEP 0: Local variables
        vData = None
        vGarry = None
        vSRG = None

        dTRO_Params = None

        lCandidates = []
        lFitness = []

        #   STEP 1: Setup - Local variables

        #   region STEP 2->5: Error checking

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

        #   STEP 4: CHeck if data arg passed
        if ("data" not in kwargs):
            #   STEP 5: Error handling
            raise Exception(
                "An error occured in SpongeBob.__troMapping__() -> Step 4: No data arg passed"
            )

        #
        #   endregion

        #   region STEP 6->10: Setup - Local variabls

        #   STEP 6: Update - Local variables
        vData = kwargs["data"]
        vSRG = kwargs["surrogate"]

        #   STEP 7: Get initial candidate
        iTmp_Candidate = vData.getInputWidth()
        lTmp_Candidate = []

        for _ in range(0, iTmp_Candidate):
            lTmp_Candidate.append(0.0)

        lCandidates.append(lTmp_Candidate)

        #   STEP 8: Get initial fitness
        lFitness.append(vSRG.getPointOutput(lTmp_Candidate))

        #   STEP 9: Get TRO params
        dTRO_Params = self.__getParams__(optimizer=ga.TRO)["mapping"]

        #   STEP 10: Setup - Garry
        vGarry = Garry(dTRO_Params["candidates"])

        vGarry.initTroParticles(candidates=lCandidates)
        vGarry.initTroFitness(fitness=lFitness)
        vGarry.initTroParams(region=dTRO_Params["region"])

        #
        #   endregion

        #   STEP 11: User output
        if (self.bShowOutput):
            print("SpongeBob (map-srg-TRO) {" + Helga.time() +
                  "} - Starting Trust-Region Optimization mapping")

        #   STEP 12: Loop for max iterations
        for i in range(0, dTRO_Params["iterations"] + 1):
            #   STEP 13: Clear required variables
            lFitness = []

            #   STEP 14: Populate candidate list
            lCandidates = self.__getCandidates__(
                optimizer=ga.TRO,
                params=dTRO_Params,
                initial=vGarry.lTroBest[0].lCurrPosition,
                region=float(vGarry.iTroRegion / dTRO_Params["region"]))

            #   STEP 15: Loop through candidates
            for j in range(0, len(lCandidates)):
                #   STEP 16: Get candidate fitness
                lFitness.append(vSRG.getPointOutput(lCandidates[j]))

            #   STEP 17: Update garry
            vGarry.setPopulation(candidates=lCandidates)
            vGarry.setFitness(fitness=lFitness)

            #   STEP 18: Perform trust-region optimization
            vGarry.tro()

            #   STEP 19: Check if region too small
            if (vGarry.iTroRegion <= 1):
                #   STEP 20: Exit loop
                break

        #   STEP 27: User output
        if (self.bShowOutput):
            print("SpongeBob (map-srg-TRO) {" + Helga.time() +
                  "} - Trust-Region Optimizaion mapping completed")
            print("\tTotal Iterations: " + str(i))

        #   STEP 28: Populate output dictionary
        dOut = {
            "result": vGarry.lTroBest[0].lCurrPosition,
            "fitness": vGarry.lTroBest[1],
            "iterations": i
        }

        #   STEP ??: Return
        return dOut
Example #29
0
    def trainSurrogate(self, **kwargs) -> vars:
        """
            Description:
            
                Trains the passed surrogate using thread techniques if required to
                do so. If an optimizer is specified, only that optimizer will be used.
                However, if no optimizer is specified a random optimizer will be used. 

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

            Args:

                + surrogate   = ( vars ) A surrogate class instance
                    ~ Required

                + data        = ( vars ) Data container
                    ~ Required

                + password    = ( int ) Surrogate pass
                    ~ Required

                + optimizer   = ( enum ) The enum of the optimizer to be used
                    ~ Default = PSO

                + threading   = ( bool ) Multi-treading flag
                    ~ Default = False

            |\n

            Returns:

                surrogate   = ( vars ) The trained surrogate

        """

        #   STEP 0: Local variables
        eOptimizer              = sw.PSO
        bThreading              = False

        #   STEP 1: Setup - Local variables

        #   region STEP 2->7: Error checking

        #   STEP 2: Check that a surrogate was passed
        if ("surrogate" not in kwargs):
            #   STEP 3: Error handling
            raise Exception("An error occured in Hermione.trainSurrogate() -> Step 2: No surrogate passed")

        #   STEP 4: Check that the data container was passed
        if ("data" not in kwargs):
            #   STEP 5: Error handling
            raise Exception("An error occured in Hermione.trainSurrogate() -> Step 4: Data container not passed")

        #   STEP 6: Check that the surrogate password was passed
        if ("password" not in kwargs):
            #   STEP 7: Error handling
            raise Exception("An error occured in Hermione.trainSurrogate() -> Step 4: Password associated with surrogate not passed")

        #
        #   endregion
        
        #   region STEP 8->12: Setup - Local variables

        #   STEP 8: Check if threading was specified
        if ("threading" in kwargs):
            #   STEP 9: Check if threading enabled
            if (kwargs["threading"] == True):
                #   STEP 10: Update - Local variables
                bThreading  = True

        #   STEP 11: Check if optimizer was specified
        if ("optimizer" in kwargs):
            #   STEP 12: Set optimizer
            eOptimizer = kwargs["optimizer"]

        #
        #   endregion

        #   STEP 13: Check if optimizer is GA
        if (ga.isEnum(eOptimizer)):
            #   STEP 14: Check - Threading status
            if (bThreading):
                #   STEP 15: User output
                if (self.bShowOutput):
                    print("Hermione (train-srg) {" + Helga.time() + "} - Starting threaded surrogate training")

                #   STEP 16: Outsource and return
                return self.__trainSurrogate__(surrogate=kwargs["surrogate"], data=kwargs["data"], password=kwargs["password"], optimizer=eOptimizer)

            #   STEP 17: Not threaded
            else:
                #   STEP 18: User output
                if (self.bShowOutput):
                    print("Hermione (train-srg) {" + Helga.time() + "} - Starting surrogate training")

                #   STEP 19: Create new optimizer
                sb      = SpongeBob()

                #   STEP 20: Outsource and return
                return sb.trainSurrogate(surrogate=kwargs["surrogate"], data=kwargs["data"], password=kwargs["password"], optimizer=eOptimizer)

        #   STEP 21: Check if optimizer is swarm
        if (sw.isEnum(eOptimizer)):
            #   STEP 22: Check - Threading status
            if (bThreading):
                #   STEP 23: User output
                if (self.bShowOutput):
                    print("Hermione (train-srg) {" + Helga.time() + "} - Starting threaded surrogate training")

                #   STEP 24: Outsouce and return
                return self.__trainSurrogate__(surrogate=kwargs["surrogate"], data=kwargs["data"], password=kwargs["password"], optimizer=eOptimizer)

            #   STEP 25: Not threaded
            else:
                #   STEP 26: User output
                if (self.bShowOutput):
                    print("Hermione (train-srg) {" + Helga.time() + "} - Starting surrogate training")

                #   STEP 27: Create new optimizer
                sarah       = Sarah()

                #   STEP 28: Outsource and return
                return sarah.trainSurrogate(surrogate=kwargs["surrogate"], data=kwargs["data"], password=kwargs["password"], optimizer=eOptimizer)

        #   STEP 29: Unidentified optimizer - Error handling
        print("Initial error: ", eOptimizer)
        raise Exception("An error occured in Natalie.trainSurrogate() -> Step 29: Unidentified optimizer")