コード例 #1
0
ファイル: onClick.py プロジェクト: freekang/hazmatrouting
 def __call__(self, event):
     clickX = event.xdata
     clickY = event.ydata
     # needed for detecting clicks inside top left box:
     ymin, ymax = plt.ylim()
     
     # if left mouse click: remove plot window and continue with optimization
     # where the closest solution to the mouse click is extracted:
     if (event.button == 1) and event.inaxes:
         if (len(self.objectiveVectors[0]) == 2):
             for i in range(len(self.objectiveVectors)):
                 if clickX-self.xtol < self.objectiveVectors[i,0] < clickX+self.xtol and  clickY-self.ytol < self.objectiveVectors[i,1] < clickY+self.ytol :
                     self.lastChosenIndex = i
                     plt.close()
         else:
             # sort objective vectors for easy selecting them on the right
             I = np.argsort(self.objectiveVectors[:,self.objectiveVectors.shape[1]-1])
             objVectorsSorted = self.objectiveVectors[I,:]
             # add (kind of) id of solution as last column like in plot:
             maximum = objVectorsSorted.max(0).max(0)
             minimum = objVectorsSorted.min(0).min(0)
             
             objVectorsSorted = np.column_stack((objVectorsSorted,
                                 np.linspace(minimum, maximum,
                                 objVectorsSorted.shape[0]).transpose()))
             
             # detect click only when on the right:
             if (clickX > objVectorsSorted.shape[1]-2 + 0.25):
                 # find correct objective vector among the sorted ones:
                 for i in range(len(objVectorsSorted)):
                     yleft = objVectorsSorted[i, objVectorsSorted.shape[1] - 2]
                     yright = objVectorsSorted[i, objVectorsSorted.shape[1] - 1]
                     
                     xrel = (clickX - (objVectorsSorted.shape[1]-2)) / 0.5
                     yrel = yleft + xrel * (yright - yleft)
                      
                     if clickY-self.ytol < yrel < clickY+self.ytol :
                         # before returning, choose corresponding index
                         # in the original sorting!
                         self.lastChosenIndex = I[i]
                     
                         plt.close()
             # or if in box on top left corner:
             if (0 < clickX < 1 and ymax - (ymax-ymin)*0.1 < clickY < ymax ):
                 self.lastChosenIndex = -1 # indicate that pref statements
                                           # should be used to build weight
                                           # function of hypervolume
                 plt.close()
                 
     # if right mouse button clicked in top left box, compute
     # partial order from preference statements and write the order
     # to the window
     if (event.button == 3) and event.inaxes:
         # detect whether click was in box on top left corner:
         if (0 < clickX < 1 and ymax - (ymax-ymin)*0.1 < clickY < ymax ):
             # sort objective vectors for easy selecting them on the right
             I = np.argsort(self.objectiveVectors[:,self.objectiveVectors.shape[1]-1])
             objVectorsSorted = self.objectiveVectors[I,:]
             cps = comparativePrefStatements(objVectorsSorted, "testPrefStatements.txt")
             poset = cps.computePartialOrderWithOptimisticSemantics()
             if (self.toggle == 1):
                 plt.text(1.1, ymax - (ymax-ymin)*0.05,
                                         self.ps,
                                         horizontalalignment='left',
                                         verticalalignment='center',
                                         color='w')
                 plt.fill([1, 1, objVectorsSorted.shape[1]-0.5, objVectorsSorted.shape[1]-0.5], [ymax, ymax - (ymax-ymin)*0.1, ymax - (ymax-ymin)*0.1, ymax], color='w', alpha=1.0)
                 plt.text(1.1, ymax - (ymax-ymin)*0.05,
                                         poset,
                                         horizontalalignment='left',
                                         verticalalignment='center',
                                         color='k')
                 self.ps = poset
                 self.toggle = 0
             else:
                 plt.text(1.1, ymax - (ymax-ymin)*0.05,
                                         self.ps,
                                         horizontalalignment='left',
                                         verticalalignment='center',
                                         color='k')
                 plt.fill([1, 1, objVectorsSorted.shape[1]-0.5, objVectorsSorted.shape[1]-0.5], [ymax, ymax - (ymax-ymin)*0.1, ymax - (ymax-ymin)*0.1, ymax], color='k', alpha=1.0)
                 plt.text(1.1, ymax - (ymax-ymin)*0.05,
                                         poset,
                                         horizontalalignment='left',
                                         verticalalignment='center',
                                         color='w')
                 self.ps = poset
                 self.toggle = 1
             plt.draw()
コード例 #2
0
    def __call__(self, event):
        clickX = event.xdata
        clickY = event.ydata
        # needed for detecting clicks inside top left box:
        ymin, ymax = plt.ylim()

        # if left mouse click: remove plot window and continue with optimization
        # where the closest solution to the mouse click is extracted:
        if (event.button == 1) and event.inaxes:
            if (len(self.objectiveVectors[0]) == 2):
                for i in range(len(self.objectiveVectors)):
                    if clickX - self.xtol < self.objectiveVectors[
                            i,
                            0] < clickX + self.xtol and clickY - self.ytol < self.objectiveVectors[
                                i, 1] < clickY + self.ytol:
                        self.lastChosenIndex = i
                        plt.close()
            else:
                # sort objective vectors for easy selecting them on the right
                I = np.argsort(
                    self.objectiveVectors[:,
                                          self.objectiveVectors.shape[1] - 1])
                objVectorsSorted = self.objectiveVectors[I, :]
                # add (kind of) id of solution as last column like in plot:
                maximum = objVectorsSorted.max(0).max(0)
                minimum = objVectorsSorted.min(0).min(0)

                objVectorsSorted = np.column_stack(
                    (objVectorsSorted,
                     np.linspace(minimum, maximum,
                                 objVectorsSorted.shape[0]).transpose()))

                # detect click only when on the right:
                if (clickX > objVectorsSorted.shape[1] - 2 + 0.25):
                    # find correct objective vector among the sorted ones:
                    for i in range(len(objVectorsSorted)):
                        yleft = objVectorsSorted[i,
                                                 objVectorsSorted.shape[1] - 2]
                        yright = objVectorsSorted[i,
                                                  objVectorsSorted.shape[1] -
                                                  1]

                        xrel = (clickX - (objVectorsSorted.shape[1] - 2)) / 0.5
                        yrel = yleft + xrel * (yright - yleft)

                        if clickY - self.ytol < yrel < clickY + self.ytol:
                            # before returning, choose corresponding index
                            # in the original sorting!
                            self.lastChosenIndex = I[i]

                            plt.close()
                # or if in box on top left corner:
                if (0 < clickX < 1
                        and ymax - (ymax - ymin) * 0.1 < clickY < ymax):
                    self.lastChosenIndex = -1  # indicate that pref statements
                    # should be used to build weight
                    # function of hypervolume
                    plt.close()

        # if right mouse button clicked in top left box, compute
        # partial order from preference statements and write the order
        # to the window
        if (event.button == 3) and event.inaxes:
            # detect whether click was in box on top left corner:
            if (0 < clickX < 1 and ymax - (ymax - ymin) * 0.1 < clickY < ymax):
                # sort objective vectors for easy selecting them on the right
                I = np.argsort(
                    self.objectiveVectors[:,
                                          self.objectiveVectors.shape[1] - 1])
                objVectorsSorted = self.objectiveVectors[I, :]
                cps = comparativePrefStatements(objVectorsSorted,
                                                "testPrefStatements.txt")
                poset = cps.computePartialOrderWithOptimisticSemantics()
                if (self.toggle == 1):
                    plt.text(1.1,
                             ymax - (ymax - ymin) * 0.05,
                             self.ps,
                             horizontalalignment='left',
                             verticalalignment='center',
                             color='w')
                    plt.fill([
                        1, 1, objVectorsSorted.shape[1] - 0.5,
                        objVectorsSorted.shape[1] - 0.5
                    ], [
                        ymax, ymax - (ymax - ymin) * 0.1, ymax -
                        (ymax - ymin) * 0.1, ymax
                    ],
                             color='w',
                             alpha=1.0)
                    plt.text(1.1,
                             ymax - (ymax - ymin) * 0.05,
                             poset,
                             horizontalalignment='left',
                             verticalalignment='center',
                             color='k')
                    self.ps = poset
                    self.toggle = 0
                else:
                    plt.text(1.1,
                             ymax - (ymax - ymin) * 0.05,
                             self.ps,
                             horizontalalignment='left',
                             verticalalignment='center',
                             color='k')
                    plt.fill([
                        1, 1, objVectorsSorted.shape[1] - 0.5,
                        objVectorsSorted.shape[1] - 0.5
                    ], [
                        ymax, ymax - (ymax - ymin) * 0.1, ymax -
                        (ymax - ymin) * 0.1, ymax
                    ],
                             color='k',
                             alpha=1.0)
                    plt.text(1.1,
                             ymax - (ymax - ymin) * 0.05,
                             poset,
                             horizontalalignment='left',
                             verticalalignment='center',
                             color='w')
                    self.ps = poset
                    self.toggle = 1
                plt.draw()
コード例 #3
0
ファイル: interaction.py プロジェクト: freekang/hazmatrouting
def do_interactionStep(pop, params, state, generation, run):
    """ performs interaction step including plotting of objective values """
    print '      plotting and interaction step'
        
    f_current = pop.getObjectiveValuesOfCurrentPop()
        
    # kmedoids clustering:
    #centroidids, clusterids = do_kmedoidsclustering(f_current, params)
    # kmeans clusterin (does not work at the moment):
    centroidids, clusterids = do_kmeansclustering(f_current, params)
    
    oc = plot(f_current, centroidids, clusterids, generation, run, params)

    
    if (params.interaction == 1):
        ## blocking
        plt.show()
    else:
        # non-blocking plotting to see population over time
        plt.ion()
        plt.draw()

    chosenSolution = oc.getLastChosenObjectiveVector()
    
    if (math.isnan(chosenSolution[0])):
        # do nothing (user probably just closed the window)
        # in this case, the current weight function is used again
        print "no new information about user's preferences, hence weight function is kept"
    else:
        newWeightStrings = []
        
        if params.interactiontype == 'box':
            # compute new weight based on box idea:
            #
            # TODO: to be implemendted
            #
            print 'not implemented yet'
        elif params.interactiontype == 'gau':
            # compute new weight function as a gaussian around the selected
            # solution:
            length = np.linalg.norm(pop.getSpreadOfCurrentPop())
            
            #
            # TODO: get #samples and seed from W-HypE's parameter file and
            # write it back
            #
            newWeightStrings.append('seed ' + str(2037115396 + generation))
            newWeightStrings.append('nrOfSamples 10000')
                
    
            if (chosenSolution[0] == -1):  # ie if prefstatements should be used
                cps = comparativePrefStatements(f_current, "testPrefStatements.txt")
                poset = cps.computePartialOrderWithOptimisticSemantics()
                
                for s in poset[0]:
                    weightString = getWeightString(f_current[s], pop, f_current, length, params)
                    newWeightStrings.append(weightString)
            
            else:
                weightString = getWeightString(chosenSolution, pop, f_current, length, params)
                newWeightStrings.append(weightString)
        
        # write new weight function to file for selector:
        io.printToFile(newWeightStrings, params.parameter_file_selector)
            
        # reset selector in order to read new weights
        
        # first reset:
        state.write(params.sta_file_selector, 10)
        while 1:
            selState = state.read(params.sta_file_selector)
            if selState != 11:
                state.write(params.sta_file_selector, 10)
            else:
                params.log('    selector reset\n')
                break
            wait(params.poll);
        # then init (since variator is still running, just write state 1
        # for selector):
        params.log('    selector reset to read new weights\n')
        state.write(params.sta_file_selector, 1)
コード例 #4
0
def do_interactionStep(pop, params, state, generation, run):
    """ performs interaction step including plotting of objective values """
    print '      plotting and interaction step'

    f_current = pop.getObjectiveValuesOfCurrentPop()

    # kmedoids clustering:
    #centroidids, clusterids = do_kmedoidsclustering(f_current, params)
    # kmeans clusterin (does not work at the moment):
    centroidids, clusterids = do_kmeansclustering(f_current, params)

    oc = plot(f_current, centroidids, clusterids, generation, run, params)

    if (params.interaction == 1):
        ## blocking
        plt.show()
    else:
        # non-blocking plotting to see population over time
        plt.ion()
        plt.draw()

    chosenSolution = oc.getLastChosenObjectiveVector()

    if (math.isnan(chosenSolution[0])):
        # do nothing (user probably just closed the window)
        # in this case, the current weight function is used again
        print "no new information about user's preferences, hence weight function is kept"
    else:
        newWeightStrings = []

        if params.interactiontype == 'box':
            # compute new weight based on box idea:
            #
            # TODO: to be implemendted
            #
            print 'not implemented yet'
        elif params.interactiontype == 'gau':
            # compute new weight function as a gaussian around the selected
            # solution:
            length = np.linalg.norm(pop.getSpreadOfCurrentPop())

            #
            # TODO: get #samples and seed from W-HypE's parameter file and
            # write it back
            #
            newWeightStrings.append('seed ' + str(2037115396 + generation))
            newWeightStrings.append('nrOfSamples 10000')

            if (chosenSolution[0] == -1
                ):  # ie if prefstatements should be used
                cps = comparativePrefStatements(f_current,
                                                "testPrefStatements.txt")
                poset = cps.computePartialOrderWithOptimisticSemantics()

                for s in poset[0]:
                    weightString = getWeightString(f_current[s], pop,
                                                   f_current, length, params)
                    newWeightStrings.append(weightString)

            else:
                weightString = getWeightString(chosenSolution, pop, f_current,
                                               length, params)
                newWeightStrings.append(weightString)

        # write new weight function to file for selector:
        io.printToFile(newWeightStrings, params.parameter_file_selector)

        # reset selector in order to read new weights

        # first reset:
        state.write(params.sta_file_selector, 10)
        while 1:
            selState = state.read(params.sta_file_selector)
            if selState != 11:
                state.write(params.sta_file_selector, 10)
            else:
                params.log('    selector reset\n')
                break
            wait(params.poll)
        # then init (since variator is still running, just write state 1
        # for selector):
        params.log('    selector reset to read new weights\n')
        state.write(params.sta_file_selector, 1)