def test_shannon_entropy(self): entropy = cpl.shannon_entropy('1111111') self.assertEqual(entropy, 0) entropy = cpl.shannon_entropy('0000000') self.assertEqual(entropy, 0) entropy = cpl.shannon_entropy('01010101') self.assertEqual(entropy, 1.0) entropy = cpl.shannon_entropy('00010001') np.testing.assert_almost_equal(entropy, 0.8113, decimal=4) entropy = cpl.shannon_entropy('1234') self.assertEqual(entropy, 2.0)
def entropyValue(x: list) -> float: fitness = abs(cpl.shannon_entropy(x)) return fitness
cellular_automaton = np.array([[0] * 40 + [1] * 20 + [0] * 40]) rule = cpl.ReversibleRule(cellular_automaton[0], 122) cellular_automaton = cpl.evolve(cellular_automaton, timesteps=1000, apply_rule=rule) timestep = [] bientropies = [] shannon_entropies = [] average_cell_entropies = [] apentropies = [] for i, c in enumerate(cellular_automaton): timestep.append(i) bit_string = ''.join([str(x) for x in c]) bientropies.append(cpl.ktbien(bit_string)) shannon_entropies.append(cpl.shannon_entropy(bit_string)) average_cell_entropies.append( cpl.average_cell_entropy(cellular_automaton[:i + 1])) apentropies.append(cpl.apen(bit_string, m=1, r=0)) print("%s, %s, %s, %s" % (i, bientropies[-1], shannon_entropies[-1], apentropies[-1])) plt.figure(1) plt.title("KTBiEn") plt.plot(timestep, bientropies) plt.figure(2) plt.title("Shannon Information") plt.plot(timestep, shannon_entropies) plt.figure(3)