GraphRandomizer.java

package edu.hawaii.ics.yucheng;

/**
 * A class that creates random graphs.
 */
class GraphRandomizer {

    /** The maximum possible battery consumption value. */
    private static final int MAX_CONSUMPTION = 99;

    /** The maximum possible height. */
    private static final int MAX_HEIGHT      = 9;

    /** The maximum possible priority value. */
    private static final int MAX_PRIORITY    = 99;

    /** The maximum possible width. */
    private static final int MAX_WIDTH       = 9;

    /** The minimum possible battery consumption value. */
    private static final int MIN_CONSUMPTION = 1;

    /** The minimum possible height. */
    private static final int MIN_HEIGHT      = 1;

    /** The minimum possible priority value. */
    private static final int MIN_PRIORITY    = 1;

    /** The minimum possible width. */
    private static final int MIN_WIDTH       = 1;


    /**
     * Initializes a vertex in the graph.
     * 
     * @param i The horizontal coordinate.
     * 
     * @param j The vertical coordinate.
     * 
     * @param builder The graph builder.
     * 
     * @return The consumption value for the vertex.
     */
    private static int initCell(final int i, final int j,
        final GraphBuilder builder) {

        assert null != builder;

        if (builder.base().equals(i, j))
            return 0;

        if (random(0, 9) == 0) {
            builder.setObstacle(i, j, true);
            return 0;
        }

        final int priority = random(MIN_PRIORITY, MAX_PRIORITY);
        final int consumption = random(MIN_CONSUMPTION, MAX_CONSUMPTION);

        builder.setPriority(i, j, priority);
        builder.setConsumption(i, j, consumption);
        return builder.consumption(i, j);
    }


    /**
     * Creates and returns a new random graph.
     * 
     * @return A new random graph.
     */
    public static Graph newGraph() {

        // Create the random size.
        final int width = random(MIN_WIDTH, MAX_WIDTH);
        final int height = random(MIN_HEIGHT, MAX_HEIGHT);
        final GraphBuilder builder = new GraphBuilder(width, height);

        // Randomly select the base.
        final int x = random(0, width - 1);
        final int y = random(0, height - 1);
        builder.setBase(x, y);

        // Loop over each vertex and randomly create the attributes.
        int sum = 0;
        for (int j = 0; j < height; j++)
            for (int i = 0; i < width; i++)
                sum += initCell(i, j, builder);

        // Set the capacity based on 0 - 100% of the sum of the capacities.
        builder.setCapacity(sum * random(0, 100) / 100);

        builder.setPriority(x, y, 0);
        builder.setConsumption(x, y, 0);

        // Return the new graph.
        return builder.toGraph();
    }


    /**
     * Returns a random number between a minimum and maximum value.
     * 
     * @param min The minimum value.
     * 
     * @param max The maximum value.
     * 
     * @return A random number between a minimum and maximum value (inclusive).
     */
    private static int random(final int min, final int max) {
        return (int) (Math.random() * (1 + max - min) + min);
    }
}
Valid HTML 4.01 Valid CSS