All Downloads are FREE. Search and download functionalities are using the official Maven repository.

us.ihmc.simulationconstructionset.util.simulationRunner.StateFileComparer Maven / Gradle / Ivy

There is a newer version: 0.25.2
Show newest version
package us.ihmc.simulationconstructionset.util.simulationRunner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import us.ihmc.simulationconstructionset.DataFileReader;
import us.ihmc.yoVariables.registry.YoRegistry;
import us.ihmc.yoVariables.registry.YoVariableList;
import us.ihmc.yoVariables.variable.YoDouble;

public class StateFileComparer
{
   /**
    * Compares two state files and returns a list of variables that differ by more than a given amount.
    * If variables differ, those variables' values are set to the absolute difference.
    *
    * @param filenameOne     file name for first state file
    * @param filenameTwo     file name for second state file
    * @param maxAbsoluteDiff max difference percentage between each YoVariable in state file 1 and
    *                        state file 2
    * @param exceptions      variables to skip while comparing state files
    * @return a VarList containing the YoVariables from state file one that differ more than
    *         maxAbsoluteDiff from those in state file 2
    */
   public static ArrayList absoluteCompareStateFiles(String filenameOne, String filenameTwo, double maxAbsoluteDiff,
                                                                         ArrayList exceptions)
   {
      File fileOne = new File(filenameOne);
      File fileTwo = new File(filenameTwo);
      return absoluteCompareStateFiles(fileOne, fileTwo, maxAbsoluteDiff, exceptions);
   }

   /**
    * Compares two state files and returns a list of variables that differ by more than a given amount.
    * If variables differ, those variables' values are set to the absolute difference.
    *
    * @param fileOne         file name for first state file
    * @param fileTwo         file name for second state file
    * @param maxAbsoluteDiff max difference percentage between each YoVariable in state file 1 and
    *                        state file 2
    * @param exceptions      variables to skip while comparing state files
    * @return a VarList containing the YoVariables from state file one that differ more than
    *         maxAbsoluteDiff from those in state file 2
    */
   public static ArrayList absoluteCompareStateFiles(File fileOne, File fileTwo, double maxAbsoluteDiff, ArrayList exceptions)
   {
      return compareStateFiles(fileOne, fileTwo, maxAbsoluteDiff, false, exceptions);
   }

   /**
    * Compares two state files and returns a list of variables that differ by more than a given
    * percentage. If variables differ, those variables' values are set to the percentage difference.
    *
    * @param filenameOne    file name for first state file
    * @param filenameTwo    file name for second state file
    * @param maxPercentDiff max difference percentage between each YoVariable in state file 1 and state
    *                       file 2
    * @param exceptions     variables to skip while comparing state files
    * @return a VarList containing the YoVariables from state file one that differ more than
    *         maxPercentDiff from those in state file 2
    */
   public static ArrayList percentualCompareStateFiles(String filenameOne, String filenameTwo, double maxPercentDiff,
                                                                           ArrayList exceptions)
   {
      File fileOne = new File(filenameOne);
      File fileTwo = new File(filenameTwo);

      return percentualCompareStateFiles(fileOne, fileTwo, maxPercentDiff, exceptions);
   }

   /**
    * Compares two state files and returns a list of variables that differ by more than a given
    * percentage. If variables differ, those variables' values are set to the percentage difference.
    *
    * @param file1          first state file
    * @param file2          second state file
    * @param maxPercentDiff max difference percentage between each YoVariable in state file 1 and state
    *                       file 2
    * @param exceptions     variables to skip while comparing state files
    * @return a VarList containing the YoVariables from state file one that differ more than
    *         maxPercentDiff from those in state file 2
    */
   public static ArrayList percentualCompareStateFiles(File fileOne, File fileTwo, double maxPercentDiff, List exceptions)
   {
      return compareStateFiles(fileOne, fileTwo, maxPercentDiff, true, exceptions);
   }

   private static ArrayList compareStateFiles(File fileOne, File fileTwo, double maxDifference, boolean checkForPercentDifference,
                                                                  List exceptions)
   {
      DataFileReader dataFileReaderOne = new DataFileReader(fileOne);
      DataFileReader dataFileReaderTwo = new DataFileReader(fileTwo);

      YoVariableList varListOne = new YoVariableList("VarListOne");
      YoVariableList varListTwo = new YoVariableList("VarListTwo");

      YoRegistry registryOne = new YoRegistry("root");
      YoRegistry registryTwo = new YoRegistry("root");

      try
      {
         dataFileReaderOne.readState(varListOne, true, false, registryOne);
         dataFileReaderTwo.readState(varListTwo, true, false, registryTwo);
      }
      catch (Exception e)
      {
         e.printStackTrace();
         System.out.flush();
         System.err.flush();
      }

      return compareVarLists(varListOne, varListTwo, maxDifference, checkForPercentDifference, exceptions);
   }

   public static ArrayList compareVarLists(YoVariableList varListOne, YoVariableList varListTwo, double maxDifferenceAllowed,
                                                               boolean checkForPercentDifference, List exceptions)
   {
      VariablesThatShouldMatchList list = new VariablesThatShouldMatchList(varListOne, varListTwo, exceptions);
      ArrayList variableDifferences = new ArrayList<>();

      YoDouble timeYoVariable = (YoDouble) varListOne.findVariable("t");
      double time = Double.NaN;
      if (timeYoVariable != null)
      {
         time = timeYoVariable.getDoubleValue();
      }

      list.doVariableValuesMatch(variableDifferences, time, maxDifferenceAllowed, checkForPercentDifference);
      return variableDifferences;
   }

   public static void main(String[] args)
   {
      //    String filenameOne = "Tests/test_2.2999.state";
      //    String filenameTwo = "Tests/test_2.2999_Rewind.state";

      String filenameOne = "Tests/test_2.270000.state";
      String filenameTwo = "Tests/test_2.270000_Rewind.state";

      //    String filenameOne = "090122_testAfter1_01Sec.state";
      //    String filenameTwo = "090122_testAfter1_01Sec_Rewind.state";

      //    String filenameOne = "090122_testAfter1_25Sec.state";
      //    String filenameTwo = "090122_testAfter1_25Sec_Rewind.state";

      //    String filenameOne = "090122_testAfter1_61Sec.state";
      //    String filenameTwo = "090122_testAfter1_61Sec_Rewind.state";

      //    String filenameOne = "090122_testAfter5_4Sec.state";
      //    String filenameTwo = "090122_testAfter5_4Sec_Rewind.state";

      //    String filenameOne = "090122_testAfter6Sec.state";
      //    String filenameTwo = "090122_testAfter6Sec_Rewind.state";

      double maxPercentDiff = 0.05;

      ArrayList variableDifferences = StateFileComparer.percentualCompareStateFiles(filenameOne, filenameTwo, maxPercentDiff, null);

      for (VariableDifference variableDifference : variableDifferences)
      {
         System.out.println(variableDifference);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy