File: test_element_operators.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (79 lines) | stat: -rw-r--r-- 2,587 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def test_element_operators(seed):
    import numpy as np
    from ase import Atoms
    from ase.ga.element_crossovers import OnePointElementCrossover

    # set up the random number generator
    rng = np.random.RandomState(seed)

    a1 = Atoms('SrSrSrBaClClClClBrBrBrBr')
    a1.info['confid'] = 1
    a2 = Atoms('CaCaMgBaFFFFFFFF')
    a2.info['confid'] = 2

    cations = ['Sr', 'Ba', 'Ca', 'Mg']
    anions = ['Cl', 'F', 'Br']
    op = OnePointElementCrossover([cations, anions], [3, 2], [.25, .5],
                                  rng=rng)

    a3, desc = op.get_new_individual([a1, a2])

    syms = a3.get_chemical_symbols()
    assert len(set([i for i in syms if i in cations])) < 4
    assert len(set([i for i in syms if i in anions])) < 3

    from ase.ga.element_mutations import RandomElementMutation

    op = RandomElementMutation([cations, anions], [3, 2], [.25, .5], rng=rng)
    a4, desc = op.get_new_individual([a1])
    syms = a4.get_chemical_symbols()

    assert len(set([i for i in syms if i in cations])) < 4
    assert len(set([i for i in syms if i in anions])) < 3

    op = RandomElementMutation(anions, 2, .5, rng=rng)
    a4, desc = op.get_new_individual([a2])
    syms = a4.get_chemical_symbols()

    assert len(set([i for i in syms if i in anions])) == 2

    from ase.ga.element_mutations import MoveDownMutation
    from ase.ga.element_mutations import MoveUpMutation
    from ase.ga.element_mutations import MoveRightMutation
    from ase.ga.element_mutations import MoveLeftMutation

    a1 = Atoms('SrSrClClClCl')
    a1.info['confid'] = 1
    op = MoveDownMutation(cations, 2, .5, rng=rng)
    a2, desc = op.get_new_individual([a1])
    a2.info['confid'] = 2

    syms = a2.get_chemical_symbols()
    assert 'Ba' in syms
    assert len(set(syms)) == 3

    op = MoveUpMutation(cations, 1, 1., rng=rng)
    a3, desc = op.get_new_individual([a2])
    syms = a3.get_chemical_symbols()
    assert 'Ba' not in syms
    assert len(set(syms)) == 2

    cations = ['Co', 'Ni', 'Cu']
    a1 = Atoms('NiNiBrBr')
    a1.info['confid'] = 1
    op = MoveRightMutation(cations, 1, 1., rng=rng)
    a2, desc = op.get_new_individual([a1])
    a2.info['confid'] = 2
    syms = a2.get_chemical_symbols()

    assert len(set(syms)) == 2
    assert len([i for i in syms if i == 'Cu']) == 2

    op = MoveLeftMutation(cations, 2, .5, rng=rng)
    a3, desc = op.get_new_individual([a2])
    syms = a3.get_chemical_symbols()

    from ase.ga import set_raw_score, get_raw_score
    assert len(set(syms)) == 3
    set_raw_score(a3, 5.0)
    assert get_raw_score(a3) == 5.0