def generateBehaviour():
    print("Generate Behaviours")

    np.random.seed(42)
    random.seed(42)

    countOfItems: int = 100
    countOfRepetitions: int = 5

    uBehavStatic08Desc: UserBehaviourDescription = UserBehaviourDescription(
        observationalStaticProbabilityFnc, [0.8])
    uBehavStatic06Desc: UserBehaviourDescription = UserBehaviourDescription(
        observationalStaticProbabilityFnc, [0.6])
    uBehavStatic04Desc: UserBehaviourDescription = UserBehaviourDescription(
        observationalStaticProbabilityFnc, [0.4])
    uBehavStatic02Desc: UserBehaviourDescription = UserBehaviourDescription(
        observationalStaticProbabilityFnc, [0.2])

    uBehavLinear0109Desc: UserBehaviourDescription = UserBehaviourDescription(
        observationalLinearProbabilityFnc, [0.1, 0.9])

    uBehavPowerlaw054min048: UserBehaviourDescription = UserBehaviourDescription(
        observationalPowerLawFnc, [0.54, -0.48])

    # ML
    #   BehavioursML.generateFileMl1m(countOfItems, countOfRepetitions, BehavioursML.BHVR_STATIC08, uBehavStatic08Desc)
    #   BehavioursML.generateFileMl1m(countOfItems, countOfRepetitions, BehavioursML.BHVR_STATIC06, uBehavStatic06Desc)
    #   BehavioursML.generateFileMl1m(countOfItems, countOfRepetitions, BehavioursML.BHVR_STATIC04, uBehavStatic04Desc)
    BehavioursML.generateFileMl1m(countOfItems, countOfRepetitions,
                                  BehavioursML.BHVR_STATIC02,
                                  uBehavStatic02Desc)
def test01():

    print("Simulation: ML DHontThompsonSampling")

    jobID: str = "Roulette1"

    selector = RouletteWheelSelector({RouletteWheelSelector.ARG_EXPONENT: 1})

    rIDs, rDescs = InputRecomSTDefinition.exportPairOfRecomIdsAndRecomDescrs()

    pDescr: Portfolio1AggrDescription = Portfolio1AggrDescription(
        "DHontThompsonSampling" + jobID, rIDs, rDescs,
        InputAggrDefinition.exportADescDHondtThompsonSampling(selector))

    batchID: str = "ml1mDiv90Ulinear0109R1"
    dataset: DatasetML = DatasetML.readDatasets()
    behaviourFile: str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF: DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    model: DataFrame = PModelDHondtBanditsVotes(pDescr.getRecommendersIDs())

    eTool: AEvalTool = EvalToolDHondtBanditVotes({})

    # simulation of portfolio
    simulator: Simulator = Simulator(batchID, SimulationML, argsSimulationDict,
                                     dataset, behavioursDF)
    simulator.simulate([pDescr], [model], [eTool],
                       [HistoryHierDF(pDescr.getPortfolioID())])
Пример #3
0
def test01():

    print("Simulation: ML TheMostPopular")

    rDescr:RecommenderDescription = InputRecomMLDefinition.exportRDescTheMostPopular()

    pDescr:APortfolioDescription = Portfolio1MethDescription(InputRecomMLDefinition.THE_MOST_POPULAR.title(),
                                                             InputRecomMLDefinition.THE_MOST_POPULAR, rDescr)

    batchID:str = "ml1mDiv90Ulinear0109R1"
    dataset:DatasetML = DatasetML.readDatasets()
    behaviourFile:str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF:DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    # remove old results
    #path:str = ".." + os.sep + "results" + os.sep + batchID
    #try:
    #    os.remove(path + os.sep + "computation-theMostPopular.txt")
    #    os.remove(path + os.sep + "historyOfRecommendation-theMostPopular.txt")
    #    os.remove(path + os.sep + "portfModelTimeEvolution-theMostPopular.txt")
    #except:
    #    print("An exception occurred")

    # simulation of portfolio
    simulator:Simulator = Simulator(batchID, SimulationML, argsSimulationDict, dataset, behavioursDF)
    simulator.simulate([pDescr], [DataFrame()], [EToolDoNothing({})], [HistoryHierDF(pDescr.getPortfolioID())])
def test01():

    print("Simulation: ML FuzzyDHondtDirectOptimize")

    jobID: str = "Roulette1"

    selector = RouletteWheelSelector({RouletteWheelSelector.ARG_EXPONENT: 1})

    rIDs, rDescs = InputRecomSTDefinition.exportPairOfRecomIdsAndRecomDescrs()

    pDescr: Portfolio1AggrDescription = Portfolio1AggrDescription(
        "FuzzyDHondtDirectOptimize" + jobID, rIDs, rDescs,
        InputAggrDefinition.exportADescDHondtDirectOptimizeThompsonSampling(
            selector, "DCG"))

    batchID: str = "ml1mDiv90Ulinear0109R1"
    dataset: DatasetML = DatasetML.readDatasets()
    behaviourFile: str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF: DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    #model:DataFrame = PModelDHont(pDescr.getRecommendersIDs())
    model: DataFrame = PModelDHondtBanditsVotes(pDescr.getRecommendersIDs())

    #lrClick:float = 0.03
    #lrView:float = lrClick / 500
    #eTool:AEvalTool = EvalToolDHondt({EvalToolDHondt.ARG_LEARNING_RATE_CLICKS: lrClick,
    #                                  EvalToolDHondt.ARG_LEARNING_RATE_VIEWS: lrView})
    eTool: AEvalTool = EvalToolDHondtBanditVotes({})
    # simulation of portfolio
    simulator: Simulator = Simulator(batchID, SimulationML, argsSimulationDict,
                                     dataset, behavioursDF)
    simulator.simulate([pDescr], [model], [eTool],
                       [HistoryHierDF(pDescr.getPortfolioID())])
    def exportSimulatorML1M(self, batchID: str,
                            divisionDatasetPercentualSize: int,
                            uBehaviourID: str, repetition: int):

        argsSimulationDict: dict = {
            SimulationML.ARG_WINDOW_SIZE: 5,
            SimulationML.ARG_RECOM_REPETITION_COUNT: repetition,
            SimulationML.ARG_NUMBER_OF_RECOMM_ITEMS: 100,
            SimulationML.ARG_NUMBER_OF_AGGR_ITEMS: self.numberOfAggrItems,
            SimulationML.ARG_DIV_DATASET_PERC_SIZE:
            divisionDatasetPercentualSize,
            SimulationML.ARG_HISTORY_LENGTH: 10
        }

        # dataset reading
        dataset: ADataset = DatasetML.readDatasets()

        behaviourFile: str = BehavioursML.getFile(uBehaviourID)
        behavioursDF: DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

        # simulation of portfolio
        simulator: Simulator = Simulator(batchID, SimulationML,
                                         argsSimulationDict, dataset,
                                         behavioursDF)

        return simulator
Пример #6
0
def test06():

    print("Simulation: ML VMCMF")

    rDescr:RecommenderDescription = InputRecomSTDefinition.exportRDescVMContextKNN()

    pDescr:APortfolioDescription = Portfolio1MethDescription(InputRecomSTDefinition.VMC_KNN.title(),
                                                             InputRecomSTDefinition.VMC_KNN, rDescr)

    batchID:str = "mlDiv90Ulinear0109R1"
    dataset:DatasetML = DatasetML.readDatasets()
    behaviourFile:str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF:DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    # simulation of portfolio
    simulator:Simulator = Simulator(batchID, SimulationML, argsSimulationDict, dataset, behavioursDF)
    simulator.simulate([pDescr], [DataFrame()], [EToolDoNothing({})], [HistoryHierDF(pDescr.getPortfolioID())])
Пример #7
0
def test02():

    print("Simulation: ML W2V")

    rDescr:RecommenderDescription = InputRecomMLDefinition.exportRDescW2Vtpositivei50000ws1vs32upsweightedMeanups3()

    pDescr:APortfolioDescription = Portfolio1MethDescription("W2vPositiveMax",
                                    "w2vPositiveMax", rDescr)

    batchID:str = "ml1mDiv90Ulinear0109R1"
    dataset:DatasetML = DatasetML.readDatasets()
    behaviourFile:str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF:DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    # simulation of portfolio
    simulator:Simulator = Simulator(batchID, SimulationML, argsSimulationDict, dataset, behavioursDF)
    simulator.simulate([pDescr], [DataFrame()], [EToolDoNothing({})], [HistoryHierDF(pDescr.getPortfolioID())])
def test01():

    print("Simulation: ML ContextDHondtINF")

    jobID: str = "Roulette1"

    selector = RouletteWheelSelector({RouletteWheelSelector.ARG_EXPONENT: 1})

    #pProbToolOLin0802HLin1002:APenalization = PenalizationToolDefinition.exportProbPenaltyToolOStat08HLin1002(
    #    InputSimulatorDefinition.numberOfAggrItems)
    pToolOLin0802HLin1002: APenalization = PenalizationToolDefinition.exportPenaltyToolOLin0802HLin1002(
        InputSimulatorDefinition.numberOfAggrItems)

    itemsDF: DataFrame = Items.readFromFileMl1m()
    usersDF: DataFrame = Users.readFromFileMl1m()

    historyDF: AHistory = HistoryDF("test01")

    eTool: AEvalTool = EvalToolContext({
        EvalToolContext.ARG_USERS: usersDF,
        EvalToolContext.ARG_ITEMS: itemsDF,
        EvalToolContext.ARG_DATASET: "ml",
        EvalToolContext.ARG_HISTORY: historyDF
    })

    rIDs, rDescs = InputRecomSTDefinition.exportPairOfRecomIdsAndRecomDescrs()

    pDescr: Portfolio1AggrDescription = Portfolio1AggrDescription(
        "ContextDHondtINF" + jobID, rIDs, rDescs,
        InputAggrDefinition.exportADescDContextHondtINF(
            selector, pToolOLin0802HLin1002, eTool))

    batchID: str = "ml1mDiv90Ulinear0109R1"
    dataset: DatasetML = DatasetML.readDatasets()
    behaviourFile: str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF: DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    model: DataFrame = PModelDHondt(pDescr.getRecommendersIDs())

    # simulation of portfolio
    simulator: Simulator = Simulator(batchID, SimulationML, argsSimulationDict,
                                     dataset, behavioursDF)
    simulator.simulate([pDescr], [model], [eTool],
                       [HistoryHierDF(pDescr.getPortfolioID())])
def test01():

    print("Simulation: ML FuzzyDHondtINF")

    jobID: str = "Roulette1"

    selector = RouletteWheelSelector({RouletteWheelSelector.ARG_EXPONENT: 1})

    pProbToolOLin0802HLin1002: APenalization = PenalizationToolDefinition.exportProbPenaltyToolOStat08HLin1002(
        InputSimulatorDefinition.numberOfAggrItems)

    rIDs, rDescs = InputRecomSTDefinition.exportPairOfRecomIdsAndRecomDescrs()

    pDescr: Portfolio1AggrDescription = Portfolio1AggrDescription(
        "FuzzyDHondtINF" + jobID, rIDs, rDescs,
        InputAggrDefinition.exportADescDHondtINF(selector,
                                                 pProbToolOLin0802HLin1002))

    batchID: str = "ml1mDiv90Ulinear0109R1"
    dataset: DatasetML = DatasetML.readDatasets()
    behaviourFile: str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF: DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    model: DataFrame = PModelDHondt(pDescr.getRecommendersIDs())

    lrClick: float = 0.03
    lrView: float = lrClick / 500
    eTool: AEvalTool = EvalToolDHondt({
        EvalToolDHondt.ARG_LEARNING_RATE_CLICKS:
        lrClick,
        EvalToolDHondt.ARG_LEARNING_RATE_VIEWS:
        lrView
    })

    # simulation of portfolio
    simulator: Simulator = Simulator(batchID, SimulationML, argsSimulationDict,
                                     dataset, behavioursDF)
    simulator.simulate([pDescr], [model], [eTool],
                       [HistoryHierDF(pDescr.getPortfolioID())])
Пример #10
0
def test01():

    print("Simulation: ML ContextFuzzyDHondtDirectOptimize")

    jobID: str = "Roulette1"

    selector: ADHondtSelector = RouletteWheelSelector(
        {RouletteWheelSelector.ARG_EXPONENT: 1})

    itemsDF: DataFrame = Items.readFromFileMl1m()
    usersDF: DataFrame = Users.readFromFileMl1m()

    historyDF: AHistory = HistoryHierDF("test01")

    eTool: AEvalTool = EvalToolContext({
        EvalToolContext.ARG_USERS: usersDF,
        EvalToolContext.ARG_ITEMS: itemsDF,
        EvalToolContext.ARG_DATASET: "ml",
        EvalToolContext.ARG_HISTORY: historyDF
    })

    rIDs, rDescs = InputRecomSTDefinition.exportPairOfRecomIdsAndRecomDescrs()

    pDescr: Portfolio1AggrDescription = Portfolio1AggrDescription(
        "ContextFuzzyDHondtDirectOptimize" + jobID, rIDs, rDescs,
        InputAggrDefinition.exportADescContextFuzzyDHondtDirectOptimize(
            selector, eTool))

    batchID: str = "ml1mDiv90Ulinear0109R1"
    dataset: DatasetML = DatasetML.readDatasets()
    behaviourFile: str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF: DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    model: DataFrame = PModelDHondt(pDescr.getRecommendersIDs())

    # simulation of portfolio
    simulator: Simulator = Simulator(batchID, SimulationML, argsSimulationDict,
                                     dataset, behavioursDF)
    simulator.simulate([pDescr], [model], [eTool], [historyDF])
Пример #11
0
def test04():

    print("Simulation: ML CB")

    #rDescr:RecommenderDescription = InputRecomMLDefinition.exportRDescCBmean()
    rDescr:RecommenderDescription = InputRecomMLDefinition.exportRDescCosineCBcbdOHEupsmaxups1()


    #pDescr:APortfolioDescription = Portfolio1MethDescription(InputRecomMLDefinition.COS_CB_MEAN.title(),
    #                                InputRecomMLDefinition.COS_CB_MEAN, rDescr)
    pDescr:APortfolioDescription = Portfolio1MethDescription(InputRecomMLDefinition.COS_CB_WINDOW3.title(),
                                                             InputRecomMLDefinition.COS_CB_WINDOW3, rDescr)


    batchID:str = "ml1mDiv90Ulinear0109R1"
    dataset:DatasetML = DatasetML.readDatasets()
    behaviourFile:str = BehavioursML.getFile(BehavioursML.BHVR_LINEAR0109)
    behavioursDF:DataFrame = BehavioursML.readFromFileMl1m(behaviourFile)

    # simulation of portfolio
    simulator:Simulator = Simulator(batchID, SimulationML, argsSimulationDict, dataset, behavioursDF)
    simulator.simulate([pDescr], [DataFrame()], [EToolDoNothing({})], [HistoryHierDF(pDescr.getPortfolioID())])
Пример #12
0
    def __generateGeneralBehaviourrRR(behavioursDF: DataFrame,
                                      numberOfItems: int,
                                      countOfRepetitions: int,
                                      uBehavDesc: UserBehaviourDescription):

        for numberOfRepetitionI in range(countOfRepetitions):
            for indexJ, rowJ in behavioursDF.iterrows():
                if indexJ % 1000 == 0:
                    print("Generating repetition " + str(numberOfRepetitionI) +
                          "   " + str(indexJ) + " / " +
                          str(behavioursDF.shape[0]))

                repetitionIJ: int = rowJ[BehavioursRR.COL_REPETITION]
                if numberOfRepetitionI != repetitionIJ:
                    continue

                uBehavIJ: List[bool] = uBehavDesc.getBehaviour(numberOfItems)
                strBehavIJ: str = BehavioursML.convertToString(uBehavIJ)
                behavioursDF.at[indexJ,
                                BehavioursRR.COL_BEHAVIOUR] = strBehavIJ
    def simulateRecommendations(
            self, portfolios: List[APortfolio],
            portfolioDescs: List[APortfolioDescription],
            portFolioModels: List[DataFrame], evaluatonTools: List[AEvalTool],
            histories: List[AHistory], evaluations: List[dict],
            currentDFIndex: int, counterI: int, counterMax: int, userID: int,
            sessionID: int, repetition: int, testRatingsDF: DataFrame,
            testBehaviourDict: Dict[int, DataFrame],
            windowOfItemIDsI: List[int], currentPageType: object):

        COL_BEHAVIOUR: str = self._behaviourClass.getColNameBehaviour()
        COL_ITEMID: str = self._ratingClass.getColNameItemID()

        currentItemID: int = testRatingsDF.loc[currentDFIndex][COL_ITEMID]

        print("userID: " + str(userID))
        print("currentDFIndex: " + str(currentDFIndex))
        print("currentItemID: " + str(currentItemID))
        print("repetition: " + str(repetition))

        uObservationStrI: str = testBehaviourDict[repetition].loc[
            currentDFIndex][COL_BEHAVIOUR]
        uObservation: List[bool] = BehavioursML.convertToListOfBoolean(
            uObservationStrI)
        #uObservation: List[bool] = [True]*20

        print("uObservation: " + str(uObservation))

        portfolioI: Portfolio1Aggr
        portFolioModelI: pd.DataFrame
        historyI: pd.DataFrame
        for portfolioI, portfolioDescI, portFolioModelI, evaluatonToolI, historyI, evaluationI in zip(
                portfolios, portfolioDescs, portFolioModels, evaluatonTools,
                histories, evaluations):

            self.simulateRecommendation(portfolioI, portfolioDescI,
                                        portFolioModelI, evaluatonToolI,
                                        historyI, evaluationI, currentDFIndex,
                                        counterI, counterMax, testRatingsDF,
                                        uObservation, userID, sessionID,
                                        windowOfItemIDsI, currentPageType)