Esempio n. 1
0
 def eval_single(self, ckt, options):
     """Used in plotting, when only 1 circuits needs to be simulated"""
     program = ckt.spice(options)
     thread = circuits.spice_thread(program)
     thread.start()
     thread.join(2*self.timeout)
     return thread.result
Esempio n. 2
0
    def rank_pool(self,pool):
        """Multithreaded version of self.rank, computes scores for whole pool"""
        results = [[None for c in xrange(len(self.spice_commands))] for i in xrange(self.pool_size)]
        threads = [None for i in xrange(THREADS)]

        for i in xrange(len(self.spice_commands)):
            for t in xrange(self.pool_size/THREADS):

                threads = [circuits.spice_thread(a.spice_input(self.spice_commands[i])) for a in pool[t*THREADS:t*THREADS+THREADS]]
                for thread in threads:
                    thread.start()
                for thread in threads:
                    thread.join()
                for e,thread in enumerate(threads):
                    results[THREADS*t+e][i] = thread.result
        new_pool = [None for i in xrange(self.pool_size)]
        for t in xrange(self.pool_size):
            new_pool[t] = [0,pool[t]]#(Score, Circuit)
            for i in xrange(len(self.spice_commands)):
                if results[t][i]==None or len(results[t][i].keys())==0:
                    new_pool[t][0]=inf
                    continue
                for k in results[t][i].keys():
                    new_pool[t][0]+=self._rank(results[t][i],i,k)

        return sorted(new_pool)
Esempio n. 3
0
 def eval_single(self, ckt, options):
     """Used in plotting, when only 1 circuits needs to be simulated"""
     program = ckt.spice(options)
     thread = circuits.spice_thread(program)
     thread.start()
     thread.join(2 * self.timeout)
     return thread.result
Esempio n. 4
0
 def evaluate(self,options):
     """Used in plotting, when only 1 circuits needs to be simulated"""
     program = self.spice_input(options)
     if ' n2 ' not in program:
         return None
     thread = circuits.spice_thread(self.spice_input(options))
     thread.start()
     thread.join(simulation_timeout)
     return thread.result
Esempio n. 5
0
    def rank_pool(self,pool):
        """Multithreaded version of self.rank, computes scores for whole pool"""
        results = [[None for c in xrange(len(self.spice_commands))] for i in xrange(self.pool_size)]
        threads = [None for i in xrange(THREADS)]

        lasterror = None
        for i in xrange(len(self.spice_commands)):
            errors = 0
            for t in xrange(self.pool_size/THREADS):

                threads = [circuits.spice_thread(a.spice_input(self.spice_commands[i])) for a in pool[t*THREADS:t*THREADS+THREADS]]
                for thread in threads:
                    thread.start()
                for thread in threads:
                    thread.join(simulation_timeout)
                    if thread.is_alive():
                        try:
                            thread.spice.terminate()
                        except OSError:#Thread died before we could kill it
                            pass
                        thread.join()

                for e,thread in enumerate(threads):
                    if thread.result==None:
                        errors+=1
                        lasterror = "Simulation timedout"
                    elif thread.result[1]=={}:
                        errors+=1
                        lasterror = thread.result[0]
                    else:
                        results[THREADS*t+e][i] = thread.result[1]
                        thread.result = None
                del threads
            if errors == self.pool_size:
                #All simulations failed
                raise SyntaxError("Simulation {} failed for every circuit.\nSpice returned {}".format(i,lasterror))

        #new_pool = [None for i in xrange(self.pool_size)]
        for t in xrange(self.pool_size):
            #new_pool[t] = [0,pool[t]]#(Score, Circuit)
            pool[t]=[0,pool[t]]
            for i in xrange(len(self.spice_commands)):
                if results[t][i]==None or len(results[t][i].keys())==0:
                    pool[t][0]=inf
                    continue
                for k in results[t][i].keys():
                    pool[t][0]+=self._rank(results[t][i],i,k,extra=pool[t][1].extra_value)
            pool[t]=tuple(pool[t])#Make immutable for reduction in memory

        return sorted(pool)
Esempio n. 6
0
    def rank_pool(self,pool):
        """Multithreaded version of self.rank, computes scores for whole pool"""
        try:
            scores  = [0]*len(pool)

            lasterror = None
            timeouts = 0
            for i in xrange(len(self.spice_commands)):
                errors = 0
                skipped = 0
                for t in xrange(len(pool)):
                    if scores[t]>=inf:
                        continue
                    thread = circuits.spice_thread(pool[t].spice(self.spice_commands[i]))
                    thread.start()
                    thread.join(self.timeout)
                    if thread.is_alive():
                        timeouts += 1
                        if self.generation==1:
                            if t<len(self.seed_circuits):
                                print "Seed circuit simulation timed out. Aborting."
                                print "Increase simulation timeout with 'timeout=?' command"
                                exit()
                        try:
                            thread.spice.terminate()
                        except OSError:#Thread died before we could kill it
                            pass

                    if thread==None:
                        #Something went wrong, just ignore it
                        scores[t]=inf
                        skipped+=1
                        continue
                    if thread.result==None:
                        #Error in simulation
                        errors+=1
                        lasterror = "Simulation timedout"
                        scores[t]=inf
                    elif thread.result[1]=={}:
                        #SPICE didn't return anything
                        errors+=1
                        lasterror = thread.result[0]
                        scores[t]=inf
                    else:
                        #Everything seems to be fine, calculate scores
                        if scores[t]<inf:
                            #Custom scoring function
                            if self.c_rank!=None:
                                scores[t]+=self.c_rank(thread.result[1],extra=pool[t].extra_value,circuit=pool[t])
                            #Default scoring function
                            if self.def_scoring:
                                for k in thread.result[1].keys():
                                    scores[t]+=self._rank(thread.result[1],i,k,extra=pool[t].extra_value,circuit=pool[t])
                        thread.result = None
                #Disable error reporting when size of pool is very low
                if errors + skipped == len(pool) and len(pool)>10:
                    #All simulations failed
                    raise SyntaxError("Simulation {} failed for every circuit.\nSpice returned {}".format(i,lasterror))
            #Don't increase timeout when there is only 1 circuit
            if len(pool) > 1:
                if timeouts != 0:
                    print '{} simulation(s) timed out'.format(timeouts)
                if timeouts > len(pool)/10:
                    if timeouts > len(pool)/2:
                        self.timeout *= 1.5
                        print "Increasing timeout length by 50%, to {}".format(self.timeout)
                    else:
                        self.timeout *= 1.25
                        print "Increasing timeout length by 25%, to {}".format(self.timeout)
            return [(scores[i],pool[i]) for i in xrange(len(pool))]
        except KeyboardInterrupt:
            return
Esempio n. 7
0
    def rank_pool(self, pool):
        """Multithreaded version of self.rank, computes scores for whole pool"""
        try:
            scores = [0] * len(pool)

            lasterror = None
            timeouts = 0
            for i in xrange(len(self.spice_commands)):
                errors = 0
                skipped = 0
                for t in xrange(len(pool)):
                    if scores[t] >= inf:
                        continue
                    thread = circuits.spice_thread(pool[t].spice(
                        self.spice_commands[i]))
                    thread.start()
                    thread.join(self.timeout)
                    if thread.is_alive():
                        timeouts += 1
                        if self.generation == 1:
                            if t < len(self.seed_circuits):
                                print "Seed circuit simulation timed out. Aborting."
                                print "Increase simulation timeout with 'timeout=?' command"
                                exit()
                        try:
                            thread.spice.terminate()
                        except OSError:  #Thread died before we could kill it
                            pass

                    if thread == None:
                        #Something went wrong, just ignore it
                        scores[t] = inf
                        skipped += 1
                        continue
                    if thread.result == None:
                        #Error in simulation
                        errors += 1
                        lasterror = "Simulation timedout"
                        scores[t] = inf
                    elif thread.result[1] == {}:
                        #SPICE didn't return anything
                        errors += 1
                        lasterror = thread.result[0]
                        scores[t] = inf
                    else:
                        #Everything seems to be fine, calculate scores
                        if scores[t] < inf:
                            #Custom scoring function
                            if self.c_rank != None:
                                scores[t] += self.c_rank(
                                    thread.result[1],
                                    extra=pool[t].extra_value,
                                    circuit=pool[t])
                            #Default scoring function
                            if self.def_scoring:
                                for k in thread.result[1].keys():
                                    scores[t] += self._rank(
                                        thread.result[1],
                                        i,
                                        k,
                                        extra=pool[t].extra_value,
                                        circuit=pool[t])
                        thread.result = None
                #Disable error reporting when size of pool is very low
                if errors + skipped == len(pool) and len(pool) > 10:
                    #All simulations failed
                    raise SyntaxError(
                        "Simulation {} failed for every circuit.\nSpice returned {}"
                        .format(i, lasterror))
            #Don't increase timeout when there is only 1 circuit
            if len(pool) > 1:
                if timeouts != 0:
                    print '{} simulation(s) timed out'.format(timeouts)
                if timeouts > len(pool) / 10:
                    if timeouts > len(pool) / 2:
                        self.timeout *= 1.5
                        print "Increasing timeout length by 50%, to {}".format(
                            self.timeout)
                    else:
                        self.timeout *= 1.25
                        print "Increasing timeout length by 25%, to {}".format(
                            self.timeout)
            return [(scores[i], pool[i]) for i in xrange(len(pool))]
        except KeyboardInterrupt:
            return