Пример #1
0
 def findPath(self,a,start):
     
     tmp = start
     last = np.copy(tmp)
     end = False
     lim = self.game.dim-1
     while(end == False):
         if tmp[0] < lim   and a[tmp[0]+1,tmp[1]] < a[tmp[0],tmp[1]]and a[tmp[0]+1,tmp[1]] >= 0:
             last = numpy.copy(tmp)
             tmp = ar([tmp[0]+1,tmp[1]])
             
         elif tmp[0]>0 and a[tmp[0]-1,tmp[1]]< a[tmp[0],tmp[1]]and a[tmp[0]-1,tmp[1]] >= 0:
             last = np.copy(tmp)
             tmp = ar([tmp[0]-1,tmp[1]])            
             
         elif tmp[1] < lim and a[tmp[0],tmp[1]+1]< a[tmp[0],tmp[1]]and a[tmp[0],tmp[1]+1] >= 0:
             last = np.copy(tmp)
             tmp = ar([tmp[0],tmp[1]+1])
             
         elif tmp[1]>0 and a[tmp[0],tmp[1]-1]< a[tmp[0],tmp[1]] and a[tmp[0],tmp[1]-1] >= 0:
             last = np.copy(tmp)
             tmp = ar([tmp[0],tmp[1]-1])
         
         else:
             end=True
             
     dire = last - self.snake[0]
     return dire
def serial_thread(PORT, BAUD, qu):
    sensorvals = ar([0., 0., 0., 0.])
    line = ''
    sport = None
    SFLAG = 0

    while True:
        if (SFLAG == 0):
            try:
                sport = serial.Serial(PORT, BAUD)
                SFLAG = 1
            except:
                SFLAG = 0
        else:
            try:
                line = sport.readline().strip()
                strvals = line.split('\t')
                #print(line)
            except:
                SFLAG = 0
                sport.close()
            try:
                sensorvals = ar([float(i) for i in strvals])
                #print "Serial Thread: "+str(sensorvals)
                qu.append(sensorvals)
            except:
                pass
Пример #3
0
 def stringToDir(self,strg):
     if strg == 'left':
         return ar([-1,0])
     elif strg == 'up':
         return ar([0,1])
     elif strg == 'right':
         return ar([1,0])
     elif strg == 'down':
         return ar([0,-1])
Пример #4
0
 def calcAntenaVert(self):
     xy =self.posi * self.s + self.offset
     d = self.snake.direc
     rotMat = np.vstack([d,d[::-1]]) 
     
     v = ar([self.s,0.5*self.s])
     w = ar([self.s,-0.5*self.s])
     vv = xy + np.dot(rotMat,v)
     ww = xy + np.dot(rotMat,w)
     
     r = np.vstack([vv,xy,ww]).reshape(6)
     return list(r)
Пример #5
0
 def get_statistics(self,population,sample,get_distances=False,original_fitnesses=False):
     if original_fitnesses:
         original_fitnesses = ar(self.fitness_many(population))
     sample_fitnesses = ar(self.fitness_many(sample))
     if original_fitnesses == False:
         original_fitnesses = sample_fitnesses
     if get_distances:
         differences = sample_fitnesses - original_fitnesses
         distances = [distance.hamming(population[k],sample[k]) for k in range(len(sample))]
     else:
         differences = []
         distances = []
     return original_fitnesses,sample_fitnesses,differences,distances
Пример #6
0
 def draw(self):
     self.clear(self.viewport, globals.background)
     dirties=set(self.level.getDirtyLocations()).union(self.perception.getDirtyLocations())
     #dirties=[(i,j) for j in xrange(self.height) for i in xrange(self.width)]
     gray=globals.darkest_gray
     gray=ar((gray,gray,gray))
     white=1.0
     white=ar((white,white,white))
     npcs=[]
     for loc in dirties:
         if self.perception[loc].cover==1.0:
             self[loc].hue=copy(gray)
         else:
             self[loc].hue=copy(white)
             #obj = self.perception[loc].top().obj if self.perception[loc].top() else None
             #if isinstance(obj, Actor) and obj!=self.perception.actor:
             #   npcs.append((ar(loc), obj.facing, STATE_HUE_MAP[obj.state]))
     """
     for loc, facing, hue in npcs:
         print loc, facing, hue
         index=HEX_NEIGHBORS.index(facing)
         self[tuple(loc+facing)].hue+=hue
         self[tuple(loc+HEX_NEIGHBORS[index-1])].hue+=hue/2
         try:
             self[tuple(loc+HEX_NEIGHBORS[index+1])].hue+=hue/2
         except IndexError:
             self[tuple(loc+HEX_NEIGHBORS[0])].hue+=hue/2
     """
     for loc in dirties:
         try:
             element=self.perception[loc].top()
             #self[loc].image = globals.font.render(element.symbol,True,ar(map(max,map(min, self[loc].hue, MAX_COLOR), MIN_COLOR))*element.hue*(255,255,255))
             self[loc].image = element.obj.render(ar(map(max,map(min, self[loc].hue, MAX_COLOR), MIN_COLOR))*(255,255,255))
             if element.facing and element.obj != self.ctx.pc and (element.facing[0] or element.facing[1]):
                 h, w = globals.cell_height-1, globals.cell_width-1
                 pointlist = ()
                 vertical, horizontal = h*((element.facing[0] or element.facing[1])*0.5+0.5), w*((element.facing[1]-element.facing[0])*0.5+0.5)
                 print horizontal, w/2.0
                 if horizontal == w/2.0:
                     pointlist = ((horizontal-3, vertical-cmp(vertical, 1)*3), (horizontal, vertical), (horizontal+3, vertical-cmp(vertical,1)*3))
                 else:
                     pointlist = ((horizontal, vertical-cmp(vertical,1)*3), (horizontal, vertical), (horizontal-cmp(horizontal,1)*3, vertical))
                 draw.lines(self[loc].image, STATE_HUE_MAP[element.obj.state], False, pointlist, True)
         except AttributeError:
             pass
     pygame.display.update(super(GridView, self).draw(self.viewport))
     for tile in self.level.tiles:
         tile.dirty=0
     for tile in self.perception.tiles:
         tile.dirty=0
Пример #7
0
    def checkLBUBMix(self, opts, lb, ub):

        # these are indices to bound
        lbindices = (0,3)

        ubindices = {}
        ubindices["t"] = (3,6)
        ubindices["n"] = "a"
        ubindices["N"] = "a"
        ubindices["l"] = [3,4,5]
        ubindices["a"] = ar([3,4,5],dtype=uint)
        ubindices["f"] = ar([3,4,5],dtype=float64)

        ubvalues = {}
        ubvalues["s"] = ub
        ubvalues["l"] = [ub, ub, ub]
        ubvalues["a"] = ar([ub, ub, ub])

        lbvalues = {}
        lbvalues["s"] = lb
        lbvalues["l"] = [lb, lb, lb]
        lbvalues["a"] = ar([lb, lb, lb])

        lp = LP()

        lp.setLowerBound(lbindices, lbvalues[opts[1]])

        if opts[0] == "N":
            lp.getIndexBlock(ubindices["N"], 3)

        lp.setUpperBound(ubindices[opts[0]], ubvalues[opts[1]])

        lp.setObjective([1,1,1,-1,-1,-1])


        for num_times in range(2):  # make sure it's same anser second time
            lp.solve()

            self.assertAlmostEqual(lp.getObjectiveValue(), lb*3 - ub*3)

            v = lp.getSolution()

            self.assert_(len(v) == 6)
            self.assertAlmostEqual(v[0], lb)
            self.assertAlmostEqual(v[1], lb)
            self.assertAlmostEqual(v[2], lb)

            self.assertAlmostEqual(v[3], ub)
            self.assertAlmostEqual(v[4], ub)
            self.assertAlmostEqual(v[5], ub)
Пример #8
0
 def __init__(self,
              train_in,
              train_out,
              input_names=None,
              gaussian_radius=0.3):
     train_in = matrix(train_in, dtype=float)
     train_out = ar(train_out)
     order = train_out.argsort()
     train_in = train_in[order, ]
     train_out = train_out[order]
     inputs_num = train_in.shape[1]
     self._classes = unique(train_out,
                            return_index=True,
                            return_inverse=True,
                            return_counts=True)
     self._classes_num = len(self._classes[0])
     if input_names is None: input_names = [None for i in range(inputs_num)]
     self._input_layer = NeuronsLayer(
         [InputNeuron(name=input_names[i]) for i in range(inputs_num)])
     self._pattern_layer = NeuronsLayer([
         PatternNeuron(train_in[i, ].tolist()[0], gaussian_radius)
         for i in range(train_in.shape[0])
     ])
     self._summa_layer = NeuronsLayer([
         SummaNeuron(int(self._classes[3][i]))
         for i in range(self._classes_num)
     ])
     self._out_neuron = OutputNeuron(int(self._classes_num),
                                     self._classes[0].astype(str))
Пример #9
0
 def ForceSystem(self):
     XRotated = np.hstack([
         ar(self.Concrete.StressPolygon.Centroid[0]),
         self.Reinforcements.Coordinate.X
     ])
     YRotated = np.hstack([
         ar(self.Concrete.StressPolygon.Centroid[1]),
         self.Reinforcements.Coordinate.Y
     ])
     Compression = np.hstack([
         ar(self.Concrete.CompressionForce),
         self.Reinforcements.CompressionForce
     ])
     return force_system(
         coordinate(XRotated, YRotated).rotate(-self.Concrete.Angle),
         Compression, self.PnMax)
Пример #10
0
 def sin(self, val, degree=False):
     """Calculates SIN of a value and its errors"""
     if degree:
         val = self.deg2rad(val)
     ret = sin(val[0]) * cos(val[1])
     eret = sin(val[1]) * cos(val[0])
     return (ar([ret, eret]))
Пример #11
0
 def mean_read(self, number):
     self.logger.log("Calculating mean and standard deviation of ({}) samples".format(number))
     try:
         data = []
         c = 0
         self.con = self.connect()
         for i in range(number):
             d = self._read_data_()
             if d is not None:
                 data.append(d)
                 c += 1
             sleep(0.01)
             
         self.close()
         data = ar(data)
         mean_data = mean(data, axis=0)
         stdv_mag = std(data[:, 1])
         stdv_sqm_mag = std(data[:, 2])
         stdv_tmp = std(data[:, 3])
         return({"JD": mean_data[0], "Mag": mean_data[1], "STDV": stdv_mag,
                    "CMag": mean_data[2], "CSTDV": stdv_sqm_mag,
                    "Temperature": mean_data[3], "TemperatureSTDV": stdv_tmp,
                    "N": number, "Valid": c})
     except Exception as e:
         self.logger.log(e)
         self.close()
Пример #12
0
 def __equal_linear_altitude_slicer__(self, pieces):
     """Generates AltAz for given pieces"""
     self.logger.log("Calculating altitudes for {} pieces".format(pieces))
     try:
         return rad2deg(arccos(ar(range(0, pieces + 1)) / pieces))
     except Exception as excpt:
         self.logger.log(excpt)
Пример #13
0
 def findDirection(self):
     sm = self.makeScoreMap()
     dbm,cm = self.makeClimbMat(sm)
     bestBonIdx = self.findBestBonus(dbm)
     dire = self.snake.direc
     if bestBonIdx > -1:
         goTo = self.game.bonusList[bestBonIdx].posi
         dire = self.findPath(cm,goTo) 
     #~ Else, we pick randomly a point near the middle:
     else:
         found = False
         i = 0.05
         while (found == False and i < 0.5):                
             XY = np.random.normal((self.game.dim-1)/2,i*self.game.dim,2).astype(int)
             #~ Determines validity of the point:
             #~ The 3x3 area around is empty
             area = cm[XY[0]-1:XY[0]+2,XY[1]-1:XY[1]+2]
             
             t0 = (np.sum(area < 0) == 0)
             t1 = np.all(XY > 0)and np.all(XY < self.game.dim)
             
             test = t0 and t1
             if test:
                 dire = self.findPath(cm,XY)
                 found=True
             else:
                 i+=0.05
         #~ by default:
         
         if found == False:
             dire = ar([0,0])
             toCent = self.snake[0] - self.game.dim/2
             q = max(abs(toCent))
             if not q==0:
                 toCent = -toCent/q
                 if toCent[0] == toCent[1]:
                         if toCent[0] == 1:
                             toCent[0] = 0
                         if toCent[0] == -1:
                             toCent[0] = 0
                         if toCent[0] == 0:
                             toCent[0] = 1
                 dire = toCent
             else:
                 dire = ar([1,0])
     
     self.addDirAsKeyStack(dire)
Пример #14
0
 def readPolyline(self):
     """Lee solo dos tipos de linea LWPOLYLINE (2D) y POLYLINE (3D), y devuelve un array con el siguiente formato: \n
     -->  X, Y, Z, File, Layer, Atrib, IDx """
     count = 0
     s = 0
     t = 0
     tt = []
     File = self.NameFile
     PointResult = []
     Project = "Test_1"
     Period = 1
     for idx, Gpline in enumerate(self.Sheet.query('LWPOLYLINE')):
         Elev = Gpline.dxf.elevation
         Index = idx
         Layer = Gpline.dxf.layer
         Atrib1 = 'Cut' if Layer[:3] in {
             'Cut', "Abi", "POL"
         } else 'Road'  # Por lo pronto depende del nombre
         Element_type = 'Road' if Atrib1 == 'Road' else 'Cut' if Gpline.dxf.flags == 1 else 'aa'  # Por lo pronto depende del nombre
         Atrib = [
             Elev, File, Layer, Atrib1, Index, Element_type, Project, Period
         ]
         with Gpline.points() as Lines:
             for pp, point in enumerate(Lines):
                 count += 1
                 if Atrib[5] == 'aa':
                     tt.append(Atrib[2])
                     continue
                 else:
                     PointIter = list(point[:2]) + list(Atrib)
                     PointResult.append(PointIter)
         s = idx
     s += 1
     for idx, GGpoly in enumerate(self.Sheet.query('POLYLINE')):
         Index = idx + s
         Layer = GGpoly.dxf.layer
         Atrib1 = 'Cut' if Layer[:3] in {
             'Cut', "Abi", "POL"
         } else 'Road'  # Por lo pronto depende del nombre
         Element_type = 'Road' if Atrib1 == 'Road' else 'Cut' if Gpline.dxf.flags == 1 else 'bb'  # Por lo pronto depende del nombre
         Atrib = [File, Layer, Atrib1, Index, Element_type, Project, Period]
         for i, locations in enumerate(GGpoly.points()):
             count += 1
             if Atrib[5] == 'bb':
                 tt.append(Atrib[2])
                 continue
             else:
                 PointIter = next if Element_type == None else list(
                     locations) + Atrib
                 PointResult.append(PointIter)
     g = Conq.LoadData(File, Project)
     g.LoadRoads(PointResult)
     VecPoly = ar(PointResult)
     tt1 = list(np.unique(tt, return_counts=True)[0])
     print("Corte {} se encuentra abierto, por favor revisar".format(tt1))
     # TableResult = pd.DataFrame(data=VecPoly, columns=[
     #    "X", "Y", "Z", "File", "Layer", "Atrib", "IDx", "Element_type", "Project", "Period"])
     # TableResult.to_csv("Poly.csv")
     return VecPoly
Пример #15
0
 def save_numpy(self, src, arr, dm=" ", h=""):
     """Writes an array to a file"""
     self.logger.log("Writing to {0}".format(src))
     try:
         arr = ar(arr)
         savetxt(src, arr, delimiter=dm, newline='\n', header=h)
     except Exception as excpt:
         self.logger.log(excpt)
Пример #16
0
 def _vectorize(self, segment):
     if segment is None:
         return None
     seg=ar(segment)
     vector=seg[1]-seg[0]
     vector/=sqrt(sum(pow(vector,2)))
     seg[1]=vector
     return seg
Пример #17
0
 def write_array(self, src, arr, dm=" ", h=""):
     """Writes an array to a file"""
     self.logger.info("Writing to {0}".format(src))
     try:
         arr = ar(arr)
         savetxt(src, arr, delimiter=dm, newline='\n', header=h)
     except Exception as e:
         self.logger.error(e)
Пример #18
0
 def __equal_area_azimuth_slicer__(self, pieces):
     """Generates AltAz widenings"""
     self.logger.log(
         "Calculating number of areas for {} pieces".format(pieces))
     try:
         return 1 + ar(range(0, pieces)) * 2
     except Exception as excpt:
         self.logger.log(excpt)
Пример #19
0
 def create_population_with_unif_cross(self,sampled_population,population,cross_rate=0.1):
     new_population = []
     for i,p in enumerate(population):
         mask = np.where(np.random.binomial(1,cross_rate,len(p)) == 1)[0]
         crossed = ar(p)
         crossed[[mask]] = sampled_population[i][[mask]]
         new_population.append(crossed)
     return new_population
Пример #20
0
 def multiply(self, val1, val2):
     """Calculates multipication of two value and their errors"""
     try:
         ret = val1[0] * val2[0]
     except Exception as e:
         self.etc.print_if(e)
         ret = None
     try:
         eret = ret * power(
             power(val1[1] / val1[0], 2) + power(val2[1] / val2[0], 2), 0.5)
     except Exception as e:
         self.etc.print_if(e)
         eret = None
     if eret is not None:
         return (ar([ret, abs(eret)]))
     else:
         return (ar([ret, eret]))
Пример #21
0
 def _recognize(self, X):
     inp = self._input_layer.feed(X, False)
     pattern = ar(self._pattern_layer.feed(inp, True))
     summa_inp = list()
     for i in range(len(self._classes[0])):
         summa_inp.append(pattern[self._classes[2] == i])
     summa = self._summa_layer.feed(summa_inp, False)
     return self._out_neuron.feed(summa)
Пример #22
0
    def ar_sum_inner(self, in_arr):
        """Calculates sum of all values and their error given in an array
        Calls sum method to do so"""
        the_sum = in_arr[0]
        for i in in_arr[1:]:
            the_sum = self.sum(the_sum, i)

        return (ar(the_sum))
Пример #23
0
    def checkInconsistentSubarrays(self, opts):

        values = {}

        indices = {}
        indices["t"] = (0,3)
        indices["n"] = "a"
        indices["N"] = "a"
        indices["l"] = [0,1,2]
        indices["a"] = ar([0,1,2],dtype=uint)
        indices["f"] = ar([0,1,2],dtype=float64)
        indices["e"] = None  # empty

        A = [[1,0,  0],
             [0,1],      # inconsistent; does this get caught?
             [0,0.5,0]]

        values = {}
        values["L"] = A
        values["l"] = [ar(le) for le in A]
        values["B"] = [[1, 0, 0], [[1,0,0]], [0,1,1]]
        values["C"] = ones((1,3,3) )
        values["D"] = [[1, 0, 0], [1,1,[1]], [0,1,1]]
        values["E"] = [[1, 0, 0], (1,1,1), [0,1,1]]

        targets = {}
        targets["s"] = 1
        targets["l"] = [1,1,1]
        targets["a"] = ar([1,1,1],dtype=uint)
        targets["f"] = ar([1,1,1],dtype=float64)

        lp = LP()

        if opts[0] == "N":
            lp.getIndexBlock(indices["N"], 3)

        io = indices[opts[0]]
        vl = values [opts[1]]
        tr = targets[opts[2]]
        ob = [1,2,3]
        
        if io is None:
            self.assertRaises(ValueError, lambda: lp.addConstraint(vl, ">=", tr))
        else:
            self.assertRaises(ValueError, lambda: lp.addConstraint( (io, vl), ">=", tr))
Пример #24
0
    def equal_angle(self, n_alt, n_az):
        try:
            altitudes = linspace(0, 90, n_alt + 1)
            azimuths = linspace(0, 360, n_az + 1)

            ret = []

            for altitude in range(len(altitudes) - 1):
                for azimuth in range(len(azimuths) - 1):
                    ret.append(
                        ar([
                            altitudes[altitude:altitude + 2],
                            azimuths[azimuth:azimuth + 2]
                        ]))

            return ar(ret)
        except Exception as excpt:
            self.logger.log(excpt)
Пример #25
0
    def stdv(self, in_arr):
        """Calculates standard deviation of given values and their errors"""
        try:
            mean = self.mean(in_arr)
            the_subs = []
            for i in in_arr:
                the_subs.append(
                    self.minor.power(self.minor.sum(i, -1 * mean), 2))

            the_subs = ar(the_subs)
            sq_sum = self.minor.ar_sum_inner(the_subs)
            sq_sum_o_num = self.minor.multiply(sq_sum, [1 / len(the_subs), 0])
            ret = self.minor.power(sq_sum_o_num, 0.5)
        except Exception as e:
            self.etc.print_if(e)
            ret = ar([None, None])

        return (ret)
Пример #26
0
    def ar_multiply_inner(self, in_arr):
        """Calculates multipication of all values and 
        their error given in an array
        Calls multiply method to do so"""
        the_mul = in_arr[0]
        for i in in_arr[1:]:
            the_mul = self.multiply(the_mul, i)

        return (ar(the_mul))
Пример #27
0
    def addDirAsKeyStack(self,dire):
        dirStr=''
        
        if sum(dire == ar([-1,0],dtype=int)) == 2:
            dirStr="left"            
        elif sum(dire == ar([1,0],dtype=int)) == 2:
            dirStr="right"
        elif sum(dire == ar([0,-1],dtype=int)) == 2:
            dirStr= "down"
        elif sum(dire == ar([0,1],dtype=int)) == 2:
            dirStr="up" 
        if sum(dire == self.snake.direc) == 2: #avoid boost
            dirStr =''
            

        if dirStr != '':
           
            self.keyStack.append(dirStr)
Пример #28
0
    def traj_callback(self, traj):
        """stores the trajectory"""

        for i in range(len(traj.points)):
            posx = traj.points[i].transforms[0].translation.x
            posy = traj.points[i].transforms[0].translation.y
            posz = traj.points[i].transforms[0].translation.z

            velx = traj.points[i].velocities[0].linear.x
            vely = traj.points[i].velocities[0].linear.y
            velz = traj.points[i].velocities[0].linear.z

            accx = traj.points[i].accelerations[0].linear.x
            accy = traj.points[i].accelerations[0].linear.y
            accz = traj.points[i].accelerations[0].linear.z

            dirx = traj.points[i].accelerations[0].angular.x
            diry = traj.points[i].accelerations[0].angular.y
            dirz = traj.points[i].accelerations[0].angular.z
            # "time_from_start" in MultiDOFJointTrajectory is rospy.Duration, to_sec() is needed to convert that to seconds
            tt = traj.points[i].time_from_start.to_sec()

            self.despos.append(ar([[posx], [posy], [posz]]))
            self.desvel.append(ar([[velx], [vely], [velz]]))
            self.desacc.append(ar([[accx], [accy], [accz]]))
            self.desdirection.append(ar([[dirx], [diry],
                                         [dirz]]))  # a dummy direction for now
            self.trajectory_time.append(tt)

            self.controller = 0  # position controller
            f4 = open('trajectory_generated_subscribed.txt', 'a')
            f4.write(
                "%s, %s, %s, %s, %s, %s, %s, %s, %s, %s\n" %
                (tt, posx, posy, posz, velx, vely, velz, accx, accy, accz))
            if len(self.trajectory_time) > 100:
                self.trajectory_time.pop(0)
                self.despos.pop(0)
                self.desvel.pop(0)
                self.desacc.pop(0)
                self.desdirection.pop(0)

        #if len(self.trajectory_time) >2000:
        #    self.trajectory_time.pop(); self.despos.pop(); self.desvel.pop(); self.desacc.pop(); self.desdirection.pop()
        """
Пример #29
0
    def power(self, bs, pw):
        """Calculates power of a value to a constant and its errors"""
        try:
            ret = power(bs[0], pw)
            eret = ret * pw * (bs[1] / bs[0])
        except:
            ret = None
            eret = None

        return (ar([ret, abs(eret)]))
Пример #30
0
def fun(arg):
    if len(arg) > 1:
        scoreMat = arg[0]
        s0 = arg[1]
        bonus = np.zeros((1,1),dtype = int) #no used !
        out = np.zeros(scoreMat.shape,dtype=int)
        C_AI.Lee2(scoreMat,s0,bonus,out)
        return out
    else:
        return ar([0])
Пример #31
0
 def recognize(self, X, y=None):
     X = ar(X)
     ndims = len(X.shape)
     if (ndims == 1):
         res = [self._recognize(X)]
     elif (ndims == 2):
         res = list()
         prints = not y is None
         if prints:
             n = X.shape[0]
             i = 0
         for row in X:
             recognized = self._recognize(row)
             res.append(recognized)
             if prints:
                 print(f"{i+1}/{n}", y[i], "recognized as", recognized,
                       f"({+(recognized==y[i])})")
                 i += 1
     return ar(res)
Пример #32
0
 def background(self, data, isar=False):
     """background of data"""
     self.logger.log("Getting Background of data")
     try:
         bkg = Background(data)
         if isar:
             bkg = ar(bkg)
         return bkg
     except Exception as excpt:
         self.logger.log(excpt)
Пример #33
0
    def checkUB(self, opts, ub):

        # these are indices to bound
        indices = {}
        indices["t"] = (0,3)
        indices["N"] = "a"
        indices["l"] = [0,1,2]
        indices["a"] = ar([0,1,2],dtype=uint)
        indices["f"] = ar([0,1,2],dtype=float64)

        ubvalues = {}
        ubvalues["s"] = ub
        ubvalues["l"] = [ub, ub, ub]
        ubvalues["a"] = ar([ub, ub, ub])

        lp = LP()

        if opts[0] == "N":
            lp.getIndexBlock(indices["N"], 3)

        lp.setObjective([1,1,1,1,1,1])
        lp.addConstraint( ((3,6), [[1,0,0],[0,1,0],[0,0,1]]), "<=", 10)
        lp.setMaximize()

        lp.setLowerBound(indices[opts[0]], None)
        lp.setUpperBound(indices[opts[0]], ubvalues[opts[1]])

        for num_times in range(2):  # make sure it's same anser second time
            lp.solve()

            self.assertAlmostEqual(lp.getObjectiveValue(), ub*3 + 10 * 3)

            v = lp.getSolution()

            self.assert_(len(v) == 6)
            self.assertAlmostEqual(v[0], ub)
            self.assertAlmostEqual(v[1], ub)
            self.assertAlmostEqual(v[2], ub)

            self.assertAlmostEqual(v[3], 10)
            self.assertAlmostEqual(v[4], 10)
            self.assertAlmostEqual(v[5], 10)
Пример #34
0
    def checkUB(self, opts, ub):

        # these are indices to bound
        indices = {}
        indices["t"] = (0, 3)
        indices["N"] = "a"
        indices["l"] = [0, 1, 2]
        indices["a"] = ar([0, 1, 2], dtype=uint)
        indices["f"] = ar([0, 1, 2], dtype=float64)

        ubvalues = {}
        ubvalues["s"] = ub
        ubvalues["l"] = [ub, ub, ub]
        ubvalues["a"] = ar([ub, ub, ub])

        lp = LP()

        if opts[0] == "N":
            lp.getIndexBlock(indices["N"], 3)

        lp.setObjective([1, 1, 1, 1, 1, 1])
        lp.addConstraint(((3, 6), [[1, 0, 0], [0, 1, 0], [0, 0, 1]]), "<=", 10)
        lp.setMaximize()

        lp.setLowerBound(indices[opts[0]], None)
        lp.setUpperBound(indices[opts[0]], ubvalues[opts[1]])

        for num_times in range(2):  # make sure it's same anser second time
            lp.solve()

            self.assertAlmostEqual(lp.getObjectiveValue(), ub * 3 + 10 * 3)

            v = lp.getSolution()

            self.assert_(len(v) == 6)
            self.assertAlmostEqual(v[0], ub)
            self.assertAlmostEqual(v[1], ub)
            self.assertAlmostEqual(v[2], ub)

            self.assertAlmostEqual(v[3], 10)
            self.assertAlmostEqual(v[4], 10)
            self.assertAlmostEqual(v[5], 10)
Пример #35
0
def plot_pblh_diff(y_train, y_test, p_train, p_test, t, ax, bx):

    ax.cla()
    bx.cla()

    # PLOT TRAINING TIME SERIES
    ts_train = ar(len(y_train))  # TIME SERIES INPUTS
    ax.plot(ts_train, pblh_diff(y_train, p_train), 'b', lw=2)
    ax.set_ylim([0, 2])
    ax.set_ylabel('$|y_{PBLH}-p_{PBLH}|$ in km')
    ax.set_title('training data  ')

    # PLOT TESTING TIME SERIES
    ts_test = ar(len(y_test))
    bx.plot(ts_test, pblh_diff(y_test, p_test), 'g', lw=2)
    bx.set_ylim([0, 2])
    bx.set_ylabel('|y_{PBLH}-p_{PBLH}|$ in km')
    bx.set_title('test data : ')

    plt.pause(1.0 / 60.0)
Пример #36
0
    def one80(self):
        """Returns 180's value with 10^-9 error"""
        try:
            ret = 180
            eret = 0
        except Exception as e:
            self.etc.print_if(e)
            ret = None
            eret = None

        return (ar([ret, eret]))
Пример #37
0
def sphere_demo():
    center=np.array((0,0,0))
    r=1
    cs=sphere_CSTri(center=center,r=r)
    cs.center=center
    cs.r=r
    start_pt=ar([0,0,1])
    start_c=0.1
    cs.alpha_err=pi/18
    cs.gen_triangles(start_pt,start_c)
    plotc(cs,before=lambda :plot_sphere(center,r))
Пример #38
0
 def PnMax(self):
     FsAtCompStrainLim = np.full(
         self.Reinforcements.Fy.shape,
         self.Concrete.CompStrainLim * self.Reinforcements.E)
     Fs = np.min(ar([self.Reinforcements.Fy, FsAtCompStrainLim]), axis=0)
     Ps = np.sum(self.Reinforcements.As * Fs)
     Pc = self.Concrete.Abbr * self.Concrete.fc * (
         (self.Concrete.B) *
         (self.Concrete.H) - np.sum(self.Reinforcements.As))
     PnMax = (Pc + Ps) * self.Reinforcements.PhiPureC
     return PnMax
Пример #39
0
    def log(self, val):
        """Calculates natural logarithm of a value and its errors"""
        try:
            ret = log(val[0])
            eret = val[1] / val[0]
        except Exception as e:
            self.etc.print_if(e)
            ret = None
            eret = None

        return (ar([ret, abs(eret)]))
Пример #40
0
    def pi(self):
        """Returns Pi's value with 10^-9 error"""
        try:
            ret = 3.14159265
            eret = 10**-9
        except Exception as e:
            self.etc.print_if(e)
            ret = None
            eret = None

        return (ar([ret, eret]))
Пример #41
0
 def __init__(self, coord_blocks_triple):
     (x, y, self.blocksLOS)=coord_blocks_triple
     self.id=(x,y)
     self.d_2=x*x+y*y-x*y
     coord=Locus.T.dot((x,y,1))
     factor=sqrt(coord[0]*coord[0]+coord[1]*coord[1])*2.0
     n=ar((-coord[1]/factor, coord[0]/factor)) #rotate -pi/2 and scale producing the 'left' normal
     self.coord=coord[0:2]
     self.n=n
     self.cover_left=0.0
     self.cover_right=0.0
Пример #42
0
 def polygon(self, shape, points, rev=False):
     img = Image.new('L', shape, 0)
     ImageDraw.Draw(img).polygon(points, outline=1, fill=1)
     mask = ar(img)
     
     the_mask = mask == 1
     
     if rev:
         return(lnot(the_mask))
     else:
         return(the_mask)
Пример #43
0
def frame_converter(size, fold):
    c = 0
    matrices = []
    for d in ['house', 'lab', 'office']:
        for i in range(3):
            for h in ['Lhand', 'Rhand']:
                images = os.listdir('E:/CEDL/' + fold + '/' + d + '/' +
                                    str(i + 1) + '/' + h + '/')
                for j in range(len(images)):
                    matrix = imread(
                        'E:/CEDL/' + fold + '/' + d + '/' + str(i + 1) + '/' +
                        h + '/Image' + str(j + 1) + '.png', 'L')
                    matrix = imresize(matrix, size, 'nearest')
                    matrix = matrix / 255
                    matrices += [ar([matrix], dtype='float32')]
                    c += 1
                    if c % 100 == 0:
                        print(c)
    matrices = ar(matrices, dtype='float32')
    numpy.save('E:/matrices_' + fold + '.npy', matrices)
Пример #44
0
    def e(self):
        """Returns Euler's number's value with 10^-9 error"""
        try:
            ret = 2.71828182
            eret = 10**-9
        except Exception as e:
            self.etc.print_if(e)
            ret = None
            eret = None

        return (ar([ret, eret]))
Пример #45
0
def points_in_path(path):
    points = [(ar([0, 0]), 0)]
    current_count = 0
    for direction, steps in path:
        current_point = points[-1][0]
        for i in range(1, steps + 1):
            current_count += 1
            points.append((tuple(
                (current_point + direction * i).tolist()), current_count))

    points[0] = (tuple(points[0][0].tolist()), 0)
    return points
Пример #46
0
 def calculate_changes_in_fitness(self,population,number_of_trials):
     original_fitnesses = ar(self.fitness_many(population))
     print original_fitnesses.shape
     sample = [self.sample_dA([i]) for i in population]
     # print sample.shape
     sample_fitnesses = ar(self.fitness_many([j for j in sample]))
     # return original_fitnesses,sample,sample_fitnesses
     print sample_fitnesses.shape
     print sample_fitnesses[0:10]
     differences = sample_fitnesses - original_fitnesses
     distances = [[distance.hamming(population[k],sample[k]) for k in range(len(sample))]]
     # pdb.set_trace()
     for i in range(number_of_trials):
         print "trial:",i
         new_sample = [self.sample_dA([j]) for j in population]
         new_sample_fitnesses = ar(self.fitness_many([j for j in new_sample]))
         new_difference = new_sample_fitnesses - original_fitnesses
         sample_fitnesses = np.vstack((sample_fitnesses,new_sample_fitnesses))
         differences = np.vstack((differences,new_difference))
         distances.append([distance.hamming(population[k],sample[k]) for k in range(len(sample))])
     return sample_fitnesses,differences,distances
Пример #47
0
    def __hexagon_creator__(self, center, radus, ang_offset=0):
        """Creates corner points of a hexagon"""
        try:
            points = []
            for ang in range(0, 360, 60):
                angle = ang_offset + ang
                x = radus * cos(deg2rad(angle)) + center[1]
                y = radus * sin(deg2rad(angle)) + center[0]
                points.append([x, y])

            return ar(points)
        except Exception as excpt:
            self.logger.log(excpt)
Пример #48
0
 def ini_makePosiDirList(self):
     thr = self.dim / (len(self.players) * 5)
     center = ar([self.dim // 2, self.dim // 2], dtype=int)
     pdl = list()
     while len(pdl) < len(self.players):
         r = numpy.random.rand(2)
         XY = (numpy.around((center - self.dim // 10) * 2 * r) + self.dim // 10).astype(int)
         ok = True
         for j in pdl:  # check distance
             if sum(abs(XY - j[0])) < thr:
                 ok = False
         if ok:
             r = numpy.random.randint(4)
             if r == 0:
                 pdl.append((XY, ar([-1, 0])))
             elif r == 1:
                 pdl.append((XY, ar([0, 1])))
             elif r == 2:
                 pdl.append((XY, ar([1, 0])))
             elif r == 3:
                 pdl.append((XY, ar([0, -1])))
     return pdl
Пример #49
0
    def checkLB(self, opts, lb):

        # these are indices to bound
        indices = {}
        indices["t"] = (0,3)
        indices["N"] = "a"
        indices["l"] = [0,1,2]
        indices["a"] = ar([0,1,2],dtype=uint)
        indices["f"] = ar([0,1,2],dtype=float64)

        lbvalues = {}
        lbvalues["s"] = lb
        lbvalues["l"] = [lb, lb, lb]
        lbvalues["a"] = ar([lb, lb, lb])

        lp = LP()

        if opts[0] == "N":
            lp.getIndexBlock(indices["N"], 3)

        lp.setObjective([1,1,1,1,1,1])

        lp.setLowerBound(indices[opts[0]], lbvalues[opts[1]])

        for num_times in range(2):  # make sure it's same anser second time
            lp.solve()

            self.assertAlmostEqual(lp.getObjectiveValue(), lb*3)

            v = lp.getSolution()

            self.assert_(len(v) == 6)
            self.assertAlmostEqual(v[0], lb)
            self.assertAlmostEqual(v[1], lb)
            self.assertAlmostEqual(v[2], lb)

            self.assertAlmostEqual(v[3], 0)
            self.assertAlmostEqual(v[4], 0)
            self.assertAlmostEqual(v[5], 0)
Пример #50
0
 def experiment(self,name,no_trials=10,corruption_level=0.2):
     ensure_dir("results/autoencoder/".format(name))
     all_strings,good_strings=self.generate_good_strings(10000)
     self.train_dA(ar(good_strings),corruption_level=corruption_level)
     original_fitnesses = self.fitness_many(all_strings)
     f,d,dist = self.calculate_changes_in_fitness(all_strings,no_trials)
     data = {
     "original":original_fitnesses,
     "fitnesses_sampled":f,
     "differences_in_fitness":d,
     "distances":dist,
     "no_trials":no_trials,
     "corruption_level":corruption_level,
     "all_strings":all_strings,
     "good_strings":good_strings
     }
     pickle.dump(data,open("results/autoencoder/{0}.pkl".format(name),"wb"))
     return data
Пример #51
0
def rayPairFromFF(facing, fov):
    fov=2*pi-fov
    reflex=fov>=pi
    facing=Locus.T.dot((facing[0],facing[1],1.0))
    logger.debug('facing:'+str(facing)+' reflex: '+str(reflex))
    facing=-facing[0:2]
    #left, right = [ar((-facing[1]/2.0,facing[0]/2.0)), facing], [ar((facing[1]/2.0,-facing[0]/2.0)), facing]
    left, right = [ar((0,0)), facing], [ar((0,0)), facing]
    # rotation matrix = [[ cos -sin ] [ sin cos ]] , rotates COUNTERCLOCKWISE
    ltheta, rtheta = fov/2.0, -fov/2.0
    cos_theta=cos(rtheta)
    sin_ltheta=sin(ltheta)
    sin_rtheta=-sin_ltheta #sin is symmetrical
    rotate_left=ar(((cos_theta, sin_rtheta),(sin_ltheta, cos_theta)))
    rotate_right=ar(((cos_theta, sin_ltheta),(sin_rtheta, cos_theta)))
    #left, right = (rotate_left*matrix(left[0]).transpose(), rotate_left*matrix(left[1]).transpose()),(rotate_right*matrix(right[0]).transpose(), rotate_right*matrix(right[1]).transpose())
    left, right = (left[0], rotate_left*matrix(left[1]).transpose()),(right[0], rotate_right*matrix(right[1]).transpose())
    #left, right = (ar(left[0].transpose())[0], ar(left[1].transpose())[0]), (ar(right[0].transpose())[0],ar(right[1].transpose())[0])
    left, right = ar((left[0], ar(left[1].transpose())[0])), ar((right[0],ar(right[1].transpose())[0]))
    logger.debug("left:"+str(left)+" right:"+str(right))
    rp=RayPair(None, None, reflex)
    (rp._left, rp._right)=(left, right)
    return rp
Пример #52
0
 def get_statistics(self,population,sample):
     original_fitnesses = ar(self.fitness_many(population))
     sample_fitnesses = ar(self.fitness_many([j for j in sample]))
     differences = sample_fitnesses - original_fitnesses
     distances = [distance.hamming(population[k],sample[k]) for k in range(len(sample))]
     return original_fitnesses,sample_fitnesses,differences,distances
Пример #53
0
 def iterative_algorithm(
     self,
     name,
     pop_size=100,
     genome_length=20,
     lim_percentage=20,
     lim=20,
     trials=10,
     corruption_level=0.2,
     num_epochs=50,
     lr = 0.1,
     online_training=False,
     pickle_data=False,
     max_evaluations=200000,
     save_data=False,
     cross_rate=0.9):
     self.mask = np.random.binomial(1,0.5,genome_length)
     results_path = "results/autoencoder/{0}/".format(name)
     ensure_dir(results_path)
     trials = max_evaluations/pop_size
     population_limit = lim
     cross_rate = cross_rate
     if lim_percentage > 0:
         population_limit = int(pop_size*(lim_percentage/100.0))
         print "population_limit:",population_limit
         print "{0}*({1}/100.0) = {2}".format(pop_size,lim_percentage,int(pop_size*(lim_percentage/100.0)))
     fitfile = open("{0}fitnesses.dat".format(results_path),"w")
     self.dA = Deep_dA(n_visible=genome_length,n_hidden=[60])
     self.dA.build_dA(corruption_level)
     self.build_sample_dA_2(k=5)
     all_strings,good_strings=self.generate_good_strings(pop_size,genome_length,population_limit)
     self.train_dA(ar(good_strings),corruption_level=corruption_level,num_epochs=num_epochs,lr=lr,output_folder=results_path,iteration=0)
     # sampled_population = [self.sample_dA([i]) for i in self.get_new_population_rw(all_strings)]
     fit_p_pop = self.get_new_population_rw(all_strings)
     sampled_population = np.array(self.sample_dA(fit_p_pop[0:pop_size/5]),"b").reshape(-1,genome_length)
     print "len sampled:",sampled_population.shape
     new_population = self.create_population_with_unif_cross(sampled_population,fit_p_pop,cross_rate)
     # print "s:",sampled_population
     original_fitnesses,sample_fitnesses,differences,distances = self.get_statistics(all_strings,new_population)
     if save_data:
         self.save_population(sampled_population,0)
         self.save_training_data(good_strings,0)
         random_pop = [self.generate_random_string(genome_length) for z in range(1000)]
         print random_pop[0]
         sampled_r_pop = self.sample_dA(random_pop)
         self.save_sampled_random_population([r for r in sampled_r_pop],0)
         self.save_random_population(random_pop,0)
         self.save_pop_fitnesses(sample_fitnesses,"fitness_pop",0)
         self.save_pop_fitnesses(self.fitness_many(good_strings),"fitness_training_data",0)
     print "writing..."
     fitfile.write("{0},{1},{2},{3}\n".format(np.mean(original_fitnesses),np.min(original_fitnesses),np.max(original_fitnesses),np.std(original_fitnesses)))
     fitfile.write("{0},{1},{2},{3}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses),np.std(original_fitnesses)))
     print "{0},{1},{2}\n".format(np.mean(original_fitnesses),np.min(original_fitnesses),np.max(original_fitnesses))
     print "{0},{1},{2}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses))
     print "writing over"
     for iteration in range(0,trials):
         print "iteration:",iteration
         print "choosing pop:"
         population = self.get_new_population_rw(new_population,sample_fitnesses)
         population[0] = new_population[np.argmax(sample_fitnesses)]
         print "choosing pop over"
         if online_training == False:
             print "building model..."
             self.dA = Deep_dA(n_visible=genome_length,n_hidden=100)
             self.dA.build_dA(corruption_level)
             self.build_sample_dA()
         good_strings,good_strings_fitnesses=self.get_good_strings(population,population_limit,unique=True)
         # return self.get_good_strings(population,population_limit,unique=True)
         for f in good_strings_fitnesses:
             print "good_strings_fitnesses:",f
         self.train_dA(ar(good_strings),corruption_level=corruption_level,num_epochs=num_epochs,lr=lr,output_folder=results_path,iteration=iteration+1)
         print "sampling..."
         sampled_population = np.array(self.sample_dA(population[0:pop_size/5]),"b").reshape(-1,genome_length)
         print "sampled pop:",sampled_population.shape
         new_population = self.create_population_with_unif_cross(sampled_population,population,cross_rate)
         new_population[0:1] = good_strings[0:1]
         print "sampling over"
         print "getting_statistics..."
         original_fitnesses,sample_fitnesses,differences,distances = self.get_statistics(population,new_population)
         print "statistics over"
         fitfile.write("{0},{1},{2},{3}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses),np.std(sample_fitnesses)))
         fitfile.flush()
         print "{0},{1},{2}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses))
         print "best from previous:",self.fitness(new_population[np.argmax(sample_fitnesses)])
         print "np.argmax(sample_fitnesses):",np.argmax(sample_fitnesses)
         print "new_population[np.argmax(sample_fitnesses)]:",new_population[np.argmax(sample_fitnesses)]
         print "best fitnesses:",sorted(sample_fitnesses,reverse=True)[0:10]
         print new_population[np.argmax(sample_fitnesses)]
         print self.fitness(new_population[np.argmax(sample_fitnesses)])
         print "sample_fitnesses[np.argmax(sample_fitnesses)]:",sample_fitnesses[np.argmax(sample_fitnesses)]
         if save_data:
             print "saving stuff..."
             self.save_population(sampled_population,iteration+1)
             self.save_training_data(good_strings,iteration+1)
             random_pop = [self.generate_random_string(genome_length) for z in range(1000)]
             print random_pop[0]
             sampled_r_pop = self.sample_dA(random_pop)
             self.save_sampled_random_population([r for r in sampled_r_pop],iteration+1)
             self.save_random_population(random_pop,iteration+1)
             self.save_pop_fitnesses(sample_fitnesses,"fitness_pop",iteration+1)
             self.save_pop_fitnesses(good_strings_fitnesses,"fitness_training_data",iteration+1)
             print "saving over"
     fitfile.close()
Пример #54
0
 def __init__(self):
     self.blocks_los=False
     self.long_description=self.__doc__
     self.short_description=self.__doc__
     self.symbol=' '
     self.hue=ar((1.0,1.0,1.0))
Пример #55
0
 def __init__(self, loc):
     super(PseudoHexTile, self).__init__(loc)
     self.rect=pygame.sprite.Rect(((loc[1]-loc[0])*(globals.cell_width+int(globals.cell_width*globals.scale_horizontally*0.75)), (loc[1]+loc[0])*globals.cell_height/2),(globals.cell_width,globals.cell_height))
     self.hue=ar((0.0,0.0,0.0))
Пример #56
0
import math
import logging
import pygame
from pygame.rect import Rect
from pygame import draw
from copy import copy

import globals
from reality import Grid
from objects import State, Actor
from lie.reality import HEX_NEIGHBORS
from lie.context import Context as Context

logger=logging.getLogger(__name__)

MAX_COLOR=ar((1.0,1.0,1.0))
MIN_COLOR=ar((0.0,0.0,0.0))

STATE_HUE_MAP={
        State.Friendly: ar((0.2,1.0,0.2))*(255,255,255),
        State.Neutral: ar((0.2,0.2,1.0))*(255,255,255),
        State.Unaware: ar((1.0,1.0,1.0))*(255,255,255),
        State.Alert: ar((1.0,1.0,0.2))*(255,255,255),
        State.Hostile: ar((1.0,0.2,0.2))*(255,255,255)
        }

class RenderableTile(pygame.sprite.DirtySprite):
    __metaclass__ = abc.ABCMeta
    BLANK_IMAGE = None
    
    @abc.abstractmethod
Пример #57
0
    def checkBindSandwich(self, opts):

        idxlist = [{}, {}]

        idxlist[0]["t"] = (0,3)
        idxlist[0]["N"] = "a"
        idxlist[0]["l"] = [0,1,2]
        idxlist[0]["a"] = ar([0,1,2])
        idxlist[0]["r"] = ar([0,0,1,1,2,2])[::2]
        idxlist[0]["f"] = ar([0,1,2],dtype=float64)

        idxlist[1]["t"] = (3,6)
        idxlist[1]["n"] = "b"
        idxlist[1]["l"] = [3,4,5]
        idxlist[1]["a"] = ar([3,4,5])
        idxlist[1]["r"] = ar([3,3,4,4,5,5])[::2]
        idxlist[1]["f"] = ar([3,4,5],dtype=float64)

        lp = LP()

        if opts[0] == "N":
            self.assert_(lp.getIndexBlock(idxlist[0]["N"], 3) == (0,3) )

        # Now bind the second group

        lp.bindSandwich(idxlist[0][opts[0]], idxlist[1][opts[1]])

        if opts[2] == "u":
            lp.addConstraint( (idxlist[0][opts[0]], 1), ">=", 1)
        elif opts[2] == "l":
            lp.addConstraint( (idxlist[0][opts[0]], 1), "<=", -1)
            lp.setUnbounded(idxlist[0][opts[0]])
        else:
            assert False

        lp.setObjective( (idxlist[1][opts[1]], [1,2,3]) )
        lp.setMinimize()

        lp.solve()

        v = lp.getSolution()

        v0 = 1 if opts[2] == "u" else -1

        self.assert_(len(v) == 6, "len(v) = %d != 6" % len(v))
        self.assertAlmostEqual(v[0], v0)
        self.assertAlmostEqual(v[1], 0)
        self.assertAlmostEqual(v[2], 0)
        self.assertAlmostEqual(v[3], 1)
        self.assertAlmostEqual(v[4], 0)
        self.assertAlmostEqual(v[5], 0)

        if opts[0] in "nN" and opts[1] in "nN":

            d = lp.getSolutionDict()

            self.assert_(set(d.iterkeys()) == set(["a", "b"]))

            self.assertAlmostEqual(d["a"][0], v0)
            self.assertAlmostEqual(d["a"][1], 0)
            self.assertAlmostEqual(d["a"][2], 0)
            self.assertAlmostEqual(d["b"][0], 1)
            self.assertAlmostEqual(d["b"][1], 0)
            self.assertAlmostEqual(d["b"][2], 0)
Пример #58
0
    def checkBindEach(self, opts):

        idxlist = [{}, {}]

        idxlist[0]["t"] = (0,3)
        idxlist[0]["N"] = "a"
        idxlist[0]["l"] = [0,1,2]
        idxlist[0]["a"] = ar([0,1,2])
        idxlist[0]["r"] = ar([0,0,1,1,2,2])[::2]
        idxlist[0]["f"] = ar([0,1,2],dtype=float64)

        idxlist[1]["t"] = (3,6)
        idxlist[1]["n"] = "b"
        idxlist[1]["l"] = [3,4,5]
        idxlist[1]["a"] = ar([3,4,5])
        idxlist[1]["r"] = ar([3,3,4,4,5,5])[::2]
        idxlist[1]["f"] = ar([3,4,5],dtype=float64)

        lp = LP()

        if opts[0] == "N":
            self.assert_(lp.getIndexBlock(idxlist[0]["N"], 3) == (0,3) )

        # Now bind the second group
        if opts[2] == "g":
            self.assert_(
                lp.bindEach(idxlist[1][opts[1]], ">", idxlist[0][opts[0]])
                == [0,1,2])

        elif opts[2] == "l":
            self.assert_(
                lp.bindEach(idxlist[0][opts[0]], "<", idxlist[1][opts[1]])
                == [0,1,2])

        elif opts[2] == "e":
            self.assert_(
                lp.bindEach(idxlist[0][opts[0]], "=", idxlist[1][opts[1]])
                == [0,1,2])
        elif opts[2] == "E":
            self.assert_(
                lp.bindEach(idxlist[1][opts[1]], "=", idxlist[0][opts[0]])
                == [0,1,2])
        else:
            assert False

        # Forces some to be defined implicitly above to catch that case
        lp.addConstraint( (idxlist[0][opts[0]], 1), ">=", 1)

        lp.setObjective( (idxlist[1][opts[1]], [1,2,3]) )
        lp.setMinimize()

        lp.solve()

        v = lp.getSolution()

        self.assert_(len(v) == 6, "len(v) = %d != 6" % len(v))
        self.assertAlmostEqual(v[0], 1)
        self.assertAlmostEqual(v[1], 0)
        self.assertAlmostEqual(v[2], 0)
        self.assertAlmostEqual(v[3], 1)
        self.assertAlmostEqual(v[4], 0)
        self.assertAlmostEqual(v[5], 0)

        if opts[0] in "nN" and opts[1] in "nN":

            d = lp.getSolutionDict()

            self.assert_(set(d.iterkeys()) == set(["a", "b"]))

            self.assertAlmostEqual(d["a"][0], 1)
            self.assertAlmostEqual(d["a"][1], 0)
            self.assertAlmostEqual(d["a"][2], 0)
            self.assertAlmostEqual(d["b"][0], 1)
            self.assertAlmostEqual(d["b"][1], 0)
            self.assertAlmostEqual(d["b"][2], 0)
Пример #59
0
 def __init__(self,length = 0,posi = ar([0,0]) , direc = ar([0,1])):
     deque.__init__(self)
     self.ini_snake(length,posi,direc)
Пример #60
0
 def iterative_algorithm(
     self,
     name,
     pop_size=100,
     genome_length=20,
     lim_percentage=20,
     lim=20,
     trials=10,
     corruption_level=0.2,
     num_epochs=50,
     lr = 0.1,
     online_training=False,
     pickle_data=False,
     max_evaluations=200000,
     save_data=False):
     results_path = "results/autoencoder/{0}/".format(name)
     ensure_dir(results_path)
     trials = max_evaluations/pop_size
     population_limit = lim
     if lim_percentage > 0:
         population_limit = int(pop_size*(lim_percentage/100.0))
         print "population_limit:",population_limit
         print "{0}*({1}/100.0) = {2}".format(pop_size,lim_percentage,int(pop_size*(lim_percentage/100.0)))
     fitfile = open("{0}fitnesses.dat".format(results_path),"w")
     self.dA = dA(n_visible=genome_length,n_hidden=50)
     self.dA.build_dA(corruption_level)
     self.build_sample_dA()
     all_strings,good_strings=self.generate_good_strings(pop_size,genome_length,population_limit)
     self.train_dA(ar(good_strings),corruption_level=corruption_level,num_epochs=num_epochs,lr=lr,output_folder=results_path,iteration=0)
     # sampled_population = [self.sample_dA([i]) for i in self.get_new_population_rw(all_strings)]
     sampled_population = self.sample_dA(self.get_new_population_rw(all_strings))
     print "s:",sampled_population
     original_fitnesses,sample_fitnesses,differences,distances = self.get_statistics(all_strings,sampled_population)
     data = {
     "original":original_fitnesses,
     "fitnesses_sampled":sample_fitnesses,
     "differences_in_fitness":differences,
     "distances":distances
     }
     if pickle_data:
         pickle.dump(data,open("results/autoencoder/{0}_0.pkl".format(name),"wb"))
     if save_data:
         self.save_population(sampled_population,0)
         self.save_training_data(good_strings,0)
         random_pop = [self.generate_random_string(genome_length) for z in range(1000)]
         print random_pop[0]
         sampled_r_pop = self.sample_dA(random_pop)
         self.save_sampled_random_population([r for r in sampled_r_pop],0)
         self.save_random_population(random_pop,0)
         self.save_pop_fitnesses(sample_fitnesses,"fitness_pop",0)
         self.save_pop_fitnesses(self.fitness_many(good_strings),"fitness_training_data",0)
     print "writing..."
     fitfile.write("{0},{1},{2},{3}\n".format(np.mean(original_fitnesses),np.min(original_fitnesses),np.max(original_fitnesses),np.std(original_fitnesses)))
     fitfile.write("{0},{1},{2},{3}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses),np.std(original_fitnesses)))
     print "{0},{1},{2}\n".format(np.mean(original_fitnesses),np.min(original_fitnesses),np.max(original_fitnesses))
     print "{0},{1},{2}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses))
     print "writing over"
     for iteration in range(0,trials):
         print "choosing pop:"
         population = self.get_new_population_rw([z for z in sampled_population])
         print "choosing pop over"
         if online_training == False:
             print "building model..."
             self.dA = dA(n_visible=genome_length,n_hidden=50)
             self.dA.build_dA(corruption_level)
             self.build_sample_dA()
         good_strings,good_strings_fitnesses=self.get_good_strings(population,population_limit)
         for f in good_strings_fitnesses:
             print "good_strings_fitnesses:",f
         self.train_dA(ar(good_strings),corruption_level=corruption_level,num_epochs=num_epochs,lr=lr,output_folder=results_path,iteration=iteration+1)
         print "sampling..."
         sampled_population = self.sample_dA(population)
         print "sampling over"
         original_fitnesses,sample_fitnesses,differences,distances = self.get_statistics(population,sampled_population)
         data = {
         "original":original_fitnesses,
         "fitnesses_sampled":sample_fitnesses,
         "differences_in_fitness":differences,
         "distances":distances
         }
         if pickle_data:
             pickle.dump(data,open("results/autoencoder/{0}_{1}.pkl".format(name,iteration+1),"wb"))
         fitfile.write("{0},{1},{2},{3}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses),np.std(original_fitnesses)))
         fitfile.flush()
         print "{0},{1},{2}\n".format(np.mean(sample_fitnesses),np.min(sample_fitnesses),np.max(sample_fitnesses))
         if save_data:
             self.save_population(sampled_population,iteration+1)
             self.save_training_data(good_strings,iteration+1)
             random_pop = [self.generate_random_string(genome_length) for z in range(1000)]
             print random_pop[0]
             sampled_r_pop = self.sample_dA(random_pop)
             self.save_sampled_random_population([r for r in sampled_r_pop],iteration+1)
             self.save_random_population(random_pop,iteration+1)
             self.save_pop_fitnesses(sample_fitnesses,"fitness_pop",iteration+1)
             self.save_pop_fitnesses(good_strings_fitnesses,"fitness_training_data",iteration+1)
     fitfile.close()