示例#1
0
def cuthill_mckee(graph):
    """Return a Cuthill-McKee ordering for the given graph.

    See (for example)
    Y. Saad, Iterative Methods for Sparse Linear System,
    2nd edition, p. 76.

    *graph* is given as an adjacency mapping, i.e. each node is
    mapped to a list of its neighbors.
    """
    from pytools import argmin

    # this list is called "old_numbers" because it maps a
    # "new number to its "old number"
    old_numbers = []
    visited_nodes = set()
    levelset = []

    all_nodes = set(graph.keys())

    def levelset_cmp(node_a, node_b):
        return cmp(len(graph[node_a]), len(graph[node_b]))

    while len(old_numbers) < len(graph):
        if not levelset:
            unvisited = list(set(graph.keys()) - visited_nodes)

            if not unvisited:
                break

            start_node = unvisited[argmin(
                len(graph[node]) for node in unvisited)]
            visited_nodes.add(start_node)
            old_numbers.append(start_node)
            levelset = [start_node]

        next_levelset = set()
        levelset.sort(levelset_cmp)

        for node in levelset:
            for neighbor in graph[node]:
                if neighbor in visited_nodes:
                    continue

                visited_nodes.add(neighbor)
                next_levelset.add(neighbor)
                old_numbers.append(neighbor)

        levelset = list(next_levelset)

    return old_numbers
示例#2
0
def cuthill_mckee(graph):
    """Return a Cuthill-McKee ordering for the given graph.

    See (for example)
    Y. Saad, Iterative Methods for Sparse Linear System,
    2nd edition, p. 76.

    *graph* is given as an adjacency mapping, i.e. each node is
    mapped to a list of its neighbors.
    """
    from pytools import argmin

    # this list is called "old_numbers" because it maps a
    # "new number to its "old number"
    old_numbers = []
    visited_nodes = set()
    levelset = []

    all_nodes = set(graph.keys())

    def levelset_cmp(node_a, node_b):
        return cmp(len(graph[node_a]), len(graph[node_b]))

    while len(old_numbers) < len(graph):
        if not levelset:
            unvisited = list(set(graph.keys()) - visited_nodes)

            if not unvisited:
                break

            start_node = unvisited[
                    argmin(len(graph[node]) for node in unvisited)]
            visited_nodes.add(start_node)
            old_numbers.append(start_node)
            levelset = [start_node]

        next_levelset = set()
        levelset.sort(levelset_cmp)

        for node in levelset:
            for neighbor in graph[node]:
                if neighbor in visited_nodes:
                    continue

                visited_nodes.add(neighbor)
                next_levelset.add(neighbor)
                old_numbers.append(neighbor)

        levelset = list(next_levelset)

    return old_numbers
示例#3
0
def metropolis_hastings_process():
    """
	Samples a bunch of algorithms (with count specified in Config), saves their
	scores, and returns the best of the bunch.
	"""
    trainingdata = Config.LoadTrainingPairs()
    print(len(trainingdata))
    samples, distances = metrohast(Config.mh_samplecount, trainingdata)
    best = argmin(distances)
    print("Index of best player is ", best)
    print("Distances under distance metric:")
    print(distances)
    print("Best distance: ", distances[best])
    print("Best rule (along with script):")
    print(samples[best].to_script())
    print("------------------------------------------------------------------")
    Config.SaveDistances(distances)
    Config.SaveSamples([sample.to_script() for sample in samples])
    return samples[best].player
示例#4
0
def parse_superfish_format(filename, max_point_dist=0.1):
    import re

    def chop_comment(line):
        com_start = line.find(";")
        if com_start != -1:
            return line[:com_start]
        else:
            return line

    lines = [
        chop_comment(line.strip().lower())
        for line in file(filename, "r").readlines()
    ]
    lines = [line for line in lines if not line.startswith("!")]
    lines = [line for line in lines if line]
    lines = [line for line in lines if line.startswith("&po")]

    # parse the file
    line_re = re.compile(r"^\&po\s+(.*)\s*\&$")
    key_val_re = re.compile(r"^([0-9a-zA-Z]+)\s*\=\s*([-+0-9.a-zA-Z]+)")

    proplines = []

    for line in lines:
        line_match = line_re.match(line)
        assert line_match
        line = line_match.group(1)

        properties = {}

        pos = 0
        while pos < len(line):
            if line[pos] in [" ", ","]:
                pos += 1
                continue
            datum_match = key_val_re.match(line[pos:])
            assert datum_match
            properties[datum_match.group(1)] = float(datum_match.group(2))
            pos += datum_match.end()

        proplines.append(properties)

    #for p in proplines:
    #print p

    # concoct x-y-points
    from math import atan2, sin, cos, pi, ceil
    from pytools import argmin

    def add_point(pt):
        points.append((pt[1], pt[0]))

    points = []
    for p in proplines:
        shape_type = 1
        if "nt" in p:
            shape_type = p["nt"]

        if shape_type == 1:
            points.append(numpy.array((p["x"], p["y"])))
        elif shape_type == 2:
            # draw arc
            last_point = points[-1]
            if "x0" in p:
                center = numpy.array((p["x0"], p["y0"]))
            else:
                center = numpy.zeros((2, ))

            if "r" in p or "radius" in p:
                if "r" in p:
                    r = p["r"]
                    assert "radius" not in p
                else:
                    r = p["radius"]
                    assert "r" not in p

                theta = pi / 180. * p["theta"]

                tangent = numpy.array((cos(theta), sin(theta)))
                upward_normal = numpy.array((-sin(theta), cos(theta)))

                #print 180*theta/pi, last_point, center, tangent, upward_normal, last_point-center
                if numpy.dot(last_point - center, upward_normal) < 0:
                    # draw ccw (positive) arc / upward
                    phi_start = 3 * pi / 2 + theta
                    phi_end = phi_start + pi / 2
                else:
                    # draw cw (negative) arc / downward
                    phi_start = pi / 2 + theta
                    phi_end = phi_start - pi / 2
            elif "x" in p:
                start_pt = last_point - center
                end_pt = numpy.array((p["x"], p["y"]))

                r = la.norm(end_pt)
                r2 = la.norm_2(start_pt)

                assert abs(r - r2) / r < 1e-2

                phi_start = atan2(start_pt[1], start_pt[0])
                raw_phi_end = atan2(end_pt[1], end_pt[0])

                phi_end_choices = [
                    raw_phi_end, raw_phi_end - 2 * pi, raw_phi_end + 2 * pi
                ]
                phi_end = phi_end_choices[argmin(
                    abs(phi_end - phi_start) for phi_end in phi_end_choices)]
            else:
                raise ValueError, "arcs (nt=2) must have either r/radius or x/y specified"

            steps = int(ceil((abs(phi_end - phi_start)) * r / max_point_dist))
            dphi = (phi_end - phi_start) / steps
            phi = phi_start + dphi

            for i in range(steps):
                points.append(center + r * numpy.array((cos(phi), sin(phi))))
                phi += dphi
        else:
            raise ValueError, "unhandled shape type: nt=%s" % shape_type

    # assert that last point coincides with first
    assert ((points[0][0] - points[-1][0])**2 +
            (points[0][1] - points[-1][1])**2) < 1e-10
    # and kill it
    points.pop()

    # now find the on-axis line, if any, and shift it so straddles the array boundary
    assert len(points) > 1

    for i, (z, r) in enumerate(points):
        next_i = (i + 1) % len(points)
        next_r = points[next_i][1]

        if r == 0 and next_r == 0:
            if i < len(points):
                # if the on-axis line is already the last line in the polygon,
                # then that's fine--no need to change. Otherwise, shift so that
                # this becomes the case.

                from pytools import shift
                points = shift(points, len(points) - 1 - i)
                break

    # assert start and end r are 0
    assert points[0][1] == 0
    assert points[-1][1] == 0

    outf = file("gun-lines.dat", "w")
    for p in points:
        outf.write("%f\t%f\n" % (p[0], p[1]))
    outf.close()

    # turn (x,y) into (r,z)
    return [(p[1], p[0]) for p in points]
示例#5
0
def optimize_shape_bandwidth(method, state, analytic_rho, exponent):
    discr = method.discretization
    rec = method.depositor

    adv_radius = method.mesh_data.advisable_particle_radius()
    radii = [adv_radius * 2**i for i in numpy.linspace(-4, 2, 50)]

    def set_radius(r):
        method.depositor.set_shape_function(
            state,
            method.get_shape_function_class()(
                r,
                method.mesh_data.dimensions,
                exponent,
            ))

    tried_radii = []
    l1_errors = []

    debug = "shape_bw" in method.debug
    visualize = set(["shape_bw", "vis_files"]) <= method.debug

    if visualize:
        from hedge.visualization import SiloVisualizer
        vis = SiloVisualizer(discr)

    import sys

    if debug:
        sys.stdout.write("optimizing shape bw (%d attempts): " % len(radii))
    for step, radius in enumerate(radii):
        if debug:
            sys.stdout.write("%d." % step)
            sys.stdout.flush()

        try:
            try:
                method.set_ignore_core_warnings(True)
                set_radius(radius)
            except RuntimeError, re:
                if "particle mass is zero" in str(re):
                    continue
                else:
                    raise
        finally:
            method.set_ignore_core_warnings(False)

        state.derived_quantity_cache.clear()

        try:
            try:
                method.set_ignore_core_warnings(True)
                rec_rho = method.deposit_rho(state)
            except RuntimeError, re:
                if "particle mass is zero" in str(re):
                    continue
                else:
                    raise
        finally:
            method.set_ignore_core_warnings(False)

        tried_radii.append(radius)
        l1_errors.append(discr.integral(numpy.abs(rec_rho - analytic_rho)))

        if visualize:
            visf = vis.make_file("bwopt-%04d" % step)
            method.add_to_vis(vis, visf, state, time=radius, step=step)
            vis.add_data(
                visf,
                [
                    ("rho", rec_rho),
                    #("j", method.deposit_j(state)),
                    ("rho_analytic", analytic_rho),
                ],
                expressions=[("rho_diff", "rho-rho_analytic")],
                time=radius,
                step=step)

            try:
                rec.visualize_grid_quantities
            except AttributeError:
                pass
            else:
                rec.visualize_grid_quantities(visf, [
                    ("rho_grid", rec.deposit_grid_rho(state)),
                    ("j_grid",
                     rec.deposit_grid_j(state, method.velocities(state))),
                    ("rho_resid",
                     rec.remap_residual(rec.deposit_grid_rho(state))),
                ])

            visf.close()

    if debug:
        sys.stdout.write("\n")
        sys.stdout.flush()

    if visualize:
        vis.close()

    from pytools import argmin
    min_idx = argmin(l1_errors)
    min_rad = tried_radii[min_idx]
    min_l1_error = l1_errors[min_idx]

    rel_l1_error = abs(min_l1_error / discr.integral(analytic_rho))
    if rel_l1_error > 0.1:
        from warnings import warn
        warn("Charge density is very poorly resolved (rel L1 error=%g)" %
             rel_l1_error)

    def is_local_minimum(list, i):
        if i == 0:
            return False
        elif i == len(list) - 1:
            return False
        else:
            return list[i] < list[i - 1] and list[i] < list[i + 1]

    local_minima = [
        idx for idx in range(len(tried_radii))
        if is_local_minimum(l1_errors, idx)
    ]

    chosen_idx = max(local_minima)
    chosen_rad = tried_radii[chosen_idx]

    if "shape_bw" in method.debug:
        chosen_l1_error = l1_errors[chosen_idx]
        print "radius: guessed optimum=%g, found optimum=%g, chosen=%g" % (
            adv_radius, min_rad, chosen_rad)
        print "radius: optimum l1 error=%g, chosen l1 error=%g" % (
            min_l1_error, chosen_l1_error)

    set_radius(chosen_rad)
    state.derived_quantity_cache.clear()

    if set(["interactive", "shape_bw"]) < method.debug:
        from pylab import semilogx, show
        semilogx(tried_radii, l1_errors)
        show()
示例#6
0
def parse_superfish_format(filename, max_point_dist=0.1):
    import re
    
    def chop_comment(line):
        com_start = line.find(";")
        if com_start != -1:
            return line[:com_start]
        else:
            return line

    lines = [chop_comment(line.strip().lower()) for line in file(filename, "r").readlines()]
    lines = [line for line in lines if not line.startswith("!")]
    lines = [line for line in lines if line]
    lines = [line for line in lines if line.startswith("&po")]

    # parse the file
    line_re = re.compile(r"^\&po\s+(.*)\s*\&$")
    key_val_re = re.compile(r"^([0-9a-zA-Z]+)\s*\=\s*([-+0-9.a-zA-Z]+)")

    proplines = []

    for line in lines:
        line_match = line_re.match(line)
        assert line_match
        line = line_match.group(1)

        properties = {}

        pos = 0 
        while pos < len(line):
            if line[pos] in [" ", ","]:
                pos += 1
                continue
            datum_match = key_val_re.match(line[pos:])
            assert datum_match
            properties[datum_match.group(1)] = float(datum_match.group(2))
            pos += datum_match.end()

        proplines.append(properties)

    #for p in proplines:
        #print p

    # concoct x-y-points
    from math import atan2, sin, cos, pi, ceil
    from pytools import argmin

    def add_point(pt):
        points.append((pt[1], pt[0]))

    points = []
    for p in proplines:
        shape_type = 1
        if "nt" in p:
            shape_type = p["nt"]

        if shape_type == 1:
            points.append(numpy.array((p["x"], p["y"])))
        elif shape_type == 2:
            # draw arc
            last_point = points[-1]
            if "x0" in p:
                center = numpy.array((p["x0"], p["y0"]))
            else:
                center = numpy.zeros((2,))

            if "r" in p or "radius" in p:
                if "r" in p:
                    r = p["r"]
                    assert "radius" not in p
                else:
                    r = p["radius"]
                    assert "r" not in p

                theta = pi/180.*p["theta"]

                tangent = numpy.array((cos(theta), sin(theta)))
                upward_normal = numpy.array((-sin(theta), cos(theta)))

                #print 180*theta/pi, last_point, center, tangent, upward_normal, last_point-center
                if numpy.dot(last_point - center, upward_normal) < 0:
                    # draw ccw (positive) arc / upward
                    phi_start = 3*pi/2 + theta
                    phi_end = phi_start + pi/2
                else:
                    # draw cw (negative) arc / downward
                    phi_start = pi/2 + theta
                    phi_end = phi_start - pi/2
            elif "x" in p:
                start_pt = last_point - center
                end_pt = numpy.array((p["x"], p["y"]))

                r = la.norm(end_pt)
                r2 = la.norm_2(start_pt)

                assert abs(r-r2)/r < 1e-2

                phi_start = atan2(start_pt[1], start_pt[0])
                raw_phi_end = atan2(end_pt[1], end_pt[0])

                phi_end_choices = [
                        raw_phi_end, 
                        raw_phi_end-2*pi, 
                        raw_phi_end+2*pi]
                phi_end = phi_end_choices[argmin(
                    abs(phi_end-phi_start) for phi_end in phi_end_choices)]
            else:
                raise ValueError, "arcs (nt=2) must have either r/radius or x/y specified"

            steps = int(ceil((abs(phi_end-phi_start))*r/max_point_dist))
            dphi = (phi_end-phi_start) / steps
            phi = phi_start+dphi

            for i in range(steps):
                points.append(center + r*numpy.array((cos(phi), sin(phi))))
                phi += dphi
        else:
            raise ValueError, "unhandled shape type: nt=%s" % shape_type

    # assert that last point coincides with first
    assert ((points[0][0]-points[-1][0])**2 + (points[0][1]-points[-1][1])**2) < 1e-10
    # and kill it
    points.pop()

    # now find the on-axis line, if any, and shift it so straddles the array boundary
    assert len(points) > 1

    for i, (z, r) in enumerate(points):
        next_i = (i+1)%len(points)
        next_r = points[next_i][1]

        if r == 0 and next_r == 0:
            if i < len(points):
                # if the on-axis line is already the last line in the polygon,
                # then that's fine--no need to change. Otherwise, shift so that
                # this becomes the case.

                from pytools import shift
                points = shift(points, len(points)-1-i)
                break

    # assert start and end r are 0
    assert points[0][1] == 0
    assert points[-1][1] == 0

    outf = file("gun-lines.dat", "w")
    for p in points:
        outf.write("%f\t%f\n" % (p[0], p[1]))
    outf.close()

    # turn (x,y) into (r,z)
    return [(p[1], p[0]) for p in points]
示例#7
0
文件: cloud.py 项目: gimac/pyrticle
def optimize_shape_bandwidth(method, state, analytic_rho, exponent):
    discr = method.discretization
    rec = method.depositor

    adv_radius = method.mesh_data.advisable_particle_radius()
    radii = [adv_radius*2**i
            for i in numpy.linspace(-4, 2, 50)]

    def set_radius(r):
        method.depositor.set_shape_function(
                state,
                method.get_shape_function_class()
                (r, method.mesh_data.dimensions, exponent,))

    tried_radii = []
    l1_errors = []

    debug = "shape_bw" in method.debug
    visualize = set(["shape_bw", "vis_files"]) <= method.debug

    if visualize:
        from hedge.visualization import SiloVisualizer
        vis = SiloVisualizer(discr)

    import sys

    if debug:
        sys.stdout.write("optimizing shape bw (%d attempts): " % len(radii))
    for step, radius in enumerate(radii):
        if debug:
            sys.stdout.write("%d." % step)
            sys.stdout.flush()

        try:
            try:
                method.set_ignore_core_warnings(True)
                set_radius(radius)
            except RuntimeError, re:
                if "particle mass is zero" in str(re):
                    continue
                else:
                    raise
        finally:
            method.set_ignore_core_warnings(False)

        state.derived_quantity_cache.clear()

        try:
            try:
                method.set_ignore_core_warnings(True)
                rec_rho = method.deposit_rho(state)
            except RuntimeError, re:
                if "particle mass is zero" in str(re):
                    continue
                else:
                    raise
        finally:
            method.set_ignore_core_warnings(False)

        tried_radii.append(radius)
        l1_errors.append(discr.integral(numpy.abs(rec_rho-analytic_rho)))

        if visualize:
            visf = vis.make_file("bwopt-%04d" % step)
            method.add_to_vis(vis, visf, state, time=radius, step=step)
            vis.add_data(visf, [
                ("rho", rec_rho),
                #("j", method.deposit_j(state)),
                ("rho_analytic", analytic_rho),
                ],
                expressions=[("rho_diff", "rho-rho_analytic")],
                time=radius, step=step)

            try:
                rec.visualize_grid_quantities
            except AttributeError:
                pass
            else:
                rec.visualize_grid_quantities(visf, [
                        ("rho_grid", rec.deposit_grid_rho(state)),
                        ("j_grid", rec.deposit_grid_j(state, method.velocities(state))),
                        ("rho_resid", rec.remap_residual(rec.deposit_grid_rho(state))),
                        ])

            visf.close()

    if debug:
        sys.stdout.write("\n")
        sys.stdout.flush()

    if visualize:
        vis.close()

    from pytools import argmin
    min_idx = argmin(l1_errors)
    min_rad = tried_radii[min_idx]
    min_l1_error = l1_errors[min_idx]

    rel_l1_error = abs(min_l1_error / discr.integral(analytic_rho))
    if rel_l1_error > 0.1:
        from warnings import warn
        warn("Charge density is very poorly resolved (rel L1 error=%g)" % rel_l1_error)

    def is_local_minimum(list, i):
        if i == 0:
            return False
        elif i == len(list)-1:
            return False
        else:
            return list[i] < list[i-1] and list[i] < list[i+1]

    local_minima = [idx for idx in range(len(tried_radii))
            if is_local_minimum(l1_errors, idx)]

    chosen_idx = max(local_minima)
    chosen_rad = tried_radii[chosen_idx]

    if "shape_bw" in method.debug:
        chosen_l1_error = l1_errors[chosen_idx]
        print "radius: guessed optimum=%g, found optimum=%g, chosen=%g" % (
                adv_radius, min_rad, chosen_rad)
        print "radius: optimum l1 error=%g, chosen l1 error=%g" % (
                min_l1_error, chosen_l1_error)

    set_radius(chosen_rad)
    state.derived_quantity_cache.clear()

    if set(["interactive", "shape_bw"]) < method.debug:
        from pylab import semilogx, show
        semilogx(tried_radii, l1_errors)
        show()