class Triangle(Polygon): #vertices = None nVertices = 3 _AttributesDict = Polygon._AttributesDict.copy() def __init__(self, *args, **kw): assert len(kw) == 0 and len(args) in (1, 3) Polygon.__init__(self, *args, **kw) __call__ = lambda self, *args, **kw: Triangle( [self.vertices[i](*args, **kw) for i in range(self.nVertices)]) _sides = lambda self: ooarray([sqrt(sum((p1- p2)**2), attachConstraints=False) for p1, p2 in \ ((self.vertices[1], self.vertices[2]), (self.vertices[2], self.vertices[0]), (self.vertices[0], self.vertices[1]))]) _angles = lambda self: ooarray([angle(p1, p2) for p1, p2 in \ ((self.vertices[1]-self.vertices[0], self.vertices[2]-self.vertices[0]), (self.vertices[2]-self.vertices[1], self.vertices[0]-self.vertices[1]), (self.vertices[0]-self.vertices[2], self.vertices[1]-self.vertices[2]))]) _area = lambda self: Polygon._area(self) if self._spaceDimension( ) is 2 else self._areaViaGeron() #_area = lambda self: Polygon._area(self) if self._spaceDimension() is 2 else self._areaViaHeightSideMult() _areaViaGeron = lambda self: sqrt(self.p * (self.p - self.sides[0]) * (self.p - self.sides[1]) * (self.p - self.sides[2]), attachConstraints=False) def _areaViaHeightSideMult(self): proj = self.vertices[0].projection( Line(self.vertices[1], self.vertices[2])) return 0.5 * proj.distance( self.vertices[0]) * self.vertices[1].distance(self.vertices[2]) _circumCircleRadius = lambda self: (self.sides[0] * self.sides[1] * self. sides[2]) / (4 * self.S) _inscribedCircleRadius = lambda self: self.S / self.p _incenter = lambda self: (self.vertices[0] * self.sides[0] + self.vertices[ 1] * self.sides[1] + self.vertices[2] * self.sides[2]) / self.P _orthocenter = lambda self: self.vertices[0].perpendicular(Line(self.vertices[1], self.vertices[2])) \ & self.vertices[1].perpendicular(Line(self.vertices[0], self.vertices[2])) # Eiler's theorem: 2 * vector(OM) = vector(MH) => O = M -0.5 MH = M - 0.5(H-M) = 1.5M - 0.5H _circumCircleCenter = lambda self: 1.5 * self.M - 0.5 * self.H _InscribedCircle = lambda self: Circle(self.I, self.r) _CircumCircle = lambda self: Circle(self.O, self.R)
def distance(self, *args, **kw): assert len(kw) == 0 and len(args) == 1 other = args[0] if isinstance(other, (list, tuple)): other = Point(other) if isinstance(other, (Line, Plane)): return self.distance(self.projection(other)) elif isinstance(other, ndarray): return sqrt(sum((self-other)**2), attachConstraints = False)
def distance(self, *args, **kw): assert len(kw) == 0 and len(args) == 1 other = args[0] if isinstance(other, (list, tuple)): other = Point(other) if isinstance(other, (Line, Plane)): return self.distance(self.projection(other)) elif isinstance(other, ndarray): return sqrt(sum((self - other)**2), attachConstraints=False)
class Polygon(Polytope): _AttributesDict = Polytope._AttributesDict.copy() def __init__(self, *args, **kw): Polytope.__init__(self, *args, **kw) _sides = lambda self: ooarray([sqrt(sum((p1-p2)**2), attachConstraints=False) \ for p1, p2 in \ [(self.vertices[i], self.vertices[i+1]) for i in range(self.nVertices-1)] \ + [(self.vertices[self.nVertices-1], self.vertices[0])]]) _perimeter = lambda self: sum(self.sides) _semiperimeter = lambda self: 0.5 * self.P _angles = lambda self: ooarray([angle(p1, p2) for p1, p2 in \ [(self.vertices[i-1]-self.vertices[i], self.vertices[i+1]-self.vertices[i]) for i in range(self.nVertices-1)]+\ [(self.vertices[self.nVertices-2]-self.vertices[self.nVertices-1], self.vertices[0]-self.vertices[self.nVertices-1])]]) def _area(self): D = self._spaceDimension() if isscalar(D) and D != 2: raise SpaceFuncsException( 'polygon area is not implemented for space dimension > 2 yet') x, y = self._coords(0), self._coords(1) x.append(x[0]) y.append(y[0]) x, y = ooarray(x), ooarray(y) return 0.5 * abs(sum(x[:-1] * y[1:] - x[1:] * y[:-1])) def plot(self, *args, **kw): if not pylabInstalled: raise SpaceFuncsException( 'to plot you should have matplotlib installed') #pylab.Line2D.__init__([self.vertices[i][0] for i in range(3)], [self.vertices[i][1] for i in range(3)]) #raise 0 pylab.plot([ self.vertices[i][0] for i in (arange(self.nVertices).tolist() + [0]) ], [ self.vertices[i][1] for i in (arange(self.nVertices).tolist() + [0]) ], *args, **kw) for i in range(3): self.vertices[i].plot() pylab.draw() __call__ = lambda self, *args, **kw: Polygon( [self.vertices[i](*args, **kw) for i in range(self.nVertices)])
objective = [ # name, tol, goal 'time', 0.005, 'min', 'cost', 0.005, 'min' ] # for solver sa handling of constraints is unimplemented yet # when your solver is interalg you can use nonlinear constraints as well # for nonlinear objective and constraints functions like arctan, abs etc should be imported from FuncDesigner from FuncDesigner import arctan, sqrt constraints = lambda value: (2 * value['time']**2 + 3 * sqrt(value[ 'cost']) < 10000, 8 * arctan(value['time']) + 15 * value['cost'] > 15) p = TSP(G, objective=objective, constraints=constraints) r = p.solve( 'interalg', nProc=2) # see http://openopt.org/interalg for more info on the solver # you can provide some stop criterion, # e.g. maxTime, maxCPUTime, fEnough etc, for example # r = p.solve('interalg', maxTime = 100, maxCPUTime=100) # also you can use p.manage() to enable basic GUI (http://openopt.org/OOFrameworkDoc#Solving) # it requires tkinter installed, that is included into PythonXY, EPD; # for linux use [sudo] easy_install tk or [sodo] apt-get install python-tk #r = p.manage('interalg')
G.add_edges_from(\ [(i,j,{'time': +(4.5*(cos(i)+cos(j)+1)**2 + abs(i - j)), 'cost': (0.2*i + 0.1*j)**2, 'way': 'bike'}) for i in range(int(N)) for j in range(int(N)) if i != j ]) objective = [ # name, tol, goal 'time', 0.005, 'min', 'cost', 0.005, 'min' ] # for solver sa handling of constraints is unimplemented yet # when your solver is interalg you can use nonlinear constraints as well # for nonlinear objective and constraints functions like arctan, abs etc should be imported from FuncDesigner from FuncDesigner import arctan, sqrt constraints = lambda value: ( 2 * value['time']**2 + 3 * sqrt(value['cost']) < 10000, 8 * arctan(value['time']) + 15*value['cost'] > 15 ) p = TSP(G, objective = objective, constraints = constraints) r = p.solve('interalg', nProc=2) # see http://openopt.org/interalg for more info on the solver # you can provide some stop criterion, # e.g. maxTime, maxCPUTime, fEnough etc, for example # r = p.solve('interalg', maxTime = 100, maxCPUTime=100) # also you can use p.manage() to enable basic GUI (http://openopt.org/OOFrameworkDoc#Solving) # it requires tkinter installed, that is included into PythonXY, EPD; # for linux use [sudo] easy_install tk or [sodo] apt-get install python-tk #r = p.manage('interalg')