def test_solution_quality(self):
        """Run demo.py with no POIs or existing chargers to locate two new chargers"""

        w, h = (15, 15)
        num_poi, num_cs, num_new_cs = (0, 0, 2)

        _, pois, charging_stations, potential_new_cs_nodes = demo.set_up_scenario(
            w, h, num_poi, num_cs)

        bqm = demo.build_bqm(potential_new_cs_nodes, num_poi, pois, num_cs,
                             charging_stations, num_new_cs)

        # random.seed(1)
        sampler = neal.SimulatedAnnealingSampler()
        new_charging_nodes = demo.run_bqm_and_collect_solutions(
            bqm, sampler, potential_new_cs_nodes, seed=1)

        new_cs_dist = 0
        for i in range(num_new_cs):
            for j in range(i + 1, num_new_cs):
                new_cs_dist += abs(new_charging_nodes[i][0] -
                                   new_charging_nodes[j][0]) + abs(
                                       new_charging_nodes[i][1] -
                                       new_charging_nodes[j][1])

        self.assertGreater(new_cs_dist, 10)
    def test_close_to_pois(self):
        """Check that 1 new / 0 old chargers scenario is close to centroid of POIs"""

        w, h = (15, 15)
        num_poi, num_cs, num_new_cs = (3, 0, 1)

        _, pois, charging_stations, potential_new_cs_nodes = demo.set_up_scenario(
            w, h, num_poi, num_cs)

        pois = [(0, 0), (6, 14), (14, 0)]
        centroid = np.array(pois).mean(axis=0).round().tolist()

        bqm = demo.build_bqm(potential_new_cs_nodes, num_poi, pois, num_cs,
                             charging_stations, num_new_cs)

        # random.seed(1)
        sampler = neal.SimulatedAnnealingSampler()
        new_charging_nodes = demo.run_bqm_and_collect_solutions(
            bqm, sampler, potential_new_cs_nodes, seed=42)

        new_cs_x = new_charging_nodes[0][0]
        new_cs_y = new_charging_nodes[0][1]

        self.assertLess(new_cs_x - centroid[0], 5)
        self.assertLess(new_cs_y - centroid[1], 5)
    def test_scenario_setup(self):

        w, h = random.randint(10, 20), random.randint(10, 20)
        num_poi, num_cs, num_new_cs = (random.randint(1,
                                                      4), random.randint(1, 4),
                                       random.randint(1, 4))

        G, pois, charging_stations, potential_new_cs_nodes = demo.set_up_scenario(
            w, h, num_poi, num_cs)

        self.assertEqual(len(G.nodes()), w * h)
        self.assertEqual(len(pois), num_poi)
        self.assertEqual(len(charging_stations), num_cs)
        self.assertEqual(len(potential_new_cs_nodes),
                         len(G.nodes()) - len(charging_stations))
    def test_same_bqm(self):
        """Run demo.py and demo_numpy.py with same inputs to check same BQM created."""

        w, h = (random.randint(10, 20), random.randint(10, 20))
        num_poi, num_cs, num_new_cs = (random.randint(1,
                                                      4), random.randint(1, 4),
                                       random.randint(1, 4))

        G, pois, charging_stations, potential_new_cs_nodes = demo.set_up_scenario(
            w, h, num_poi, num_cs)

        bqm = demo.build_bqm(potential_new_cs_nodes, num_poi, pois, num_cs,
                             charging_stations, num_new_cs)
        bqm_np = demo_numpy.build_bqm(potential_new_cs_nodes, num_poi, pois,
                                      num_cs, charging_stations, num_new_cs)
        bqm_np.offset += bqm.offset

        dimod.testing.asserts.assert_bqm_almost_equal(bqm, bqm_np)
    def test_num_new_cs(self):
        """Check that correct number of new charging locations are found in a random scenario"""

        w, h = (random.randint(10, 20), random.randint(10, 20))
        num_poi, num_cs, num_new_cs = (random.randint(1,
                                                      4), random.randint(1, 4),
                                       random.randint(1, 4))

        G, pois, charging_stations, potential_new_cs_nodes = demo.set_up_scenario(
            w, h, num_poi, num_cs)

        bqm = demo.build_bqm(potential_new_cs_nodes, num_poi, pois, num_cs,
                             charging_stations, num_new_cs)

        sampler = neal.SimulatedAnnealingSampler()
        new_charging_nodes = demo.run_bqm_and_collect_solutions(
            bqm, sampler, potential_new_cs_nodes)

        self.assertEqual(num_new_cs, len(new_charging_nodes))
    q1 = quad_row[dist_mat != 0]
    q3 = dist_mat[dist_mat != 0]

    bqm_np = dimod.BinaryQuadraticModel.from_numpy_vectors(
        linear=linear, quadratic=(q1, q2, q3), offset=0, vartype=dimod.BINARY)

    return bqm_np


if __name__ == '__main__':

    # Collect user inputs
    args = demo.read_in_args()

    # Build large grid graph for city
    G, pois, charging_stations, potential_new_cs_nodes = demo.set_up_scenario(
        args.width, args.height, args.poi, args.chargers)

    # Build BQM
    bqm = build_bqm(potential_new_cs_nodes, args.poi, pois, args.chargers,
                    charging_stations, args.new_chargers)

    # Run BQM on HSS
    sampler = LeapHybridSampler()
    print("\nRunning scenario on", sampler.solver.id, "solver...")

    new_charging_nodes = demo.run_bqm_and_collect_solutions(
        bqm, sampler, potential_new_cs_nodes)

    # Print results to commnand-line for user
    demo.printout_solution_to_cmdline(pois, args.poi, charging_stations,
                                      args.chargers, new_charging_nodes,