<commit_before>/*
 * Copyright © 2016 <NAME>. All rights reserved.
 */

package io.vivarium.serialization;

import static org.junit.Assert.assertEquals;

import java.util.LinkedList;

import org.junit.Test;

import com.google.common.collect.Lists;
import com.johnuckele.vtest.Tester;

import io.vivarium.core.Blueprint;
import io.vivarium.core.Creature;
import io.vivarium.core.Species;
import io.vivarium.core.World;
import io.vivarium.core.processor.NeuralNetwork;

public class JSONSerializationTest
{
    @Test
    public void testWorldSerializeAndDeserialize()
    {
        // Build a world with creatures
        Species species = Species.makeDefault();
        species.setNormalizeAfterMutation(Math.sqrt(42));
        species.setRandomInitialization(false);
        Blueprint blueprint = Blueprint.makeDefault();
        blueprint.setSpecies(Lists.newArrayList(species));
        World world = new World(blueprint);

        // Convert to json
        String jsonString = JSONConverter.serializerToJSONString(world);

        // Deserialize
        World deserializeWorld = JSONConverter.jsonStringToSerializerCollection(jsonString).getFirst(World.class);

        // Deep compare of creature brains
        LinkedList<Creature> originalCreatures = world.getCreatures();
        LinkedList<Creature> deserializedCreatures = deserializeWorld.getCreatures();
        Tester.equal("Creature counts should be the same", originalCreatures.size(), deserializedCreatures.size());
        for (int index = 0; index < originalCreatures.size(); index++)
        {
            Creature originalCreature = originalCreatures.get(index);
            Creature deserializedCreature = originalCreatures.get(index);
            if (originalCreature.getProcessor() instanceof NeuralNetwork)
            {
                NeuralNetwork originalCreatureProcessor = (NeuralNetwork) originalCreature.getProcessor();
                NeuralNetwork deserializedCreatureProcessor = (NeuralNetwork) deserializedCreature.getProcessor();
                double[][][] originalCreatureProcessorWeights = originalCreatureProcessor.getWeights();
                double[][][] deserializedCreatureProcessorWeights = deserializedCreatureProcessor.getWeights();
                compareNeuralNetworkWeights(originalCreatureProcessorWeights, deserializedCreatureProcessorWeights);
                assertEquals(Math.sqrt(42), originalCreatureProcessor.getGenomeLength(), 0.0000000001);
                assertEquals(Math.sqrt(42), deserializedCreatureProcessor.getGenomeLength(), 0.0000000001);
            }
        }
    }

    private void compareNeuralNetworkWeights(double[][][] weights1, double[][][] weights2)
    {
        for (int i = 0; i < weights1.length; i++)
        {
            for (int j = 0; j < weights1[i].length; j++)
            {
                for (int k = 0; k < weights1[i][j].length; k++)
                {
                    assertEquals(weights1[i][j][k], weights2[i][j][k], 0);
                }
            }
        }

    }
}
<commit_msg>Switch comparison to use deep auto-generated comparisons.<commit_after>/*
 * Copyright © 2016 <NAME>. All rights reserved.
 */

package io.vivarium.serialization;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.google.common.collect.Lists;

import io.vivarium.core.Blueprint;
import io.vivarium.core.Species;
import io.vivarium.core.World;

public class JSONSerializationTest
{
    @Test
    public void testWorldSerializeAndDeserialize()
    {
        // Build a world with creatures
        Species species = Species.makeDefault();
        species.setNormalizeAfterMutation(Math.sqrt(42));
        species.setRandomInitialization(false);
        Blueprint blueprint = Blueprint.makeDefault();
        blueprint.setSpecies(Lists.newArrayList(species));
        World world = new World(blueprint);

        // Convert to json
        String jsonString = JSONConverter.serializerToJSONString(world);

        // Deserialize
        World deserializeWorld = JSONConverter.jsonStringToSerializerCollection(jsonString).getFirst(World.class);

        // Deep compare of the worlds
        assertEquals(world, deserializeWorld);
    }
}

{'fraction_non_alphanumeric': 0.09959591157594486, 'fraction_numerical': 0.01188495364868077, 'mean_word_length': 3.113391984359726, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5532243', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1101, 'n_words': 225}