Example #1
0
def optimize(nec_file_input, options):
    evaluator = NecFileEvaluator(nec_file_input, options)
    ins_sol_vec = None
    if options.seed_with_input:
        ins_sol_vec = evaluator.x
    try:
        if not options.local_search:
            de_plugin = None
            try:
                optimizer = DE.differential_evolution_optimizer(
                    evaluator,
                    population_size=options.de_np,
                    f=options.de_f,
                    cr=options.de_cr,
                    show_progress=1,
                    insert_solution_vector=ins_sol_vec,
                    max_iter=options.max_iter,
                    dither=options.de_dither)
                optimizer.run()
            except KeyboardInterrupt:
                evaluator.saveRestart(optimizer.population, optimizer.scores)
                raise
        else:
            from nec import simplex
            if not options.quiet: printOut("N=%d" % len(evaluator.x))
            evaluator.x = simplex.fmin(evaluator,
                                       ftol=options.local_search_tolerance,
                                       xtol=options.local_search_tolerance,
                                       maxfun=options.max_iter)
        #evaluator.nec_file.writeNecInput("final.nec")
        evaluator.evaluateFinalSolution()
    except KeyboardInterrupt:
        evaluator.evaluateFinalSolution(1)
    finally:
        evaluator.join()
Example #2
0
def parseLogFile(filename, np, min_distance, min_dif,num_calculated):
	f = open(filename,"rt")
	lines = f.readlines()
	f.close()
	i=-1
	for j in xrange(len(lines)):
		if lines[j].find("Score")!=-1:
			i=j
	
	if i==-1:
		return ()

	printOut( i )

	vars = lines[i].split()
	lines = lines[i+1:]
	for l in range(len(lines)):
		try:
			lines[l] = map(float, lines[l].split())
		except:
			lines[l] = lines[l].replace("None", "0")
			lines[l] = map(float, lines[l].split())

	lines.sort()
	population = []

	for l in lines:
		if isAway(population, l, min_distance, min_dif,num_calculated):
			population.append(l)
			if len(population) == np:
				break
	return vars, population
Example #3
0
	def monitor(self):
		while not self.stop:
			sleep(1)
			try:
				self.lock.acquire()
				max_time = self.max_time
				processes = dict(self.processes)
				killed = dict(self.killed)
				count = self.count
			finally:
				self.lock.release()
			c = clock()
			for p in processes.keys():
				diff = c - processes[p]
				if ( count > 100 and diff > 10*max_time or diff > self.max_run_time ) and not p in killed:
					try:
						self.lock.acquire()
						self.killed[p] = c
					finally:
						self.lock.release()
					try:
						printOut("Killing hanging engine\n")
						p.kill()
					except:
						pass
Example #4
0
 def __init__(self, nec_file_input, options):
     self.process_monitor = None
     self.options = options
     self.nec_file_input = nec_file_input
     self.wire_structure = WireStructure(options)
     if options.engine_takes_cmd_args == 'yes' or options.engine_takes_cmd_args == 'auto' and os.name != 'nt':
         self.options.engine_takes_cmd_args = 1
     else:
         self.options.engine_takes_cmd_args = 0
     if self.options.input:
         self.html_output = HtmlOutput(self.nec_file_input.input)
         try:
             if self.options.param_values_file:
                 self.parseParameterValues(self.options.param_values_file)
                 self.writeParametrized("output.nec")
                 printOut(
                     "Using parameters from file: %s (see output.nec)\n" %
                     self.options.param_values_file)
         except (AttributeError, IOError):
             pass
     self.nec_file_input.autoSegmentation(self.options.auto_segmentation)
     self.prepareSweeps()
     if self.options.debug:
         printOut("Engine jobs:")
         pprint.pprint(self.sweeps)
Example #5
0
def main():
    #default values
    try:
        options, inputs = optionParser().parse_args()
        nec_file_input = NecInputFile(options.input, options.debug)
        if "EVAL" in nec_file_input.cmd_options:
            import shlex
            options, args = optionParser().parse_args(
                shlex.split(nec_file_input.cmd_options["EVAL"]))
        run(nec_file_input, options)
    except InputError as e:
        printOut(e)
        return
    for inp in inputs:
        if inp[0] != "-":
            options.input = inp
            try:
                nec_file_input = NecInputFile(options.input, options.debug)
                if "EVAL" in nec_file_input.cmd_options:
                    import shlex
                    options, args = optionParser().parse_args(
                        shlex.split(nec_file_input.cmd_options["EVAL"]))
                options.input = inp
                run(nec_file_input, options)
            except:
                traceback.print_exc()
                pass
Example #6
0
def main():
    random.seed()
    options, inputs = optionParser().parse_args()
    try:
        nec_file_input = NecInputFile(options.input, options.debug)
        if "OPT" in nec_file_input.cmd_options:
            import shlex
            options, args = optionParser().parse_args(
                shlex.split(nec_file_input.cmd_options["OPT"]))
        options.agt_correction = not options.noagt_correction
        options.angle_step = nec_file_input.angle_step
        if options.log_file == "":
            options.log_file = options.input + ".opt_log"
        if options.profile:
            import cProfile
            cProfile.runctx('optimize(nec_file_input, options)', globals(),
                            locals())
        else:
            optimize(nec_file_input, options)
    except InputError as e:
        printOut(e)
        return
Example #7
0
	def printFreqs(self, header=1):
		lines = []
		if not self.options.frequency_data:
			if header: 
				l = " %6s%14s%6s%6s%14s%16s%11s"%(" ", "--- Gain ---", " ", " ", "-- Ratios --", "-- Impedance --", " ")
				printOut( l )
				lines.append(l)
				l = " %6s%7s%7s%6s%6s%7s%7s%8s%8s%5s%6s"%("Freq", "Raw", "Net", "SWR", "BeamW", "F/R", "F/B", "Real", "Imag", "AGT", "corr")
				printOut( l )
				lines.append(l)
				l = "=========================================================================="
				printOut( l)
				lines.append(l)
			#if self.agt!=0:
			#	printOut( "AGT=%g dB"%self.agt)
			for i in self.frequencies:
				if not i.valid():
					l = "%6.1f - invalid result"%i.freq
					printOut( l)
					lines.append(l)
				else:
					rear = "n/a"
					back = "n/a"
					raw = "n/a"
					net = "n/a"
					beam_width = "n/a"
					if self.options.calc.gain:
						raw = i.horizontalRaw(self.options.forward_dir)
						net = i.net(raw)
						if self.options.calc.f2r:
							rear = i.rearGain(self.options.rear_angle,self.options.backward_dir)
							rear = "% 7.2f"%(net-rear)
						if self.options.calc.f2b:
							back = i.backwardGain(self.options.backward_dir)
							back = "% 7.2f"%(net-back)
						raw = "% 7.2f"%raw
						net = "% 7.2f"%net
						if self.options.calc.beam_width:
							beam_width = "% 6.1f"%i.beamWidth(self.options.forward_dir, self.options.angle_step, self.options.beamwidth_ratio)
					l = " % 6.1f% 7s% 7s% 6.2f% 6s% 7s% 7s% 8.2f% 8.2f%5.2f% 6.2f"%(i.freq, raw, net,i.swr(), beam_width, rear, back, i.real, i.imag, i.AGT, i.agt)
					printOut( l)
					lines.append(l)

		else:
			if header: 
				l = " %6s%7s%28s%6s%16s%12s"%(" ", " ", "- - - - - - Gain - - - - - -", " ", "-- Impedance --", " ")
				printOut( l )
				lines.append(l)
				l = " %6s%7s%7s%7s%7s%7s%6s%8s%8s%5s%6s"%("Freq", "Angle", "Goal", "Diff", "Raw", "Net", "SWR", "Real", "Imag", "AGT", "corr")
				printOut( l)
				lines.append(l)
				l = "============================================================================"
				printOut( l)
				lines.append(l)
			for i in self.frequencies:
				if not i.valid():
					l = " %6.4g - invalid result"%i.freq
					printOut( l)
					lines.append(l)
				else:
					target = self.options.frequency_data[i.freq][1]
					l = " % 6.1f% 7.1f% 7.2f% 7.2f% 7.2f% 7.2f% 6.2f% 8.2f% 8.2f%5.2f% 6.2f"%(i.freq, i.angle, target, target-i.net(), i.gain, i.net(),i.swr(), i.real, i.imag, i.AGT, i.agt)
					printOut( l)
					lines.append(l)
		return lines
Example #8
0
	for l in lines:
		if isAway(population, l, min_distance, min_dif,num_calculated):
			population.append(l)
			if len(population) == np:
				break
	return vars, population




if __name__ == "__main__":
	import optparse 
	options = optparse.OptionParser()
	options.add_option("--de-np", default="50", type="int")
	options.add_option("-l", "--log-file", default="opt.log")
	options.add_option("-d", "--min-distance", default="10", type="int")
	options.add_option("-f", "--min-dif", default=".1", type="float")
	options.add_option("-n", "--num-calculated", default="8", type="int")
	opts, args = options.parse_args()
	p = parseLogFile(opts.log_file, opts.de_np, opts.min_distance, opts.min_dif,opts.num_calculated)
	if not p:
		printOut( "Failed to extract population")
	else:
		vars, population = p
		printOut( ((len(vars))*"%9s ")%tuple(vars) )
		for i in range(len(population)):
			printOut( ((len(vars))*"%3.5f ")%tuple(population[i]) )


	
Example #9
0
def fmin(evaluator,
         xtol=1e-4,
         ftol=1e-4,
         maxiter=None,
         maxfun=None,
         full_output=0,
         disp=1,
         callback=None):
    """Minimize a function using the downhill simplex algorithm.

    Parameters
    ----------
    func : callable func(x,*args)
        The objective function to be minimized.
    x0 : ndarray
        Initial guess.
    args : tuple
        Extra arguments passed to func, i.e. ``f(x,*args)``.
    callback : callable
        Called after each iteration, as callback(xk), where xk is the
        current parameter vector.

    Returns
    -------
    xopt : ndarray
        Parameter that minimizes function.
    fopt : float
        Value of function at minimum: ``fopt = func(xopt)``.
    iter : int
        Number of iterations performed.
    funcalls : int
        Number of function calls made.
    warnflag : int
        1 : Maximum number of function evaluations made.
        2 : Maximum number of iterations reached.
    allvecs : list
        Solution at each iteration.

    Other Parameters
    ----------------
    xtol : float
        Relative error in xopt acceptable for convergence.
    ftol : number
        Relative error in func(xopt) acceptable for convergence.
    maxiter : int
        Maximum number of iterations to perform.
    maxfun : number
        Maximum number of function evaluations to make.
    full_output : bool
        Set to True if fval and warnflag outputs are desired.
    disp : bool
        Set to True to print convergence messages.
    
    Notes
    -----
    Uses a Nelder-Mead simplex algorithm to find the minimum of
    a function of one or more variables.

    """
    fcalls, func = wrap_function(evaluator.target)
    x0 = evaluator.x
    #x0 = asfarray(x0).flatten()
    N = len(x0)
    if maxiter is None:
        maxiter = N * 200
    if maxfun is None:
        maxfun = N * 200

    rho = 1
    chi = 2
    psi = 0.5
    sigma = 0.5
    one2np1 = range(1, N + 1)

    sim = []
    fsim = [.0] * (N + 1)
    for i in range(0, N + 1):
        sim.append([.0] * (N + 1))

    sim[0] = x0

    fsim[0] = func(x0)
    nonzdelt = 0.05
    zdelt = 0.00025
    for k in range(0, N):
        y = list(x0)
        if y[k] != 0:
            y[k] = (1 + nonzdelt) * y[k]
        else:
            y[k] = zdelt

        sim[k + 1] = y
        f = func(y)
        fsim[k + 1] = f

    ind = sort_permutation(fsim)
    fsim = apply_permutation(fsim, ind)
    # sort so sim[0,:] has the lowest function value
    sim = apply_permutation(sim, ind)
    evaluator.x = sim[0]

    iterations = 1

    while (fcalls[0] < maxfun and iterations < maxiter):
        sim_size = max(
            map(lambda x: max(map(abs, map(operator.sub, x, sim[0]))),
                sim[1:]))
        #print "The simplex size is %.6g(tol=%.6g)"%(sim_size,xtol)
        fsim_size = max(map(lambda x: abs(x - fsim[0]), fsim[1:]))
        #print "The simplex image size is %.6g(tol=%.6g)"%(fsim_size, ftol)
        if ( sim_size <= xtol ) \
           and fsim_size <=ftol:
            break
#        if (max(numpy.ravel(abs(sim[1:]-sim[0]))) <= xtol \
#            and max(abs(fsim[0]-fsim[1:])) <= ftol):
#            break

        xbar = averageArrays(sim[:-1])
        xr = linearCombine((1 + rho), xbar, -rho, sim[-1])
        fxr = func(xr)
        doshrink = 0

        if fxr < fsim[0]:
            xe = linearCombine((1 + rho * chi), xbar, -rho * chi, sim[-1])
            fxe = func(xe)

            if fxe < fxr:
                sim[-1] = xe
                fsim[-1] = fxe
            else:
                sim[-1] = xr
                fsim[-1] = fxr
        else:  # fsim[0] <= fxr
            if fxr < fsim[-2]:
                sim[-1] = xr
                fsim[-1] = fxr
            else:  # fxr >= fsim[-2]
                # Perform contraction
                if fxr < fsim[-1]:
                    xc = linearCombine((1 + psi * rho), xbar, -psi * rho,
                                       sim[-1])
                    fxc = func(xc)

                    if fxc <= fxr:
                        sim[-1] = xc
                        fsim[-1] = fxc
                    else:
                        doshrink = 1
                else:
                    # Perform an inside contraction
                    xcc = linearCombine((1 - psi), xbar, psi, sim[-1])
                    fxcc = func(xcc)

                    if fxcc < fsim[-1]:
                        sim[-1] = xcc
                        fsim[-1] = fxcc
                    else:
                        doshrink = 1

                if doshrink:
                    for j in one2np1:
                        sim[j] = linearCombine((1 - sigma), sim[0], sigma,
                                               sim[j])
                        fsim[j] = func(sim[j])

        ind = sort_permutation(fsim)
        sim = apply_permutation(sim, ind)
        fsim = apply_permutation(fsim, ind)
        evaluator.x = sim[0]
        if callback is not None:
            callback(sim[0])
        iterations += 1

    x = sim[0]
    fval = min(fsim)
    warnflag = 0

    if fcalls[0] >= maxfun:
        warnflag = 1
        if disp:
            printOut("Warning: Maximum number of function evaluations has "\
                  "been exceeded.")
    elif iterations >= maxiter:
        warnflag = 2
        if disp:
            printOut("Warning: Maximum number of iterations has been exceeded")
    else:
        if disp:
            printOut("Optimization terminated successfully.")
            printOut("         Current function value: %f" % fval)
            printOut("         Iterations: %d" % iterations)
            printOut("         Function evaluations: %d" % fcalls[0])

    if full_output:
        retlist = x, fval, iterations, fcalls[0], warnflag
    else:
        retlist = x

    return retlist
Example #10
0
						crs+=1
				cr_stats.append(crs)
			population.append(new_gen)

	return (vars,scores, population,cr_stats)


if __name__ == "__main__":
	import optparse 
	options = optparse.OptionParser()
	options.add_option("--de-np", default="50", type="int")
	options.add_option("-l", "--log-file", default="opt.log")
	options.add_option("-n", "--number-of-lines", default=0,type="int")
	options.add_option("-m", "--member", default=0, type="int")
	options.add_option("-s", "--cr-stats", default=False, action="store_true")
	opts, args = options.parse_args()
	p = parseLogFile(opts.log_file, opts.de_np, opts.member, opts.number_of_lines)
	if not p:
		printOut( "Failed to extract history")
	else:
		vars,scores, population, cr_stats = p
		printOut( ((len(vars)+1)*"%9s ")%tuple(["Score"]+vars))
		for i in range(len(scores)):
			if opts.cr_stats:
				printOut( ( (len(vars)+1)*"%3.5f "+" cr=%d")%tuple([scores[i]]+population[i]+[cr_stats[i]]))
			else:
				printOut( ( (len(vars)+1)*"%3.5f ")%tuple([scores[i]]+population[i]) )


	
Example #11
0
    def evaluate(self):
        NOP = NecOutputParser
        results = self.runSweeps()  #[[174,6,8],[470,6,40]]
        h = {}
        v = {}
        res = []
        res_lines = []
        printOut("Input file : %s" % self.options.input)
        printOut("Freq sweeps: %s" % str(self.options.sweeps))
        if self.nec_file_input.autosegment[0]:
            printOut("Autosegmentation: %d per %g" %
                     self.nec_file_input.autosegment)
        else:
            printOut("Autosegmentation: NO")
        printOut("\n")

        self.options.angle_step = self.nec_file_input.angle_step
        for r in range(len(results)):
            nop = NOP(results[r][0], results[r][2], self.options)
            if self.options.debug > 1:
                for f in nop.frequencies:
                    printOut(f.horizontal)
            h.update(nop.horizontalPattern())
            v.update(nop.verticalPattern())
            res_lines = res_lines + nop.printFreqs(r == 0)
            res = res + nop.getGainSWRChartData()

        if self.options.html:
            #			if self.options.horizontal_gain:
            if not self.options.frequency_data:
                self.html_output.addHPattern(self.options.sweeps, h)


#			else:
#				self.html_output.addVPattern(v)
            self.html_output.addResults("\n".join(res_lines))
            self.html_output.addNec(self.nec_file_input.parametrizedLines())
            if not self.options.frequency_data:
                self.html_output.addGainChart(self.options.sweeps, res,
                                              self.options.char_impedance)
            self.html_output.writeToFile(self.options.input + ".html",
                                         self.options.publish)
Example #12
0
 options.add_option("-N",
                    "--output-line-numbers",
                    default=False,
                    action="store_true")
 options.add_option("-c", "--generation-count", default=0, type="int")
 options.add_option("-r",
                    "--restart-file",
                    default=False,
                    action="store_true")
 options.add_option("-s", "--stats", default=False, action="store_true")
 opts, args = options.parse_args()
 p = parseLogFile(opts.log_file, opts.full, opts.number_of_lines,
                  opts.generation_count)
 if opts.progress_only: exit(0)
 if not p:
     printOut("Failed to extract population")
 else:
     vars, scores, population = p
     if not opts.restart_file:
         if opts.output_line_numbers:
             printOut(("Num    " + (len(vars) + 1) * "%9s ") %
                      tuple(["Score"] + vars))
             for i in range(len(scores)):
                 printOut(("[%03d]. " + (len(vars) + 1) * "%3.5f ") %
                          tuple([i] + [scores[i]] + population[i]))
         else:
             printOut(((len(vars) + 1) * "%9s ") % tuple(["Score"] + vars))
             for i in range(len(scores)):
                 printOut(((len(vars) + 1) * "%3.5f ") %
                          tuple([scores[i]] + population[i]))
     if opts.restart_file:
Example #13
0
        def parse_args(self, extra_args=[]):
            options, args = ne.OptionParser.parse_args(self, extra_args)
            if not options.sweeps: return (options, args)
            if options.target_function:
                options.target_function = options.target_function.strip('"')
                options.target_function = options.target_function.strip("'")
            if options.target_levels:
                options.target_levels = self.convertToListOfLists(
                    list(map(eval, options.target_levels)))
            options.parameters = options.parameters.replace(',', ' ').split()
            if options.output_population:
                sys.stderr.write("WARNING: --output-population is IGNORED.\n")
                options.output_population = False
            if options.quiet and options.verbose:
                sys.stderr.write(
                    "WARNING: Both quite and verbose mode specified. Will use verbose.\n"
                )
                options.quiet = False
            if options.parameters:
                if not options.quiet:
                    printOut("Parameters restricted to " +
                             str(options.parameters))
            if options.output_best == -1:
                if options.local_search: options.output_best = 0
                else: options.output_best = 1
                printOut("Output best set to: %d" % options.output_best)
            options.html = ""

            if options.sweeps and len(options.sweeps) != len(
                    options.target_levels) and not options.frequency_data:
                if len(options.target_levels) == 0:
                    options.target_levels = len(options.sweeps) * [[0]]
                else:
                    raise InputError(
                        "The number of sweep ranges is not matching the number of target options"
                    )

            if options.sweeps:
                if options.swr_target:
                    options.swr_target = self.convertToListOfLists(
                        list(map(eval, options.swr_target)),
                        len(options.sweeps), 2)
                else:
                    options.swr_target = self.convertToListOfLists(
                        [], len(options.sweeps), 2)
                if options.f2r_target:
                    options.f2r_target = self.convertToListOfLists(
                        list(map(eval, options.f2r_target)),
                        len(options.sweeps), 15)
                else:
                    options.f2r_target = self.convertToListOfLists(
                        [], len(options.sweeps), 2)
                if options.f2b_target:
                    options.f2b_target = self.convertToListOfLists(
                        list(map(eval, options.f2b_target)),
                        len(options.sweeps), 15)
                else:
                    options.f2b_target = self.convertToListOfLists(
                        [], len(options.sweeps), 2)

            class Calc:
                pass

            options.calc = Calc()
            options.calc.beam_width = (
                options.target_function.find("beam_width") != -1)
            options.calc.f2r = (options.target_function.find("f2r") != -1)
            options.calc.f2b = (options.target_function.find("f2b") != -1)
            options.calc.gain = (
                options.target_function.find("gain") != -1
            ) or options.calc.f2r or options.calc.f2b or options.calc.beam_width or options.omni
            options.forward = not (options.calc.f2b or options.calc.f2r
                                   or options.omni or options.frequency_data
                                   or options.calc.beam_width)
            if not options.quiet:
                printOut("Sweeps set to:")
                printOut(options.sweeps)
            if not options.quiet:
                printOut("Target levels set to:")
                printOut(options.target_levels)
            if not options.quiet:
                printOut("SWR target level set to: %s:" %
                         str(options.swr_target))
            if options.calc.f2r:
                if not options.quiet:
                    printOut("F/R target level set to: %s:" %
                             str(options.f2r_target))
            if options.calc.f2b:
                if not options.quiet:
                    printOut("F/B target level set to: %s:" %
                             str(options.f2b_target))
            if not options.quiet:
                printOut("Target function set to \"%s\"" %
                         options.target_function)
            if not options.quiet:
                if not options.noagt_correction:
                    printOut("use-agt-correction set to 1")
                else:
                    printOut("use-agt-correction set to 0")

            return (options, args)
Example #14
0
    def __init__(self, nec_file_input, options):
        #.input, options.output,options.auto_segmentation, options.sweeps, options.target_levels,options.num_cores, options.log_file, options.target_function
        self.log = None
        self.options = options
        self.char_impedance = options.char_impedance
        self.nec_file_input = nec_file_input
        self.nec_evaluator = ne.NecEvaluator(nec_file_input, options)
        from nec.process_monitor import ProcessMonitor
        self.nec_evaluator.process_monitor = ProcessMonitor(
            options.engine_kill_time)
        #print "calculate_gain set to: %d"%self.options.calc.gain

        self.opt_vars = []
        self.domain = []
        for k in self.nec_file_input.min_max.keys():
            if not self.options.parameters or k in self.options.parameters:
                self.opt_vars.append(k)
                self.domain.append(self.nec_file_input.min_max[k])
        self.n = len(self.opt_vars)
        #		self.domain = self.n*[[-1,1]]
        self.enforce_domain_limits = True
        self.x = []
        self.best_score = 999.0
        for i in range(len(self.opt_vars)):
            var = self.opt_vars[i]
            self.x.append(self.nec_file_input.vars[var])

        self.x = self.paramsBackTransform(self.x)
        if options.restart:
            self.initial_population, self.initial_scores = self.parseInitialPopulation(
                options.restart)
        else:
            self.initial_population = []
            self.initial_scores = []

        self.comments = [""]
        self.comments.append("Input file: " + options.input)
        self.comments.append("Sweep ranges: ")
        self.errors = 1
        for i in range(len(self.options.sweeps)):
            self.comments.append("R%d = " % i + str(self.options.sweeps[i]))
            if not self.options.frequency_data:
                self.comments[-1] += (" with target levels " +
                                      str(self.options.target_levels[i]))
        if self.options.frequency_data:
            self.comments.append(" Frequency angle/gain data: ")
            self.comments.append(str(self.options.frequency_data))
        self.comments.append("SWR target: %s" % str(self.options.swr_target))
        self.comments.append("Target function: %s" %
                             self.options.target_function)
        if self.nec_file_input.autosegment[0]:
            printOut("Autosegmentation: %d per %g" %
                     self.nec_file_input.autosegment)
            self.comments.append("Autosegmentation: %d per %g" %
                                 self.nec_file_input.autosegment)
        else:
            printOut("Autosegmentation: NO")
            self.comments.append("Autosegmentation: NO")
        printOut("\n")
        self.comments.append("")
        self.agt_score_threshold = .0
        self.agt_score_threshold_stat1 = .0
        self.agt_score_threshold_stat_count1 = 0
        self.agt_score_threshold_stat2 = .0
        self.agt_score_threshold_stat_count2 = 0

        if options.log_file:
            self.log = open(options.log_file, "at")
            self.log.write("============" * 10 + "\n")
            self.log.write("\n".join(self.comments))

            self.log.write("============" * 10 + "\n")

            range_scores = []
            for i in range(len(self.options.sweeps)):
                range_scores.append("R%dmg" % i)
                range_scores.append("R%dag" % i)
                range_scores.append("R%dms" % i)
                range_scores.append("R%das" % i)

            self.log.write(
                self.nec_evaluator.formatName("Score") + "\t" + "\t".join(
                    map(self.nec_evaluator.formatName, sorted(
                        self.opt_vars))) + "\t" +
                "\t".join(map(self.nec_evaluator.formatName, range_scores)) +
                "\n")
            self.log.flush()
        self.time = time.time()
        self.start_time = self.time
Example #15
0
 def print_status(self, minv, meanv, vector, count, improved):
     t = time.time()
     t -= self.time
     self.time += t
     if self.log:
         self.log.write("#Total time %d sec., Iteration time %d sec.\n" %
                        (int(self.time - self.start_time), t))
     if self.options.quiet: return
     vector = self.paramsTransform(vector)
     z = sorted(zip(self.opt_vars, vector))
     sorted_vars = [x[0] for x in z]
     sorted_vect = [x[1] for x in z]
     if self.options.verbose:
         printOut(
             "====================================================================="
         )
     else:
         sys.stdout.write('\n')
     if not improved:
         printOut("% 5s. Min score %g, Mean score %g, IterTime(%d sec)" %
                  (str(count), minv, meanv, int(t)))
     else:
         printOut(
             "% 5s. Min score %g, Mean score %g, Improved %d members, IterTime(%d sec)"
             % (str(count), minv, meanv, improved, int(t)))
     if self.options.verbose:
         printOut("\t".join(map(self.nec_evaluator.formatName,
                                sorted_vars)))
     if self.options.verbose:
         printOut("\t".join(
             map(self.nec_evaluator.formatNumber, sorted_vect)))
     if self.options.verbose:
         printOut(
             "====================================================================="
         )
Example #16
0
    def printLog(self, vector, res, range_results):
        z = sorted(zip(self.opt_vars, vector))
        sorted_vars = [x[0] for x in z]
        sorted_vect = [x[1] for x in z]

        if self.options.verbose:
            printOut("\t".join(map(self.nec_evaluator.formatName,
                                   sorted_vars)))
            printOut("\t".join(
                map(self.nec_evaluator.formatNumber, sorted_vect)))
            printOut(res)
            printOut("\n")
        elif not self.options.quiet:
            sys.stdout.write('.')
            sys.stdout.flush()
        if self.log:
            self.logParamVector(sorted_vect, res, range_results)
            self.log.flush()

        if res < self.best_score:
            self.best_score = res
            if self.options.output_best:
                fn = ("best%.3f" % res)
                fn = fn.replace(".-", "-")
                fn = fn.replace(".", "_")
                fn = fn + ".nec"
                self.nec_evaluator.writeParametrized(fn,
                                                     comments=self.comments +
                                                     ["Score %g" % res, ""])
            if self.options.quiet:
                printOut("Best score : %.5f" % res)
            elif self.options.local_search and not self.options.verbose:
                printOut("\nBest score : %.5f" % res)
Example #17
0
    def target_(self, vector, get_agt_score, use_agt, id):
        class ExtensibleRangeResult:
            def __init__(self):
                self.data = {}

            def add(self, param, value):
                if param not in self.data:
                    self.data[param] = [value]
                else:
                    self.data[param].append(value)

            def max(self, param):
                if param not in self.data:
                    return 1000
                return max(self.data[param])

            def min(self, param):
                if param not in self.data:
                    return -1000
                return min(self.data[param])

            def ave(self, param):
                if param not in self.data:
                    return 0
                return sum(self.data[param]) / len(self.data[param])

            def aveLog(self, param):  #for 10*log10 values like gain
                if param not in self.data:
                    return 0
                return 10 * math.log10(
                    sum(map(lambda x: math.pow(10, x / 10), self.data[param]))
                    / len(self.data[param]))

            def sum(self, param):
                if param not in self.data:
                    return 0
                return sum(self.data[param])

            def sumPow(self, param):  #for 10*log10 values like gain
                if param not in self.data:
                    return 0
                return sum(
                    map(lambda x: math.pow(10, x / 10), self.data[param]))

            def size(self, param):
                if param not in self.data:
                    return 0
                return len(self.data[param])

        range_results = []
        for i in range(len(self.options.sweeps)):
            range_results.append(ExtensibleRangeResult())

        vector = self.paramsTransform(vector)
        for i in range(len(self.opt_vars)):
            var = self.opt_vars[i]
            self.nec_file_input.vars[var] = vector[i]
        #print "in target_ : Get agt score = %d"%get_agt_score
        results = self.nec_evaluator.runSweeps(get_agt_score, use_agt, id)
        res = -1000
        agts = {}
        if not results:
            if self.options.verbose: printOut("writing erroneous file...")
            try:
                self.nec_evaluator.writeParametrized("error%d.nec" %
                                                     self.errors)
                if self.options.verbose: printOut("done")
            except:
                if self.options.verbose: printOut("failed")
            if self.options.stop_on_error: sys.exit(1)
        else:
            #agts = [1.0]*len(results)

            NOP = ne.NecOutputParser
            try:
                for r in results:
                    nop = NOP(r[0], r[2], self.options)
                    #print "output parsed"
                    sweepid = r[1]
                    agts[r[3]] = r[2]
                    #print "Freqs # = %d"%len(nop.frequencies)
                    for freq in nop.frequencies:
                        freqid = self.freqID(freq.freq, sweepid)
                        if self.options.frequency_data:
                            tl = self.options.frequency_data[freq.freq][1]
                        else:
                            tl = self.targetLevel(freqid[0], freqid[1],
                                                  self.options.target_levels)
                        if self.options.calc.gain:
                            raw_gain = freq.forwardRaw(
                                self.options.forward_dir)
                        else:
                            raw_gain = 0
                        net = freq.net(raw_gain)
                        gain_diff = tl - net
                        range_results[freqid[0]].add("gain_diff", gain_diff)
                        range_results[freqid[0]].add("net_gain", net)
                        range_results[freqid[0]].add("raw_gain", raw_gain)
                        swr = freq.swr()
                        swr_target = self.targetLevel(freqid[0], freqid[1],
                                                      self.options.swr_target)
                        swr_diff = (swr - swr_target)
                        range_results[freqid[0]].add("swr_diff", swr_diff)
                        range_results[freqid[0]].add("swr", swr)
                        if self.options.calc.f2r:
                            rear = freq.rearGain(self.options.rear_angle,
                                                 self.options.backward_dir)
                            if rear is None:
                                raise RuntimeError("Failed to calculate F/R")
                        else:
                            rear = 0
                        if self.options.calc.beam_width:
                            beam = freq.beamWidth(self.options.forward_dir,
                                                  self.options.angle_step,
                                                  self.options.beamwidth_ratio)
                            range_results[freqid[0]].add("beam_width", beam)
                        #print "freq %g, target level %g, net %g, freqno %d, gaindiff %g, swrdiff %g"%(freq.freq, tl, freq.net(),freqid[0], gain_diff, swr_diff)
                        f2r = (net - rear)
                        f2r_target = self.targetLevel(freqid[0], freqid[1],
                                                      self.options.f2r_target)
                        f2r_diff = f2r_target - f2r
                        range_results[freqid[0]].add("f2r_diff", f2r_diff)
                        range_results[freqid[0]].add("f2r", f2r)
                        if self.options.calc.f2b:
                            back = freq.backwardGain(self.options.backward_dir)
                            if back is None:
                                raise RuntimeError("Failed to calculate F/B")
                        else:
                            back = 0
                        range_results[freqid[0]].add("back", back)
                        range_results[freqid[0]].add("rear", rear)
                        f2b = (net - back)
                        f2b_target = self.targetLevel(freqid[0], freqid[1],
                                                      self.options.f2b_target)
                        f2b_diff = f2b_target - f2b
                        range_results[freqid[0]].add("f2b_diff", f2b_diff)
                        range_results[freqid[0]].add("f2b", f2b)

                        ml = raw_gain - net
                        range_results[freqid[0]].add("ml", ml)
                        range_results[freqid[0]].add("real", freq.real)
                        range_results[freqid[0]].add("imag", freq.imag)
                        range_results[freqid[0]].add("agt_correction",
                                                     freq.agt)
                        if self.options.omni:
                            by_angle_net = [
                                freq.horizontalRaw(a) - ml
                                for a in sorted(freq.horizontal.keys())
                            ]
                            by_angle_gain_diff = [
                                tl - (freq.horizontalRaw(a) - ml)
                                for a in sorted(freq.horizontal.keys())
                            ]
                            range_results[freqid[0]].add(
                                "by_angle_net", by_angle_net)
                            range_results[freqid[0]].add(
                                "by_angle_gain_diff", by_angle_gain_diff)
                            range_results[freqid[0]].add(
                                "omni_net", min(by_angle_net))
                            range_results[freqid[0]].add(
                                "omni_gain_diff", max(by_angle_gain_diff))
                            range_results[freqid[0]].add(
                                "around_net",
                                sum(by_angle_net) / max(1, len(by_angle_net)))
                            range_results[freqid[0]].add(
                                "around_gain_diff",
                                sum(by_angle_gain_diff) /
                                max(1, len(by_angle_gain_diff)))


#						range_results[freqid[0]].add(gain_diff, swr_diff, f2r_diff)
                import pprint
                if self.options.debug > 1:
                    pprint.pprint(map(lambda x: x.data, range_results))
                d = {"results": [r.data for r in range_results]}
                freq_count = 0
                log_keys = [
                    "gain_diff", "net_gain", "raw_gain", "f2r_diff", "f2r",
                    "f2b_diff", "back", "rear", "ml"
                ]
                exclude_keys = ["by_angle_net", "by_angle_gain_diff"]
                all_keys = []
                for i in range(len(self.options.sweeps)):
                    result = range_results[i]
                    c = 0
                    for k in result.data.keys():
                        if k in exclude_keys: continue
                        if k not in all_keys: all_keys.append(k)
                        if k in log_keys:
                            x = math.pow(10, result.max(k) / 10)
                            n = math.pow(10, result.min(k) / 10)
                            s = result.sumPow(k)
                        else:
                            x = result.max(k)
                            n = result.min(k)
                            s = result.sum(k)
                        c = result.size(k)
                        a = s / c
                        if "max_" + k not in d:
                            d["max_" + k] = x
                            d["ave_max_" + k] = x
                            d["max_ave_" + k] = a
                            d["ave_" + k] = s
                            d["min_" + k] = n
                            d["ave_min_" + k] = n
                            d["min_ave_" + k] = a
                            d["ave_ave_" + k] = a
                        else:
                            d["max_" + k] = max(x, d["max_" + k])
                            d["ave_max_" + k] += x
                            d["max_ave_" + k] = max(a, d["max_ave_" + k])
                            d["ave_" + k] += s
                            d["min_" + k] = min(n, d["min_" + k])
                            d["ave_min_" + k] += n
                            d["min_ave_" + k] = min(a, d["min_ave_" + k])
                            d["ave_ave_" + k] += a
                    freq_count = freq_count + c

                for k in all_keys:
                    ns = len(self.options.sweeps)
                    if k in log_keys:
                        d["max_" + k] = 10 * math.log10(d["max_" + k])
                        d["ave_max_" +
                          k] = 10 * math.log10(d["ave_max_" + k] / ns)
                        d["max_ave_" + k] = 10 * math.log10(d["max_ave_" + k])
                        d["ave_" +
                          k] = 10 * math.log10(d["ave_" + k] / freq_count)
                        d["min_" + k] = 10 * math.log10(d["min_" + k])
                        d["ave_min_" +
                          k] = 10 * math.log10(d["ave_min_" + k] / ns)
                        d["min_ave_" + k] = 10 * math.log10(d["min_ave_" + k])
                        d["ave_ave_" +
                          k] = 10 * math.log10(d["ave_ave_" + k] / ns)
                    else:
                        d["ave_max_" + k] /= ns
                        d["ave_" + k] /= freq_count
                        d["ave_min_" + k] /= ns
                        d["ave_ave_" + k] /= ns

                if self.options.debug > 1: pprint.pprint(d)
                d.update(self.nec_file_input.globals)
                res = eval(self.options.target_function, d)

            except:
                if not self.options.verbose: sys.stderr.write('\n')
                traceback.print_exc()
                if self.options.stop_on_error: sys.exit(1)
                res = -1000
        if res == -1000:
            res = 1000.0

        if get_agt_score:
            return (res, agts)
        self.printLog(vector, res, range_results)
        return res
Example #18
0
def parseLogFile(filename, full, number_of_lines, population_number):
    f = open(filename, "rt")
    lines = f.readlines()
    f.close()
    i = -1
    for j in range(len(lines)):
        if lines[j].find("Score") != -1:
            i = j

    if i == -1:
        return ()

    vars = lines[i].split()[1:]
    lines = lines[i + 1:]

    i = -1
    for j in range(len(lines)):
        if lines[j].find("#Total time") != -1:
            i = j
            break
    if i == -1:
        return ()
    np = int(j / 2)

    scores = []
    population = []
    for i in range(np):
        ln = lines[i].split()
        scores.append(float(ln[0]))
        population.append(list(map(floatOrNone, ln[1:])))

    lines = lines[np:]
    count = 1
    while len(lines):
        k = []
        i = 0
        while i < np and i < len(lines):
            if lines[i] and lines[i][0] == '#':
                del lines[i]
                continue
            ln = lines[i].split()
            s = float(ln[0])
            if s < scores[i]:
                k.append((i, "%.4g" % s))
                scores[i] = s
                population[i] = list(map(floatOrNone, ln[1:]))
            i += 1
        lines = lines[i:]
        if not number_of_lines or len(lines) < number_of_lines * np:
            if i == np:
                printOut(
                    "Iteration %d [%.6g:%.6g] - %d new offsprings - " %
                    (count, min_value(scores), mean_value(scores), len(k)) +
                    str(k).replace("'", ""))
            elif not full:
                printOut(
                    "(*%d)Iteration %d [%.6g, %.6g] - %d new offsprings - " %
                    (i, count, min_value(scores), mean_value(scores), len(k)) +
                    str(k).replace("'", ""))

        count = count + 1
        if population_number and count == population_number:
            break

    return (vars, scores, population)