def visualisationOfContourLines(): #modelConf:LinPrefModelConfiguration modelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) aggrLevel = 0.90 # fileNameUsers:String fileNameUsers = "../datasets/usersRefractedModel.csv" # users:User[] users = readUsers(fileNameUsers); pointsDataCube = [Point(0.73, 0.755), Point(0.125, 0.45), Point(0.624, 0.15)] labels = ["A", "B", "C"] for indexI in range(len(users)): user = users[indexI] user.printUser() userProfileModel1 = user.exportUserProfileModel(); #userProfileModel1 = user.exportUserProfileRefractedModel(); # title:String title = 'Lin. pref. model' # painting:Painting painting = Painting(modelConf, title) painting.paintOnlyModel(userProfileModel1, aggrLevel, 'g') painting.paintPoints(pointsDataCube, [], labelsDataCube=[], labelsPrefCube=[], color="b", size=1) painting.paint(indexI); #painting.save(indexI); break;
def paintPreferenceCube(): #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(2.0, 1.25, 1.0, 1.0) #points:Point[] points = [ Point(0.1, 0.1), Point(0.2, 0.2), Point(0.3, 0.4), Point(0.9, 0.5) ] g = PaintingPreferenceCubeModel(linPrefModelConf, "Data Cube") g.paintPreferencerCube(points) g.paint(1)
class InputSimpleShow: # linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # userProfileModelStructured:UserProfileModelStructured userProfileModelStructured = UserProfileModelStructured( PrefFncRefractedModel(0.5), PrefFncRefractedModel(0.5), AggrFnc([0.5, 0.5])) # aggrLevel:Float aggrLevel = 0.8 # pointsVisitedDC:list<PointWithID> pointsWithIDVisitedDC = [ PointWithID(Point(0.70, 0.75), "A"), PointWithID(Point(0.15, 0.45), "B"), PointWithID(Point(0.60, 0.15), "C"), PointWithID(Point(0.40, 0.85), "D"), PointWithID(Point(0.5, 0.5), "E") ] # pointsNoVisitedDC:list<PointWithID> pointsWithIDNoVisitedDC = [ PointWithID(Point(0.25, 0.15), "M"), PointWithID(Point(0.1, 0.65), "N"), PointWithID(Point(0.35, 0.85), "O"), PointWithID(Point(0.9, 0.7), "P"), PointWithID(Point(0.65, 0.45), "Q") ] def generatePointsWithIDVisitedDC(count): points = [] for i in range(count): x = random.uniform(0, 1) y = random.uniform(0, 1) pI = PointWithID(Point(x, y), i) points.append(pI) return points def generatePointsWithIDNoVisitedDC(count): points = [] for i in range(count): x = random.uniform(0, 1) y = random.uniform(0, 1) pI = PointWithID(Point(x, y), 100 + i) points.append(pI) return points
def show(self, width, height): # linPrefModelConf:LinPrefModelConfiguration self.linPrefModelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # userID:int self.userID = 1 # dimensionX:int self.dimensionX = 0 self.dimensionY = 1 self.wx = 0.5 # aggrLevel:Float self.aggrLevel = 0.8 self.form(); self.model1()
def __generateRatings(self, dmDescription, upModelsDef, pointNDWithIDs): # upModel:UserProfileModelND upModel = upModelsDef.exportUserProfileModelND() # points:list<PointND> points = [pointI.point for pointI in pointNDWithIDs] #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # ratingValues:List<float> ratingValues = upModel.preferenceOfPointsInDataCube( points, linPrefModelConf) # ratings:List<Rating> ratings = [ Rating(upModelsDef.userID, pointNDWithIDs[i].pointID, ratingValues[i]) for i in range(len(ratingValues)) ] return ratings
def __init__(self): super().__init__() self.setWindowTitle('Linear Monotone Preference Model') self.resize(550, 550) self._main = QtWidgets.QWidget() self.setCentralWidget(self._main) self.layout = QtWidgets.QVBoxLayout(self._main) self.model = ModelOfPoints() self.c = CanvasDimensions(550, 550) #widget = CanvasWidget(self.c, self.model) #self.layout.addWidget(widget) linPrefModelConf = LinPrefModelConfiguration(1, 1, 1, 1) model = GraphicalModel(linPrefModelConf) model.initFigure("ahoj") self.layout.addWidget(model.fig1)
class InputTwoUsers: #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(2.0, 1.25, 1.0, 1.0) #user2D1:User2D user2D1 = User2D(1, 1.5, 0.3, 0.66) #user2D2:User2D user2D2 = User2D(2, 0.5, 0.9, 0.33) # aggrLevel1:Float aggrLevel1 = 0.667 # aggrLevel2:Float aggrLevel2 = 0.667 #pointsWithIdVisitedDC:Point[] #pointsDataCube = [Point(0.73, 0.755), Point(1.125, 0.45), Point(1.624, 0.15)] pointsWithIdVisitedDC = [ PointWithID(Point(0.5, 0.5), "A"), PointWithID(Point(0.75, 0.5), "B"), PointWithID(Point(0.5, 0.75), "C"), PointWithID(Point(0.75, 0.75), "D") ]
def paintData(): #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) #userProfileModel1:UserProfileModel userProfileModel1 = getUserProfileModel01() #userProfileModel2:UserProfileModel userProfileModel2 = getUserProfileModel02() # fileNameItems:String fileNameItems = "../datasets/itemsRefractedModel.csv" # items:Item[] items = readItems(fileNameItems); #pointsDataCube:Point[] pointsDataCube = [Point(i.x, i.y) for i in items] pointsPrefCube1 = userProfileModel1.pointsDataCubeToPointsPrefCube(pointsDataCube); pointsPrefCube2 = userProfileModel2.pointsDataCubeToPointsPrefCube(pointsDataCube); # preferences:float[] preferences1 = userProfileModel1.preferenceOfPointsInPC(pointsPrefCube1, linPrefModelConf); preferences2 = userProfileModel2.preferenceOfPointsInPC(pointsPrefCube2, linPrefModelConf); rmseVal = rmse(preferences1, preferences2) print("rmse: ", rmseVal); #title:String title = 'Lin. pref. model'; #painting:Painting painting = Painting(linPrefModelConf, title) painting.paintModelOnlyPoints(userProfileModel1, pointsDataCube, pointsPrefCube1, "r"); painting.paintModelOnlyPoints(userProfileModel2, pointsDataCube, pointsPrefCube1, "g"); painting.paint("");
class InputThreshold: #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(2.0, 1.25, 1.0, 1.0) #title:String title = "Preferenc Cube" #points:Point[] points = [ Point(0.1, 0.1), Point(0.2, 0.2), Point(0.3, 0.4), Point(0.9, 0.5) ] # pointsWithIDs:PointWithID[] pointsWithIDs = [ PointWithID(Point(0.1, 0.9), 'A'), PointWithID(Point(0.2, 0.8), 'B'), PointWithID(Point(0.3, 0.7), 'C'), PointWithID(Point(0.4, 0.6), 'D'), PointWithID(Point(0.5, 0.5), 'E') ]
def convertRefractedModelToTrianularModel(): #modelConf:LinPrefModelConfiguration modelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # usersAll:User[] usersAll = readUsers() #print("usersAll: ", len(usersAll)) # fileNameRatings:String fileNameRatings = ".." + os.sep + "datasets" + os.sep + "ratingsRefractedModel.csv" # ratingsAll:Rating[] ratingsAll = readRatings(fileNameRatings) #print("ratingsAll: ", len(ratingsAll)) # itemsAll:Item[] itemsAll = readItems() #print("itemsAll: ", len(itemsAll)) # ratingsNew:Rating[] ratingsNew = [] # userIds:int[] userIds = [u.uid for u in usersAll] for userIdI in userIds: #print(userIdI) # user:User user = [u for u in usersAll if u.uid == userIdI][0] #user.printUser(); #userProfileModel = user.exportUserProfileTriangularModel() userProfileModel = user.exportUserProfileRefractedModel() # ratings:Rating[] ratings = [r for r in ratingsAll if r.uid == userIdI] #print("ratings: ", len(ratings)) # items:Item[] items = [i for i in itemsAll if i.iid in [r.iid for r in ratings]] #print("items: ", len(items)) # pointsDataCube:Point[] #pointsDataCube = [i.exportAsPoint() for i in items] # preferences:Float[] #preferences = userProfileModel.preferenceOfPointsInDataCube(pointsInDataCube, modelConf); # itemIndexI:int for itemIndexI in range(0, len(items)): # itemI:Item itemI = items[itemIndexI] # ratingI:Rating ratingI = ratings[itemIndexI] # pointInDataCubeI:Point pointInDataCubeI = itemI.exportAsPoint() # preferencesI:Float[] preference2I = userProfileModel.preferenceOfPointInDataCube( pointInDataCubeI, modelConf) # ratingI:Rating[] ratingI = Rating(userIdI, ratingI.iid, preference2I, 0, ratingI.typ) ratingsNew.append(ratingI) fileNameRatings2 = ".." + os.sep + "datasets" + os.sep + "ratingsRectangledModel.csv" writeRatings(ratingsNew, fileNameRatings2) # test reading computed dataset # ratingsAll3:Rating[] ratingsAll3 = readRatings(fileNameRatings2)
def testDatasetRMSE(): aggrLevel = 0.95 #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # fileNameUsers:String fileNameUsers = "../datasets/usersRefractedModel.csv" # usersAll:User[] usersAll = readUsers(fileNameUsers) print("usersAll: ", len(usersAll)) # fileNameRatings:String fileNameRatings = "../datasets/ratingsRefractedModel.csv" # ratingsAll:Rating[] ratingsAll = readRatings(fileNameRatings) #print("ratingsAll: ", len(ratingsAll)) # fileNameItems:String fileNameItems = "../datasets/itemsRefractedModel.csv" # itemsAll:Item[] itemsAll = readItems(fileNameItems) #print("itemsAll: ", len(itemsAll)) userIds = [u.uid for u in usersAll] for userIdI in userIds: # user:User user = [u for u in usersAll if u.uid == userIdI][0] user.printUser() user = user.exportUser2D() user.printUser() # ratings:Rating[] ratings = [r for r in ratingsAll if r.uid == userIdI] #print("ratings: ", len(ratings)) # ratingsTrain:Rating[] ratingsTrain = [r for r in ratings if r.typ == 1] # ratingsTest:Rating[] ratingsTest = [r for r in ratings if r.typ == 2] # itemsTrain:Item[] itemsTrain = [ i for i in itemsAll if i.iid in [r.iid for r in ratingsTrain] ] # itemsTest:Item[] itemsTest = [ i for i in itemsAll if i.iid in [r.iid for r in ratingsTest] ] # pointsTrain:Point[] pointsTrain = [i.exportAsPoint() for i in itemsTrain] # pointsTest:Point[] pointsTest = [i.exportAsPoint() for i in itemsTest] # prefsTrain:float[] prefsTrain = [r.p2 for r in ratingsTrain] # prefsTest:float[] prefsTest = [r.p2 for r in ratingsTest] #userProfileModel1 = user.exportUserProfileTriangularModel(linPrefModelConf) userProfileModel1 = user.exportUserProfileRefractedModel( linPrefModelConf) # pointsPrefCubeTrain:Point[] pointsPrefCubeTrain = userProfileModel1.pointsDataCubeToPointsPrefCube( pointsTrain) pointsPrefCubeTest = userProfileModel1.pointsDataCubeToPointsPrefCube( pointsTest) # prefsPointsPrefCubeTrain:Point[] prefsPointsPrefCubeTrain = userProfileModel1.preferenceOfPointsInPC( pointsPrefCubeTrain, linPrefModelConf) prefsPointsPrefCubeTest = userProfileModel1.preferenceOfPointsInPC( pointsPrefCubeTest, linPrefModelConf) rmseTrainVal = rmse(prefsPointsPrefCubeTrain, prefsTrain) print("rmseTrain: ", rmseTrainVal) rmseTestVal = rmse(prefsPointsPrefCubeTest, prefsTest) print("rmseTest: ", rmseTestVal) # title:String title = 'Lin. pref. model' # painting:Painting painting = Painting(linPrefModelConf, title) painting.paintOnlyModel(userProfileModel1, aggrLevel, 'r') painting.paintPoints(pointsTrain, pointsPrefCubeTrain, labelsDataCube=[], labelsPrefCube=[], color="b", size=1) painting.paintPoints(pointsTest, pointsPrefCubeTest, labelsDataCube=[], labelsPrefCube=[], color="r", size=1) #painting.paint(userIdI); painting.save(userIdI) painting.close()
def search(): aggrLevel = 0.95 #modelConf:LinPrefModelConfiguration modelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # fileNameUsers:String fileNameUsers = "../datasets/usersRefractedModel.csv" # usersAll:User[] usersAll = readUsers(fileNameUsers) #print("usersAll: ", len(usersAll)) # fileNameRatings:String fileNameRatings = "../datasets/ratingsRefractedModel.csv" # ratingsAll:Rating[] ratingsAll = readRatings(fileNameRatings) #print("ratingsAll: ", len(ratingsAll)) # fileNameItems:String fileNameItems = "../datasets/itemsRefractedModel.csv" # itemsAll:Item[] itemsAll = readItems(fileNameItems) #print("itemsAll: ", len(itemsAll)) userIds = [u.uid for u in usersAll] userId = userIds[0] print("Input:") # user:User user = [u for u in usersAll if u.uid == userId][0] user.printUser() # ratings:Rating[] ratings = [r for r in ratingsAll if r.uid == userId] #print("ratings: ", len(ratings)) # ratingsTrain:Rating[] ratingsTrain = [r for r in ratings if r.typ == 1] # ratingsTest:Rating[] ratingsTest = [r for r in ratings if r.typ == 2] # itemsTrain:Item[] itemsTrain = [ i for i in itemsAll if i.iid in [r.iid for r in ratingsTrain] ] # itemsTest:Item[] itemsTest = [i for i in itemsAll if i.iid in [r.iid for r in ratingsTest]] # pointsTrain:Point[] pointsTrain = [i.exportAsPoint() for i in itemsTrain] # pointsTest:Point[] pointsTest = [i.exportAsPoint() for i in itemsTest] # prefsTrain:float[] prefsTrain = [r.p2 for r in ratingsTrain] # prefsTest:float[] prefsTest = [r.p2 for r in ratingsTest] ix = user.ix iy = user.iy wx = user.exportNormalizedWX() wy = user.exportNormalizedWY() print("User ix: " + str(ix) + ", iy: " + str(iy) + ", wx: " + str(wx)) ## searchAlg:RandomSearch #searchAlg = RandomSearch() ## theBestIndivEval:IndividualEvaluated = modelConf:LinPrefModelConfiguration, pointsTrain:Point[], prefsTrain:Point[], fitnessFnc:Function, generateFnc:Function #theBestIndivEval = searchAlg.search(modelConf, pointsTrain, prefsTrain, numberOfGenerations=10, fitnessFnc=fitnessRMSE, generateFnc=operatorGenerate) # searchAlg:HillClimbing searchAlg = HillClimbing() # theBestIndivEval:IndividualEvaluated = modelConf:LinPrefModelConfiguration, pointsTrain:Point[], prefsTrain:Point[], fitnessFnc:Function, generateFnc:Function, neighborFnc:Function theBestIndivEval = searchAlg.search( modelConf, pointsTrain, prefsTrain, numberOfGenerations=10000, fitnessFnc=fitnessRMSE_, generateFnc=operatorGenerate, neighborFnc=operatorRandomMoveTriangularModel) theBestIndiv = theBestIndivEval.individual # userProfileModel:UserProfileModel userProfileModel = theBestIndiv.exportUserProfileRefractedModel() print("") print("Result:") #userProfileModel.print() theBestIndiv.printIndividualUser() # pointsPrefCubeTrain:Point[] pointsPrefCubeTrain = userProfileModel.pointsDataCubeToPointsPrefCube( pointsTrain) pointsPrefCubeTest = userProfileModel.pointsDataCubeToPointsPrefCube( pointsTest) # prefsPointsPrefCubeTrain:Point[] prefsPointsPrefCubeTrain = userProfileModel.preferenceOfPointsInPC( pointsPrefCubeTrain, modelConf) prefsPointsPrefCubeTest = userProfileModel.preferenceOfPointsInPC( pointsPrefCubeTest, modelConf) fitnessRMSETrain = rmse(prefsPointsPrefCubeTrain, prefsTrain) print("fitnessRMSETrain: ", fitnessRMSETrain) fitnessRMSETest = rmse(prefsPointsPrefCubeTest, prefsTest) print("fitnessRMSETest: ", fitnessRMSETest)
def paintContourLinesExample(): #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(2.0, 1.25, 1.0, 1.0) #userProfileModel1:UserProfileModel userProfileModel1 = getUserProfileModelExample01() #userProfileModel2:UserProfileModel userProfileModel2 = getUserProfileModelExample02() aggrLevel = 0.667 #polygon1:Point[] polygon1 = userProfileModel1.getMorphismOfAggregationFncToDataCubePolygon( aggrLevel, linPrefModelConf) # surface:float surface1 = countPolygonSurfaceArea(polygon1) print("Surface1: ", surface1) #polygon1:Point[] polygon2 = userProfileModel2.getMorphismOfAggregationFncToDataCubePolygon( aggrLevel, linPrefModelConf) # surface:float surface2 = countPolygonSurfaceArea(polygon2) print("Surface2: ", surface2) surfaceOfOverlap = countIntersection(polygon1, polygon2) print("Overlap: ", surfaceOfOverlap) #pointsDataCube:Point[] pointsDataCube = [ Point(0.73, 0.755), Point(1.125, 0.45), Point(1.624, 0.15) ] labels = ["A", "B", "C"] pointsPrefCube = userProfileModel1.pointsDataCubeToPointsPrefCube( pointsDataCube) pointsPrefCube2 = userProfileModel2.pointsDataCubeToPointsPrefCube( pointsDataCube) # title:String title = 'Lin. pref. model' # painting:Painting painting = Painting(linPrefModelConf, title) painting.paintOnlyModel(userProfileModel1, aggrLevel, 'r') painting.paintOnlyModel(userProfileModel2, aggrLevel, 'g') painting.paintPoints(pointsDataCube, pointsPrefCube, labelsDataCube=labels, labelsPrefCube=labels, color="r", size=1) painting.paintPoints(pointsDataCube, pointsPrefCube2, labelsDataCube=labels, labelsPrefCube=labels, color="b", size=1) painting.paint("")
def cv03(): # numberOfUsers:int numberOfUsers = 10 #linPrefModelConf:LinPrefModelConfiguration linPrefModelConf = LinPrefModelConfiguration(1.0, 1.0, 1.0, 1.0) # fileNameUsers:String fileNameUsers = "../datasets/usersRefractedModel.csv" # fileNameRatings:String fileNameRatings = "../datasets/ratingsRefractedModel.csv" # fileNameItems:String fileNameItems = "../datasets/itemsRefractedModel.csv" # estimateUserFnc:function, testID:str estimateUserFnc = estimateUserByTheBestRating; testID='theBestRating' #estimateUserFnc = estimateUserByMiddleOf23; testID='middleOf23' #estimateUserFnc = estimateUserByCenterOfMassOf23; testID='centerOfMassOf23' #estimateUserFnc = estimateUserBy10Random; testID='10Random' print(estimateUserFnc) # usersAll:User[] usersAll = readUsers(fileNameUsers); #print("usersAll: ", len(usersAll)) # ratingsAll:Rating[] ratingsAll = readRatings(fileNameRatings); #print("ratingsAll: ", len(ratingsAll)) # itemsAll:Item[] itemsAll = readItems(fileNameItems); #print("itemsAll: ", len(itemsAll)) # errors:float[] errors = [] # user:User for user in usersAll: # user2DI:User2D user2DI = user.exportUser2D() user2DI.printUser(); # ratings:Rating[] ratings = [r for r in ratingsAll if r.uid == user2DI.uid] #print("ratings: ", len(ratings)) # ratingsTrain:Rating[] ratingsTrain = [r for r in ratings if r.typ == 1] # ratingsTest:Rating[] ratingsTest = [r for r in ratings if r.typ == 2] # itemsTrain:Item[] itemsTrain = [i for i in itemsAll if i.iid in [r.iid for r in ratingsTrain]] # itemsTest:Item[] itemsTest = [i for i in itemsAll if i.iid in [r.iid for r in ratingsTest]] # pointsTrain:PointWithRating[] pointsWithRatingTrain = [PointWithRating(itemsTrain[i].exportAsPoint(), ratingsTrain[i].p2) for i in range(len(ratingsTrain))] # pointsTest:PointWithRating[] pointsWithRatingTest = [PointWithRating(itemsTest[i].exportAsPoint(), ratingsTest[i].p2) for i in range(len(ratingsTest))] # user2D:User2D, error:float user2DNewI, errorI = process(user2DI, estimateUserFnc, pointsWithRatingTrain, pointsWithRatingTest, linPrefModelConf, testID=testID) errors.append(errorI); if len(errors) == numberOfUsers: break; print("Mean: ", mean(errors))