def __cutSegmentInTheMiddle(lineSegmentsX, theWorstX): #print("__cutSegmentInTheMiddle") # lineSegmentDontIntersect:list<LineSegment> lineSegmentIntersect = lineSegmentsX.lineSegmentsWhichIntersectAxisParallelToY( theWorstX) lineSegmentDontIntersect = lineSegmentsX.lineSegmentsWhichDontIntersectAxisParallelToY( theWorstX) # midpoint:Point midpoint = lineSegmentIntersect.midpoint() # functionValueMidpoint:float functionValueMidpoint = lineSegmentIntersect.intersectionWithTheAxisParallelToY( midpoint.x) # fncValue:float fncValue = functionValueMidpoint + 0.1 * random.uniform(-1, 1) if fncValue < 0.0: fncValue = 0.0 if fncValue > 1.0: fncValue = 1.0 #print("functionValueMidpoint: " + str(functionValueMidpoint)) #print("functionValueNew : " + str(fncValue)) # lineSegmentsNew:list<LineSegment> lineSegmentsNew = list(lineSegmentDontIntersect) lineSegmentsNew.append( LineSegment(lineSegmentIntersect.point1, Point(midpoint.x, fncValue))) lineSegmentsNew.append( LineSegment(Point(midpoint.x, fncValue), lineSegmentIntersect.point2)) return LineSegments(lineSegmentsNew)
def exportAsLineSegment(self, point, linPrefModelConf): aggrLine = self.exportAsLine(point) point1 = Point( linPrefModelConf.AXIS_X_BEGIN_PREF_CUBE, aggrLine.getFunctionValue(linPrefModelConf.AXIS_X_BEGIN_PREF_CUBE)) point2 = Point( linPrefModelConf.AXIS_X_END_PREF_CUBE, aggrLine.getFunctionValue(linPrefModelConf.AXIS_X_END_PREF_CUBE)) point3 = Point( aggrLine.getDomainValue(linPrefModelConf.AXIS_Y_BEGIN_PREF_CUBE), linPrefModelConf.AXIS_Y_BEGIN_PREF_CUBE) point4 = Point( aggrLine.getDomainValue(linPrefModelConf.AXIS_Y_END_PREF_CUBE), linPrefModelConf.AXIS_Y_END_PREF_CUBE) pointsOnAxes = getMorphismToPrefCube( [point1, point2, point3, point4], linPrefModelConf.AXIS_X_BEGIN_PREF_CUBE, linPrefModelConf.AXIS_Y_BEGIN_PREF_CUBE, linPrefModelConf.AXIS_X_END_PREF_CUBE, linPrefModelConf.AXIS_Y_END_PREF_CUBE) # LineSegment return LineSegment(pointsOnAxes[0], pointsOnAxes[1])
def exportAsPrefFncY(self, linPrefModelConf): # points:List<Point> points = [ Point(0, 0), Point(1, self.iCoordinate), Point(0, linPrefModelConf.SIZE_Y_DATA_CUBE) ] # lineSegments:LineSegments lineSegments = LineSegments([ LineSegment(points[i], points[i + 1]) for i in range(0, len(points) - 1) ]) # +30 in domain of a function -> +10 in range # +50 in domain of a function -> +80 in range # +20 in domain of a function -> +10 in range segmentation = [Pair(10, 30), Pair(80, 50), Pair(10, 20)] # refractedPrefFnc:LineSegment[] refractedPrefFnc = [] for lineSegmI in lineSegments.lineSegments: # s:LineSegment[] s = [] if lineSegmI.isDecreasingOnY(): s = lineSegmI.exportSegmentation(segmentation) else: s = lineSegmI.exportSegmentation(segmentation[::-1]) refractedPrefFnc = refractedPrefFnc + s return PrefFncY.createFromLineSegments(refractedPrefFnc)
def exportAsPrefFncX(self, linPrefModelConf): # points:List<Point> points = [ Point(0, 0), Point(self.iCoordinate, 1), Point(linPrefModelConf.SIZE_X_DATA_CUBE, 0) ] # lineSegments:LineSegments lineSegments = LineSegments([ LineSegment(points[i].clone(), points[i + 1].clone()) for i in range(0, len(points) - 1) ]) # +30 in domain of a function -> +10 in range # +50 in domain of a function -> +80 in range # +20 in domain of a function -> +10 in range segmentation = [Pair(30, 10), Pair(50, 80), Pair(20, 10)] # refractedPrefFnc:LineSegment[] refractedPrefFnc: List[LineSegment] = [] for lineSegmI in lineSegments.lineSegments: # s:LineSegment[] s = [] if lineSegmI.isDecreasingOnY(): s = lineSegmI.exportSegmentation(segmentation) else: s = lineSegmI.exportSegmentation(segmentation[::-1]) refractedPrefFnc = refractedPrefFnc + s return PrefFncX.createFromLineSegments(refractedPrefFnc)
def __init__(self, points: List[Point]): if type(points) is not list: raise ValueError("Argument points isn't type list.") for pointI in points: if type(pointI) is not Point: raise ValueError("Argument points don't contain Point.") self.lineSegments: LineSegments = LineSegments([ LineSegment(points[i].clone(), points[i + 1].clone()) for i in range(0, len(points) - 1) ])
def __cutSegmentInTheWorstX(lineSegmentsX, theWorstX): #print("__cutSegmentInTheWorstX") # lineSegmentDontIntersect:list<LineSegment> lineSegmentIntersect = lineSegmentsX.lineSegmentsWhichIntersectAxisParallelToY( theWorstX) lineSegmentDontIntersect = lineSegmentsX.lineSegmentsWhichDontIntersectAxisParallelToY( theWorstX) # fncValue:float fncValue = random.uniform(0, 1) # lineSegmentsNew:list<LineSegment> lineSegmentsNew = list(lineSegmentDontIntersect) lineSegmentsNew.append( LineSegment(lineSegmentIntersect.point1, Point(theWorstX, fncValue))) lineSegmentsNew.append( LineSegment(Point(theWorstX, fncValue), lineSegmentIntersect.point2)) return LineSegments(lineSegmentsNew)
def createPointToPoint(points): if type(points) is not list: raise ValueError("Argument points isn't type list.") for pointI in points: if type(pointI) is not Point: raise ValueError("Argument pointI don't contain Point.") # List<LineSegment> lineSegments = [ LineSegment(points[i], points[i + 1]) for i in range(0, len(points) - 1) ] return LineSegments(lineSegments)
def createFromIntervals(intervals, functionValues): if len(intervals) != len(functionValues): raise ValueError( "Argument functionValues don't dont have correct length.") # List<LineSegment> lineSegments = [] # intervalI:Tuple<float, float>, fncValueI:float for intervalI, fncValueI in zip(intervals, functionValues): p1 = Point(intervalI[0], fncValueI) p2 = Point(intervalI[1], fncValueI) lineSegments.append(LineSegment(p1, p2)) return LineSegments(lineSegments)
def exportAsPrefFncY(self, linPrefModelConf): # lineSegments:LineSegments lineSegments = LineSegments.createFromIntervals( self.intervals, self.functionValues) lineSegments = LineSegments([ LineSegment(Point(s.point1.y, s.point1.x), Point(s.point2.y, s.point2.x)) for s in lineSegments.lineSegments ]) # pointsRevers:Point[] #pointsRevers = [Point(p.y, p.x) for p in lineSegments.exportPoints()] # PrefFncY #return PrefFncY(pointsRevers) return PrefFncY.createFromLineSegments(lineSegments.lineSegments)
def exportTheNearest(self, point, diameter): # pointIDsAndDistances:list<(float, float)> pointIDsAndDistances = [(pointWithIdI.pointID, LineSegment(pointWithIdI.point, point).size()) for pointWithIdI in self.pointsWithID] # pointIDsAndDistances:list<(float, float)> sortedPointIDsAndDistances = sorted(pointIDsAndDistances, key = lambda x: x[1]) # pointIDAndDistance pointIDAndDistance = sortedPointIDsAndDistances[0] # pointID:float pointID = pointIDAndDistance[0] # distance:float distance = pointIDAndDistance[1] if distance < diameter: return pointID return None
def search(self, pointsWithRatingTrain: List[PointWithRating], arguments: Arguments, linPrefModelConf: LinPrefModelConfiguration): print("LineaRegression") # argFitnessFnc:Argument argFitnessFnc: Argument = arguments.exportArgument(self.FITNESS_FNC) # fitnessFnc:Function fitnessFnc = argFitnessFnc.exportValueAsFnc() nDims: int = 2 configs: List[List[int]] = [[7, 4, 0], [7, 7, 1]] (preferenceFunctionsCoefs, agregationFunctionCoefs) = self.__trainModel(pointsWithRatingTrain, configs) N: int = 100 rangeN: List[float] = [i / N for i in list(range(0, N))] lineSegmentsDim: List[LineSegments] = [] for dimI in range(nDims): (interceptI, coeficientsNewI) = preferenceFunctionsCoefs[dimI] configI: List[int] = configs[dimI] y: List[float] = interceptI + np.dot( coeficientsNewI, np.array(getRegressors(*configI, rangeN))) pointsI: List[Point] = [ Point(float(xI), float(yI)) for (xI, yI) in list(zip(rangeN, y)) ] lineSegmentsDimI: LineSegments = LineSegments.createPointToPoint( pointsI).clone() lineSegmentsDim.append(lineSegmentsDimI) lineSegmentsX: LineSegments = lineSegmentsDim[0] lineSegmentsY: LineSegments = LineSegments([ LineSegment(Point(lsI.point1.y, lsI.point1.x), Point(lsI.point2.y, lsI.point2.x)) for lsI in lineSegmentsDim[1].lineSegments ]) prefFncX: PrefFncX = PrefFncX.createFromLineSegments( lineSegmentsX.lineSegments) prefFncX.transform(linPrefModelConf) prefFncY: PrefFncY = PrefFncY.createFromLineSegments( lineSegmentsY.lineSegments) prefFncY.transform(linPrefModelConf) wx: float = agregationFunctionCoefs[1][0] wy: float = agregationFunctionCoefs[1][1] wxNorm: float = float(wx / (wx + wy)) wyNorm: float = float(wy / (wx + wy)) #print("wx: " + str(wxNorm)) #print("wy: " + str(wyNorm)) aggrFnc: AggrFnc = AggrFnc([wyNorm, wxNorm]) upModel: UserProfileModel = UserProfileModel(prefFncX, prefFncY, aggrFnc) individual = IndividualUserProfileModel(upModel) # points:list<Point> points: List[Point] = [p.point for p in pointsWithRatingTrain] # rating:list<float> rating: List[float] = [float(p.rating) for p in pointsWithRatingTrain] # ratingsPredicted:list<float> ratingsPredicted: List[float] = individual.preferenceOfPointsInDC( points, linPrefModelConf) # fitnessRMSETrain:float fitnessRMSETrain: float = fitnessFnc(ratingsPredicted, rating) return IndividualEvaluated(individual, fitnessRMSETrain)
def getMorphismOfAggregationFncToDataCubeLines(prefFncX, prefFncY, aggrFnc, aggrLevel, linPrefModelConf): # aggrLineSegment:LineSegment aggrLineSegment = aggrFnc.exportAsLineSegment( Point(aggrLevel, aggrLevel), linPrefModelConf) # intervalsX:LineSegments intervalsX = prefFncX.exportIntervals() # intervalsY:LineSegments intervalsY = prefFncY.exportIntervals() # aggrFncPoints:Point[] aggrFncPoints = aggrFnc.exportAsLineSegment( Point(aggrLevel, aggrLevel), linPrefModelConf) # aggregation:LineSegment aggrFnc = LineSegment(aggrLineSegment.point1, aggrLineSegment.point2) # polygonLineSegments:LineSegment[] polygonLineSegments = [] for intervalXI in intervalsX.lineSegments: for intervalYI in intervalsY.lineSegments: minX = intervalYI.getMinX() maxX = intervalYI.getMaxX() minY = intervalXI.getMinY() maxY = intervalXI.getMaxY() # aggrFncII:LineSegment aggrFncII = aggrFnc.deleteFromMinusInfinityToX(minX) if aggrFncII == None: # print("MinusInfinityToX") continue aggrFncII = aggrFncII.deleteFromXToPlusInfinity(maxX) if aggrFncII == None: # print("FromXToPlusInfinity") continue aggrFncII = aggrFncII.deleteFromMinusInfinityToY(minY) if aggrFncII == None: # print("MinusInfinityToY") continue aggrFncII = aggrFncII.deleteFromYToPlusInfinity(maxY) if aggrFncII == None: # print("FromYToPlusInfinity") continue point1 = aggrFncII.point1 point2 = aggrFncII.point2 # print("point1.y=" + str(point1.y)) # intervalXI.printLineSegment(); x1 = intervalXI.intersectionWithTheAxisParallelToX(point1.y) y1 = intervalYI.intersectionWithTheAxisParallelToY(point1.x) x2 = intervalXI.intersectionWithTheAxisParallelToX(point2.y) y2 = intervalYI.intersectionWithTheAxisParallelToY(point2.x) # print("x1=" + str(x1) + " y1=" + str(y1)) # print("x2=" + str(x2) + " y2=" + str(y2)) if x1 == None or y1 == None or x2 == None or y2 == None: continue if intervalXI.isParalelToX(): lineA = intervalXI.exportLineParalelToX(y2) polygonLineSegments.append(lineA) continue if intervalYI.isParalelToY(): lineA = intervalYI.exportLineParalelToY(x2) polygonLineSegments.append(lineA) continue pLineSegmentI = LineSegment(Point(x1, y1), Point(x2, y2)) polygonLineSegments.append(pLineSegmentI) # print("polygonLineSegments: ", len(polygonLineSegments)) # polygonLineSegments:LineSegment[] return polygonLineSegments