예제 #1
0
def add_systems(args, config_dict, schema, debug=False):
    """
    Params:
    config_dict: A dictionary that maps the bot name to a dictionary containing configs for the bot. The
        dictionary should contain the bot type (key 'type') and. for bots that use an underlying model for generation,
        the path to the directory containing the parameters, vocab, etc. for the model.
    Returns:
    agents: A dict mapping from the bot name to the System object for that bot.
    pairing_probabilities: A dict mapping from the bot name to the probability that a user is paired with that
        bot. Also includes the pairing probability for humans (backend.Partner.Human)
    """

    total_probs = 0.0
    # systems = {HumanSystem.name(): HumanSystem()}
    ### changed parts from original craigslistbargain code
    # get_system: in craigslistbargain/systems/__init__.py
    systems = {HumanSystem.name(): HumanSystem(), RulebasedSystem.name(): get_system("rulebased", args)}
    ###
    pairing_probabilities = {}
    timed = False if debug else True
    for (sys_name, info) in config_dict.iteritems():
        if "active" not in info.keys():
            warnings.warn("active status not specified for bot %s - assuming that bot is inactive." % sys_name)
        if info["active"]:
            name = info["type"]
            try:
                model = get_system(name, args, schema=schema, timed=timed, model_path=info.get('checkpoint'))
            except ValueError:
                warnings.warn(
                    'Unrecognized model type in {} for configuration '
                    '{}. Ignoring configuration.'.format(info, sys_name))
                continue
            systems[sys_name] = model
            if 'prob' in info.keys():
                prob = float(info['prob'])
                pairing_probabilities[sys_name] = prob
                total_probs += prob
    print '{} systems loaded'.format(len(systems))
    for name in systems:
        print name

    # TODO: clean up pairing probabilities (obsolete)
    if total_probs > 1.0:
        raise ValueError("Probabilities for active bots can't exceed 1.0.")
    if len(pairing_probabilities.keys()) != 0 and len(pairing_probabilities.keys()) != len(systems.keys()):
        remaining_prob = (1.0-total_probs)/(len(systems.keys()) - len(pairing_probabilities.keys()))
    else:
        remaining_prob = 1.0 / len(systems.keys())
    inactive_bots = set()
    for system_name in systems.keys():
        if system_name not in pairing_probabilities.keys():
            if remaining_prob == 0.0:
                inactive_bots.add(system_name)
            else:
                pairing_probabilities[system_name] = remaining_prob

    for sys_name in inactive_bots:
        systems.pop(sys_name, None)

    return systems, pairing_probabilities
예제 #2
0
def test_create_seed_nonperiodic():
    system, pars = get_system('alanine')
    configuration = Configuration(system, pars)

    seed_covalent = configuration.create_seed(kind='covalent')
    ff = yaff_generate(seed_covalent)
    energy_covalent = ff.compute()

    seed_dispersion = configuration.create_seed(kind='dispersion')
    ff = yaff_generate(seed_dispersion)
    energy_dispersion = ff.compute()

    seed_electrostatic = configuration.create_seed(kind='electrostatic')
    ff = yaff_generate(seed_electrostatic)
    energy_electrostatic = ff.compute()

    seed_nonbonded = configuration.create_seed(kind='nonbonded')
    ff = yaff_generate(seed_nonbonded)
    energy_nonbonded = ff.compute()

    seed_full = configuration.create_seed(kind='all')
    ff = yaff_generate(seed_full)
    energy_full = ff.compute()

    assert abs(energy_covalent) > 0.0
    assert abs(energy_dispersion) > 0.0
    assert abs(energy_electrostatic) > 0.0
    np.testing.assert_almost_equal(
            energy_nonbonded,
            energy_dispersion + energy_electrostatic,
            )
    np.testing.assert_almost_equal(
            energy_full,
            energy_covalent + energy_nonbonded,
            )
예제 #3
0
def test_create_openmm_topology():
    system, _ = get_system('cau13')
    with pytest.raises(AssertionError):
        create_openmm_topology(system)

    rvecs = system.cell._get_rvecs().copy()
    transform_lower_triangular(system.pos, rvecs, reorder=True)
    reduce_box_vectors(rvecs)

    system.cell.update_rvecs(rvecs)
    topology = create_openmm_topology(system)
    # verify box vectors are correct
    a, b, c = topology.getPeriodicBoxVectors()
    assert np.allclose(
        a.value_in_unit(unit.angstrom),
        rvecs[0, :] / molmod.units.angstrom,
    )
    assert np.allclose(
        b.value_in_unit(unit.angstrom),
        rvecs[1, :] / molmod.units.angstrom,
    )
    assert np.allclose(
        c.value_in_unit(unit.angstrom),
        rvecs[2, :] / molmod.units.angstrom,
    )
예제 #4
0
def test_short_simulation(tmp_path):
    system, pars = get_system('uio66')
    configuration = Configuration(system, pars)

    # conversion
    conversion = ExplicitConversion()
    openmm_seed = conversion.apply(configuration)
    system = openmm_seed.get_system() # necessary to create Simulation object
    topology, positions = configuration.create_topology()
    a, b, c = topology.getPeriodicBoxVectors()

    # instantiate simulation for each platform
    platforms = ['Reference', 'CPU', 'CUDA', 'OpenCL']
    for name in platforms:
        integrator = mm.LangevinMiddleIntegrator(
                300 * unit.kelvin, # temperature
                0.1 * unit.picosecond, # friction coefficient
                0.5 * unit.femtosecond, # step size
                )
        try:
            platform = mm.Platform.getPlatformByName(name)
        except mm.OpenMMException:
            continue
        simulation = mm.app.Simulation(
                topology,
                system,
                integrator,
                platform,
                )
        simulation.context.setPositions(positions)
        #simulation.context.setPeriodicBoxVectors(box[0], box[1], box[2])
        simulation.context.setPeriodicBoxVectors(a, b, c)
        simulation.step(20)
예제 #5
0
def test_ff_yaff_periodic():
    ff = get_system('cau13', return_forcefield=True)
    wrapper = YaffForceFieldWrapper(ff)

    pos = ff.system.pos.copy()
    rvecs = ff.system.cell._get_rvecs().copy()

    nstates = 10
    pos_ = np.zeros((nstates,) + pos.shape)
    rvecs_ = np.zeros((nstates,) + (3, 3))
    energy_ = np.zeros(10)
    gpos_ = np.zeros(pos_.shape)
    for i in range(10):
        pos += np.random.uniform(-1, 1, size=pos.shape) / 5
        rvecs += np.random.uniform(-0.5, 0.5, size=rvecs.shape) / 5
        pos_[i, :] = pos[:] / molmod.units.angstrom
        rvecs_[i, :] = rvecs[:] / molmod.units.angstrom
        ff.update_pos(pos)
        ff.update_rvecs(rvecs)
        energy_[i] = ff.compute(gpos_[i, :], None)

    energy_w, force_w = wrapper.evaluate(pos_, rvecs=rvecs_, do_forces=True)
    np.testing.assert_almost_equal(
            energy_ / molmod.units.kjmol,
            energy_w,
            )
    np.testing.assert_almost_equal(
            - gpos_ / molmod.units.kjmol * molmod.units.angstrom,
            force_w,
            )
예제 #6
0
    def init_trainer(self, args):
        if args.gpuid:
            print('Running with GPU {}.'.format(args.gpuid[0]))
            cuda.set_device(args.gpuid[0])
        else:
            print('Running with CPU.')

        if args.random_seed:
            random.seed(args.random_seed + os.getpid())
            np.random.seed(args.random_seed + os.getpid())

        schema = Schema(args.schema_path)
        scenario_db = ScenarioDB.from_dict(schema,
                                           read_json(args.scenarios_path),
                                           Scenario)
        valid_scenario_db = ScenarioDB.from_dict(
            schema, read_json(args.valid_scenarios_path), Scenario)

        # if len(args.agent_checkpoints) == 0
        # assert len(args.agent_checkpoints) <= len(args.agents)
        if len(args.agent_checkpoints) < len(args.agents):
            ckpt = [None] * 2
        else:
            ckpt = args.agent_checkpoints

        systems = [
            get_system(name, args, schema, False, ckpt[i])
            for i, name in enumerate(args.agents)
        ]

        rl_agent = 0
        system = systems[rl_agent]
        model = system.env.model
        loss = None
        # optim = build_optim(args, [model, system.env.critic], None)
        optim = {
            'model': build_optim(args, model, None),
            'critic': build_optim(args, system.env.critic, None)
        }
        optim['critic']._set_rate(0.05)

        scenarios = {
            'train': scenario_db.scenarios_list,
            'dev': valid_scenario_db.scenarios_list
        }
        from neural.a2c_trainer import RLTrainer as A2CTrainer
        trainer = A2CTrainer(systems,
                             scenarios,
                             loss,
                             optim,
                             rl_agent,
                             reward_func=args.reward,
                             cuda=(len(args.gpuid) > 0),
                             args=args)

        self.args = args
        self.trainer = trainer
        self.systems = systems
예제 #7
0
def test_update_properties(tmp_path):
    system, pars = get_system('mil53')
    configuration = Configuration(system, pars)

    config = configuration.write()
    config['yaff']['rcut'] = 15.0
    config['yaff']['interaction_radius'] = 15.0
    configuration.update_properties(config)
    assert configuration.rcut == 15.0
예제 #8
0
def test_wrap_coordinates():
    for name in ['cau13', 'uio66', 'ppycof', 'mof5', 'mil53', 'cof5']:
        ff = get_system(name, return_forcefield=True)
        positions = ff.system.pos.copy()
        rvecs = ff.system.cell._get_rvecs().copy()
        rvecs_ = ff.system.cell._get_rvecs().copy()
        ff.update_pos(positions)
        ff.update_rvecs(rvecs)
        e = ff.compute()

        # make random periodic displacements
        for i in range(100):
            coefficients = np.random.randint(0, high=3, size=(3, 1))
            atom = np.random.randint(0, high=ff.system.natom)
            positions[atom, :] += np.sum(coefficients * rvecs, axis=0)
        ff.update_pos(positions)
        ff.update_rvecs(rvecs)
        e0 = ff.compute()
        assert np.allclose(e, e0)

        wrap_coordinates(positions, rvecs, rectangular=False)
        frac = np.dot(positions,
                      np.linalg.inv(rvecs))  # fractional coordinates
        assert np.all(frac >= 0)
        assert np.all(frac <= 1)
        assert np.allclose(rvecs, rvecs_)  # rvecs should not change
        ff.update_pos(positions)
        ff.update_rvecs(rvecs)
        e1 = ff.compute()
        assert np.allclose(e0, e1)

        with pytest.raises(AssertionError):
            wrap_coordinates(positions, rvecs, rectangular=True)

        # transform rvecs
        transform_lower_triangular(positions, rvecs, reorder=False)
        reduce_box_vectors(rvecs)
        wrap_coordinates(positions, rvecs, rectangular=True)
        for i in range(positions.shape[0]):
            assert np.all(np.abs(positions[i, :]) < np.diag(rvecs))
        ff.update_pos(positions)
        ff.update_rvecs(rvecs)
        e2 = ff.compute()
        assert np.allclose(e0, e2)

        # reorder rvecs
        transform_lower_triangular(positions, rvecs, reorder=True)
        reduce_box_vectors(rvecs)
        wrap_coordinates(positions, rvecs, rectangular=True)
        for i in range(positions.shape[0]):
            assert np.all(np.abs(positions[i, :]) < np.diag(rvecs))
        ff.update_pos(positions)
        ff.update_rvecs(rvecs)
        e3 = ff.compute()
        assert np.allclose(e0, e3)
예제 #9
0
def test_initialize_nonperiodic(tmp_path):
    system, pars = get_system('alanine')
    configuration = Configuration(system, pars)
    configuration.log_system()

    # write defaults
    path_config = tmp_path / 'config.yml'
    config = configuration.write(path_config)
    with open(path_config, 'r') as f:
        content = f.read()
    assert content == """yaff: {}\n"""
예제 #10
0
def test_save_load_pdb(tmp_path):
    system, pars = get_system('mil53')
    configuration = Configuration(system, pars)

    # YAFF and OpenMM use a different switching function. If it is disabled,
    # the results between both are identical up to 6 decimals
    configuration.switch_width = 0.0  # disable switching
    configuration.rcut = 10.0  # request cutoff of 10 angstorm
    configuration.interaction_radius = 11.0
    #configuration.update_properties(configuration.write())

    conversion = ExplicitConversion(pme_error_thres=5e-4)
    seed_mm = conversion.apply(configuration, seed_kind='all')
    seed_yaff = configuration.create_seed(kind='all')
    topology, pos = configuration.create_topology()
    box = seed_yaff.system.cell._get_rvecs() / molmod.units.angstrom

    wrapper_mm = OpenMMForceFieldWrapper.from_seed(seed_mm, 'Reference')
    wrapper_yaff = YaffForceFieldWrapper.from_seed(seed_yaff)
    assert wrapper_yaff.periodic  # system should not be considered periodic
    assert wrapper_mm.periodic  # system should not be considered periodic

    #positions = seed_yaff.system.pos.copy() / molmod.units.angstrom
    #rvecs = seed_yaff.system.cell._get_rvecs().copy() / molmod.units.angstrom

    e0, f0 = wrapper_mm.evaluate(pos, box, do_forces=True)
    e1, f1 = wrapper_yaff.evaluate(pos, box, do_forces=True)
    assert np.allclose(e0, e1, rtol=1e-3)

    path_pdb = tmp_path / 'top.pdb'
    mm.app.PDBFile.writeFile(
        topology,
        pos * unit.angstrom,
        open(path_pdb, 'w+'),
        keepIds=True,
    )

    pdb = mm.app.PDBFile(str(path_pdb))
    positions = pdb.getPositions(asNumpy=True).value_in_unit(unit.angstrom)
    a, b, c = pdb.getTopology().getPeriodicBoxVectors()
    rvecs = np.array([
        a.value_in_unit(unit.angstrom),
        b.value_in_unit(unit.angstrom),
        c.value_in_unit(unit.angstrom)
    ])

    e2, f2 = wrapper_mm.evaluate(positions, rvecs, do_forces=True)
    e3, f3 = wrapper_yaff.evaluate(positions, rvecs, do_forces=True)
    assert np.allclose(e2, e3, rtol=1e-3)
    assert np.allclose(e1, e3, rtol=1e-4)  # rounding errors during saving pdb
    assert np.allclose(e0, e2, rtol=1e-4)
예제 #11
0
def test_get_prefixes():
    system, pars = get_system('cau13')
    configuration = Configuration(system, pars)

    prefixes = configuration.get_prefixes('all')
    covalent_prefixes = configuration.get_prefixes('covalent')
    dispersion_prefixes = configuration.get_prefixes('dispersion')
    electrostatic_prefixes = configuration.get_prefixes('electrostatic')
    nonbonded_prefixes = configuration.get_prefixes('nonbonded')

    _ = dispersion_prefixes + electrostatic_prefixes
    assert tuple(sorted(_)) == tuple(sorted(nonbonded_prefixes))
    __ = covalent_prefixes + nonbonded_prefixes
    assert tuple(sorted(__)) == tuple(sorted(prefixes))
예제 #12
0
def test_lattice_reduction():
    system, pars = get_system('cau13')
    pos = system.pos.copy()
    rvecs = system.cell._get_rvecs().copy()

    # use reduction algorithm from Bekker, and transform to diagonal
    reduced = do_gram_schmidt_reduction(rvecs)
    reduced_LT = np.linalg.cholesky(reduced @ reduced.T)
    assert np.allclose(reduced_LT, np.diag(np.diag(reduced_LT)))  # diagonal

    # transform to lower triangular
    transform_lower_triangular(pos, rvecs, reorder=True)
    reduce_box_vectors(rvecs)

    # assert equality of diagonal elements from both methods
    np.testing.assert_almost_equal(np.diag(rvecs), np.diag(reduced_LT))
예제 #13
0
def test_from_files(tmp_path):
    system, pars = get_system('mil53')
    configuration = Configuration(system, pars)

    configuration.write(tmp_path / 'config.yml')
    system.to_file(str(tmp_path / 'system.chk'))
    with open(tmp_path / 'pars.txt', 'w+') as f:
        f.write(pars)

    path_system = tmp_path / 'system.chk'
    path_pars   = tmp_path / 'pars.txt'
    path_config = tmp_path / 'config.yml'
    configuration = Configuration.from_files(
            chk=path_system,
            txt=path_pars,
            yml=path_config,
            )
예제 #14
0
 def __init__(self, options, models=None):
     super(RuleBasedAgent, self).__init__(options, models=models)
     args = argparse.Namespace(
         random_seed=hash(self.options.random_seed),
         schema_path='cocoa-negotiation/fb-negotiation/data/bookhatball-schema.json',
         scenarios_path='cocoa-negotiation/fb-negotiation/data/toy-scenarios.json',
         train_examples_paths='cocoa-negotiation/fb-negotiation/data/rulebased-transcripts.json',
         train_max_examples=1,
         test_max_examples=0,
         max_turns=self.options.max_dialogue_len,
         agents=['rulebased', 'rulebased'],
         templates='cocoa-negotiation/fb-negotiation/templates.pkl',
         policy='cocoa-negotiation/fb-negotiation/model.pkl',
     )
     self.schema = Schema(args.schema_path)
     self.agent = get_system('rulebased', args, self.schema,
                             model_path='cocoa-negotiation/model.pkl')
예제 #15
0
def test_nonperiodic():
    systems = ['alanine']
    platforms = ['Reference']
    seed_kinds = ['covalent', 'dispersion', 'electrostatic']

    tolerance = {
        ('Reference', 'covalent'): 1e-6,
        ('Reference', 'dispersion'): 1e-6,
        ('Reference', 'electrostatic'): 1e-6,
        #('Cuda', 'covalent'): 1e-5,
        #('Cuda', 'dispersion'): 1e-5,
        #('Cuda', 'electrostatic'): 1e-5,
    }

    nstates = 10
    disp_ampl = 1.0
    box_ampl = 1.0

    for name in systems:
        for platform in platforms:
            for kind in seed_kinds:
                system, pars = get_system(name)
                configuration = Configuration(system, pars)
                tol = tolerance[(platform, kind)]

                conversion = ExplicitConversion()
                seed_mm = conversion.apply(configuration, seed_kind=kind)
                seed_yaff = configuration.create_seed(kind=kind)

                wrapper_mm = OpenMMForceFieldWrapper.from_seed(
                    seed_mm, platform)
                wrapper_yaff = YaffForceFieldWrapper.from_seed(seed_yaff)
                assert not wrapper_yaff.periodic  # system should not be considered periodic
                assert not wrapper_mm.periodic  # system should not be considered periodic

                pos = seed_yaff.system.pos.copy()
                for i in range(nstates):
                    dpos = np.random.uniform(-disp_ampl,
                                             disp_ampl,
                                             size=pos.shape)
                    energy_mm, forces_mm = wrapper_mm.evaluate(
                        (pos + dpos) / molmod.units.angstrom, )
                    energy, forces = wrapper_yaff.evaluate(
                        (pos + dpos) / molmod.units.angstrom, )
                    assert_tol(energy, energy_mm, tol)
                    assert_tol(forces, forces_mm, 10 * tol)
예제 #16
0
def test_ff_yaff_stress():
    def energy_func(positions, rvecs):
        ff.update_pos(positions)
        ff.update_rvecs(rvecs)
        return ff.compute()

    ff = get_system('cau13', return_forcefield=True)
    positions = ff.system.pos.copy()
    rvecs = ff.system.cell._get_rvecs().copy()
    vtens = np.zeros((3, 3))
    ff.compute(None, vtens)
    unit = molmod.units.pascal * 1e6
    pressure = np.trace(vtens) / np.linalg.det(rvecs) / unit

    dUdh = estimate_cell_derivative(positions, rvecs, energy_func, dh=1e-5)
    vtens_numerical = rvecs.T @ dUdh
    pressure_ = np.trace(vtens_numerical) / np.linalg.det(rvecs) / unit
    assert abs(pressure - pressure_) < 1e-3 # require at least kPa accuracy
    stress_  = vtens_numerical / np.linalg.det(rvecs)
    stress_ /= (molmod.units.kjmol / molmod.units.angstrom ** 3)

    wrapper = YaffForceFieldWrapper(ff)
    stress_w = wrapper.compute_stress(
            positions / molmod.units.angstrom,
            rvecs / molmod.units.angstrom,
            use_symmetric=False,
            )
    assert np.allclose(stress_w, stress_, atol=1e-5)

    # compute vtens using symmetric box, and verify compute_stress symmetric
    pos_tmp = positions.copy()
    rvecs_tmp = rvecs.copy()
    transform_symmetric(pos_tmp, rvecs_tmp)
    ff.update_pos(pos_tmp)
    ff.update_rvecs(rvecs_tmp)
    vtens = np.zeros((3, 3))
    ff.compute(None, vtens)
    stress_vtens = vtens / np.linalg.det(rvecs)
    stress_vtens /= (molmod.units.kjmol / molmod.units.angstrom ** 3)
    stress_wrapper = wrapper.compute_stress(
            positions / molmod.units.angstrom,
            rvecs / molmod.units.angstrom,
            use_symmetric=True,
            )
    assert np.allclose(stress_wrapper, stress_vtens, atol=1e-5)
예제 #17
0
def test_transform_symmetric():
    system, pars = get_system('mil53')
    pos = system.pos.copy()
    rvecs = system.cell._get_rvecs().copy()

    # transform to symmetric form
    transform_symmetric(pos, rvecs)
    assert np.allclose(rvecs, rvecs.T)

    # transform to triangular, and back to symmetric
    rvecs_ = rvecs.copy()
    pos_ = pos.copy()
    transform_lower_triangular(pos_, rvecs_, reorder=False)
    transform_symmetric(pos_, rvecs_)

    # assert equality
    np.testing.assert_almost_equal(rvecs_, rvecs)
    np.testing.assert_almost_equal(pos_, pos)
예제 #18
0
def test_create_seed_periodic():
    system, pars = get_system('cau13')
    configuration = Configuration(system, pars)
    # change parameters randomly
    with pytest.raises(ValueError): # cell is too small
        configuration.supercell = [3, 1, 1]
    configuration.rcut = 12.0
    assert configuration.interaction_radius == 12.0 # should change too
    configuration.switch_width = 5.0
    configuration.tailcorrections = False

    seed_covalent = configuration.create_seed(kind='covalent')
    ff = yaff_generate(seed_covalent)
    energy_covalent = ff.compute()

    seed_dispersion = configuration.create_seed(kind='dispersion')
    ff = yaff_generate(seed_dispersion)
    energy_dispersion = ff.compute()

    seed_electrostatic = configuration.create_seed(kind='electrostatic')
    ff = yaff_generate(seed_electrostatic)
    energy_electrostatic = ff.compute()

    seed_nonbonded = configuration.create_seed(kind='nonbonded')
    ff = yaff_generate(seed_nonbonded)
    energy_nonbonded = ff.compute()

    seed_full = configuration.create_seed(kind='all')
    ff = yaff_generate(seed_full)
    energy_full = ff.compute()

    assert abs(energy_covalent) > 0.0
    assert abs(energy_dispersion) > 0.0
    assert abs(energy_electrostatic) > 0.0
    np.testing.assert_almost_equal(
            energy_nonbonded,
            energy_dispersion + energy_electrostatic,
            )
    np.testing.assert_almost_equal(
            energy_full,
            energy_covalent + energy_nonbonded,
            )
예제 #19
0
def test_wrapper_openmm_mic():
    system, pars = get_system('mil53')
    configuration = Configuration(system, pars)
    kind = 'all'

    # YAFF and OpenMM use a different switching function. If it is disabled,
    # the results between both are identical up to 6 decimals
    configuration.switch_width = 0.0 # disable switching
    configuration.rcut = 10.0 # request cutoff of 10 angstorm
    configuration.cell_interaction_radius = 10.0
    configuration.supercell = [2, 3, 5]
    configuration.update_properties(configuration.write())
    conversion = ExplicitConversion(pme_error_thres=1e-5)
    seed_mm = conversion.apply(configuration, seed_kind=kind)
    wrapper = OpenMMForceFieldWrapper.from_seed(seed_mm, 'Reference')


    u = molmod.units.angstrom
    seed_yaff = configuration.create_seed(kind=kind)
    positions = seed_yaff.system.pos.copy() / u
    rvecs = seed_yaff.system.cell._get_rvecs().copy() / u
    e, _ = wrapper.evaluate(positions, rvecs, do_forces=True)

    # make random periodic displacements
    for i in range(5):
        coefficients = np.random.randint(-3, high=3, size=(3, 1))
        atom = np.random.randint(0, high=seed_yaff.system.natom, size=(10,))
        positions[atom, :] += np.sum(coefficients * rvecs, axis=0)
        e_ = wrapper.evaluate(positions, rvecs, do_forces=False)
        assert np.allclose(e, e_)

    # make random periodic displacements and rewrap coordinates
    for i in range(5):
        coefficients = np.random.randint(-3, high=3, size=(3, 1))
        atom = np.random.randint(0, high=seed_yaff.system.natom, size=(10,))
        positions[atom, :] += np.sum(coefficients * rvecs, axis=0)
        wrap_coordinates(positions, rvecs, rectangular=True)
        e_ = wrapper.evaluate(positions, rvecs, do_forces=False)
        assert np.allclose(e, e_)
예제 #20
0
def test_check_compatibility():
    system, _ = get_system('lennardjones')
    conversion = ExplicitConversion()
    seed_kind = 'dispersion'

    # generate pars with unsupported prefix
    pars_unsupported = """
    BLAAA:UNIT SIGMA angstrom
    BLAAA:UNIT EPSILON kcalmol
    BLAAA:SCALE 1 1.0
    BLAAA:SCALE 2 1.0
    BLAAA:SCALE 3 1.0

    # ---------------------------------------------
    # KEY      ffatype  SIGMA  EPSILON  ONLYPAULI
    # ---------------------------------------------

    BLAAA:PARS      C     2.360   0.116      0"""

    with pytest.raises(AssertionError):
        configuration = Configuration(system, pars_unsupported)

    # generate pars with unsupported scaling
    pars_unsupported = """
    LJ:UNIT SIGMA angstrom
    LJ:UNIT EPSILON kcalmol
    LJ:SCALE 1 0.5
    LJ:SCALE 2 1.0
    LJ:SCALE 3 1.0

    # ---------------------------------------------
    # KEY      ffatype  SIGMA  EPSILON  ONLYPAULI
    # ---------------------------------------------

    LJ:PARS      C     2.360   0.116      0"""

    configuration = Configuration(system, pars_unsupported)
    with pytest.raises(AssertionError):
        seed_mm = conversion.apply(configuration, seed_kind=seed_kind)
예제 #21
0
def test_transform_lower_triangular():
    for i in range(100):
        trial = np.random.uniform(-20, 20, size=(3, 3))
        trial *= np.sign(np.linalg.det(trial))
        assert np.linalg.det(trial) > 0
        pos = np.random.uniform(-100, 100, size=(10, 3))
        transform_lower_triangular(pos, trial)  # in-place
        # comparison with cholesky made inside transform_lower_triangular

    for name in ['cau13', 'uio66']:  # FAILS ON COBDP; ewald_reci changes
        ff = get_system(name, return_forcefield=True)  # nonrectangular system
        gpos0 = np.zeros((ff.system.natom, 3))
        energy0 = ff.compute(gpos0, None)
        rvecs = ff.system.cell._get_rvecs().copy()
        transform_lower_triangular(ff.system.pos, rvecs, reorder=True)
        assert is_lower_triangular(rvecs)
        ff.update_pos(ff.system.pos)
        ff.update_rvecs(rvecs)
        gpos1 = np.zeros((ff.system.natom, 3))
        energy1 = ff.compute(gpos1, None)
        np.testing.assert_almost_equal(  # energy should remain the same
            energy0,
            energy1,
        )
예제 #22
0
def add_systems(args, config_dict, schema):
    """
    Args:
        config_dict: A dictionary that maps the bot name to a dictionary containing configs for the bot. The
        dictionary should contain the bot type (key 'type') and. for bots that use an underlying model for generation,
        the path to the directory containing the parameters, vocab, etc. for the model.

    Returns:
        systems: A dict mapping from the bot name to the System object for that bot.

    """

    total_probs = 0.0
    systems = {HumanSystem.name(): HumanSystem()}
    timed = False if params['debug'] else True
    for (sys_name, info) in config_dict.iteritems():
        if "active" not in info.keys():
            warnings.warn(
                "active status not specified for bot %s - assuming that bot is inactive."
                % sys_name)
        if info["active"]:
            name = info["type"]
            try:
                model = get_system(name,
                                   args,
                                   schema=schema,
                                   timed=timed,
                                   model_path=args.checkpoint)
            except ValueError:
                warnings.warn(
                    'Unrecognized model type in {} for configuration '
                    '{}. Ignoring configuration.'.format(info, sys_name))
                continue
            systems[sys_name] = model

    return systems
예제 #23
0
    parser.add_argument('--agent-id',
                        default=0,
                        type=int,
                        help='Which agent to try')
    add_scenario_arguments(parser)
    add_system_arguments(parser)
    args = parser.parse_args()

    if args.random_seed:
        random.seed(args.random_seed)
        np.random.seed(args.random_seed)

    schema = Schema(args.schema_path)
    scenario_db = ScenarioDB.from_dict(schema, read_json(args.scenarios_path),
                                       Scenario)
    agents = [get_system('rulebased', args, schema) for _ in (0, 1)]

    configs = random_configs(10)
    scenarios = scenario_db.scenarios_list[:20]
    #scenarios = [scenarios[0] for _ in xrange(10)]
    max_turns = args.max_turns
    success_rates = []
    mean_margins = []
    margin_stes = []

    def eval_bot(params):
        success = []
        margins = []
        agent_id = args.agent_id
        baseline_agent_id = 1 - agent_id
        config = Config(**params)
예제 #24
0
    options.add_model_arguments(parser)
    args = parser.parse_args()

    if args.random_seed:
        random.seed(args.random_seed)
        np.random.seed(args.random_seed)

    schema = Schema(args.schema_path)
    scenario_db = ScenarioDB.from_dict(schema, read_json(args.scenarios_path),
                                       Scenario)
    valid_scenario_db = ScenarioDB.from_dict(
        schema, read_json(args.valid_scenarios_path), Scenario)

    assert len(args.agent_checkpoints) <= len(args.agents)
    systems = [
        get_system(name, args, schema, False, args.agent_checkpoints[i])
        for i, name in enumerate(args.agents)
    ]

    rl_agent = 0
    system = systems[rl_agent]
    model = system.env.model
    loss = make_loss(args, model, system.mappings['tgt_vocab'])
    optim = build_optim(args, model, None)

    scenarios = {
        'train': scenario_db.scenarios_list,
        'dev': valid_scenario_db.scenarios_list
    }
    trainer = RLTrainer(systems,
                        scenarios,
예제 #25
0
register_failure_args(parser)
parser.add_argument(
    "--benchmark_config",
    default="",
    help=
    "A comma separated list of benchmark parameters, eg. rate=500,duration=10.",
)
parser.add_argument(
    "-d",
    action="store_true",
    help="Debug mode, sets up mininet, then waits in Mininet.CLI",
)

args = parser.parse_args()

system = get_system(args)

topo = args.topology
topo_kwargs = (dict([arg.split("=") for arg in args.topo_args.split(",")])
               if args.topo_args != "" else {})
print(topo, topo_kwargs)
topo_module = importlib.import_module("src.topologies." + topo)

failure_provider = get_failure_provider(args)

## A list of benchmark configs with defaults. Change values as appropriate when we have an
## idea of what values *are* appropriate.
bench_defs = {
    "rate": 1,  # upper bound on reqs/sec
    "duration": 160,  # duration of operation sending in seconds
    "test_results_location": "test.res",
예제 #26
0
def test_periodic():
    systems = ['uio66', 'cau13', 'mil53', 'ppycof', 'cof5', 'mof5']
    platforms = ['Reference']
    seed_kinds = ['covalent', 'dispersion', 'electrostatic']

    # systematic constant offset in dispersion energy for COFs, unclear why

    tolerance = {
        ('Reference', 'covalent'): 1e-6,  # some MM3 terms have error 1e-7
        ('Reference', 'dispersion'): 1e-2,  # some MM3 terms have error 1e-3
        ('Reference', 'electrostatic'): 1e-3,
        #('CUDA', 'covalent'): 1e-3,
        #('CUDA', 'dispersion'): 1e-3,
        #('CUDA', 'electrostatic'): 1e-3,
    }

    nstates = 5
    disp_ampl = 0.3
    box_ampl = 0.3

    for name in systems:
        for platform in platforms:
            for kind in seed_kinds:
                system, pars = get_system(name)
                configuration = Configuration(system, pars)
                tol = tolerance[(platform, kind)]

                # YAFF and OpenMM use a different switching function. If it is disabled,
                # the results between both are identical up to 6 decimals
                configuration.switch_width = 0.0  # disable switching
                configuration.rcut = 13.0  # request cutoff of 13 angstorm
                configuration.interaction_radius = 15.0
                configuration.update_properties(configuration.write())
                conversion = ExplicitConversion(pme_error_thres=5e-4)
                seed_mm = conversion.apply(configuration, seed_kind=kind)
                seed_yaff = configuration.create_seed(kind=kind)

                wrapper_mm = OpenMMForceFieldWrapper.from_seed(
                    seed_mm, platform)
                wrapper_yaff = YaffForceFieldWrapper.from_seed(seed_yaff)
                assert wrapper_yaff.periodic  # system should not be considered periodic
                assert wrapper_mm.periodic  # system should not be considered periodic

                pos = seed_yaff.system.pos.copy()
                rvecs = seed_yaff.system.cell._get_rvecs().copy()
                for i in range(nstates):
                    dpos = np.random.uniform(-disp_ampl,
                                             disp_ampl,
                                             size=pos.shape)
                    drvecs = np.random.uniform(-box_ampl,
                                               box_ampl,
                                               size=rvecs.shape)
                    drvecs[0, 1] = 0
                    drvecs[0, 2] = 0
                    drvecs[1, 2] = 0
                    tmp = rvecs + drvecs
                    reduce_box_vectors(tmp)
                    energy_mm, forces_mm = wrapper_mm.evaluate(
                        (pos + dpos) / molmod.units.angstrom,
                        rvecs=tmp / molmod.units.angstrom,
                    )
                    energy, forces = wrapper_yaff.evaluate(
                        (pos + dpos) / molmod.units.angstrom,
                        rvecs=tmp / molmod.units.angstrom,
                    )
                    assert_tol(energy, energy_mm, tol)
                    assert_tol(forces, forces_mm, 10 * tol)
예제 #27
0
def test_implicit_nonperiodic():
    system, pars = get_system('alanine')
    configuration = Configuration(system, pars)

    conversion = ImplicitConversion()
    seed_mm = conversion.apply(configuration, seed_kind='all')
예제 #28
0
    parser.add_argument('--agent-checkpoints', nargs='+', help='Directory to learned models')
    parser.add_argument('--random-seed', help='Random seed', type=int, default=1)
    parser.add_argument('--verbose', default=False, action='store_true', help='Whether or not to have verbose prints')
    parser.add_argument('--valid-scenarios-path', help='Output path for the validation scenarios')
    cocoa.options.add_scenario_arguments(parser)
    options.add_system_arguments(parser)
    options.add_rl_arguments(parser)
    options.add_model_arguments(parser)
    args = parser.parse_args()

    if args.random_seed:
        random.seed(args.random_seed)
        np.random.seed(args.random_seed)

    schema = Schema(args.schema_path)
    scenario_db = ScenarioDB.from_dict(schema, read_json(args.scenarios_path), Scenario)
    valid_scenario_db = ScenarioDB.from_dict(schema, read_json(args.valid_scenarios_path), Scenario)

    assert len(args.agent_checkpoints) <= len(args.agents)
    systems = [get_system(name, args, schema, False, args.agent_checkpoints[i]) for i, name in enumerate(args.agents)]

    rl_agent = 0
    system = systems[rl_agent]
    model = system.env.model
    loss = make_loss(args, model, system.mappings['tgt_vocab'])
    optim = build_optim(args, model, None)

    scenarios = {'train': scenario_db.scenarios_list, 'dev': valid_scenario_db.scenarios_list}
    trainer = RLTrainer(systems, scenarios, loss, optim, rl_agent, reward_func=args.reward)
    trainer.learn(args)
예제 #29
0
def add_systems(args, config_dict, schema, debug=False):
    """
    Params:
    config_dict: A dictionary that maps the bot name to a dictionary containing configs for the bot. The
        dictionary should contain the bot type (key 'type') and. for bots that use an underlying model for generation,
        the path to the directory containing the parameters, vocab, etc. for the model.
    Returns:
    agents: A dict mapping from the bot name to the System object for that bot.
    pairing_probabilities: A dict mapping from the bot name to the probability that a user is paired with that
        bot. Also includes the pairing probability for humans (backend.Partner.Human)
    """

    total_probs = 0.0
    systems = {HumanSystem.name(): HumanSystem()}
    pairing_probabilities = {}
    timed = False if debug else True
    print 'timed:', timed
    for (sys_name, info) in config_dict.iteritems():
        if "active" not in info.keys():
            warnings.warn(
                "active status not specified for bot %s - assuming that bot is inactive."
                % sys_name)
        if info["active"]:
            name = info["type"]
            try:
                # TODO: model related arguments should be in config_dict (read from params.json), instead of in command line args, currently we are assuming there is only one model that needs `checkpoint`
                model = get_system(name,
                                   args,
                                   schema=schema,
                                   timed=timed,
                                   model_path=args.checkpoint)
            except ValueError:
                warnings.warn(
                    'Unrecognized model type in {} for configuration '
                    '{}. Ignoring configuration.'.format(info, sys_name))
                continue
            systems[sys_name] = model
            if 'prob' in info.keys():
                prob = float(info['prob'])
                pairing_probabilities[sys_name] = prob
                total_probs += prob

    # TODO: clean up pairing probabilities (obsolete)
    if total_probs > 1.0:
        raise ValueError("Probabilities for active bots can't exceed 1.0.")
    if len(pairing_probabilities.keys()) != 0 and len(
            pairing_probabilities.keys()) != len(systems.keys()):
        remaining_prob = (1.0 - total_probs) / (
            len(systems.keys()) - len(pairing_probabilities.keys()))
    else:
        remaining_prob = 1.0 / len(systems.keys())
    inactive_bots = set()
    for system_name in systems.keys():
        if system_name not in pairing_probabilities.keys():
            if remaining_prob == 0.0:
                inactive_bots.add(system_name)
            else:
                pairing_probabilities[system_name] = remaining_prob

    for sys_name in inactive_bots:
        systems.pop(sys_name, None)

    return systems, pairing_probabilities
예제 #30
0
    parser.add_argument('--max-examples',
                        default=20,
                        type=int,
                        help='Number of test examples to predict')
    parser.add_argument('-v',
                        '--verbose',
                        default=False,
                        action='store_true',
                        help='whether or not to have verbose prints')
    cocoa.options.add_scenario_arguments(parser)
    cocoa.options.add_dataset_arguments(parser)
    options.add_system_arguments(parser)
    args = parser.parse_args()
    if args.random_seed:
        random.seed(args.random_seed)
        np.random.seed(args.random_seed)

    schema = Schema(args.schema_path)
    scenario_db = ScenarioDB.from_dict(schema, read_json(args.scenarios_path),
                                       Scenario)

    assert len(args.agent_checkpoints) == len(args.agents)
    agents = [
        get_system(name, args, schema, model_path=model_path)
        for name, model_path in zip(args.agents, args.agent_checkpoints)
    ]
    num_examples = args.scenario_offset

    generate_examples(num_examples, scenario_db, args.results_path,
                      args.max_examples, args.remove_fail, args.max_turns)