浏览代码

Merge pull request #61 from Unity-Technologies/sampler-zeroing-out-fix

Better hashing used for iterating random sampler states
/main
GitHub 4 年前
当前提交
d6d48f68
共有 3 个文件被更改,包括 57 次插入13 次删除
  1. 44
      com.unity.perception/Runtime/Randomization/Samplers/SamplerUtility.cs
  2. 23
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerUtilityTests.cs
  3. 3
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerUtilityTests.cs.meta

44
com.unity.perception/Runtime/Randomization/Samplers/SamplerUtility.cs


}
/// <summary>
/// Generates new a new random state by deterministically combining a base seed and an iteration index
/// Hashes using constants generated from a program that maximizes the avalanche effect, independence of
/// output bit changes, and the probability of a change in each output bit if any input bit is changed.
/// Source: https://github.com/h2database/h2database/blob/master/h2/src/test/org/h2/test/store/CalculateHashConstant.java
/// <param name="index">Usually the current scenario iteration or framesSinceInitialization</param>
/// <param name="baseSeed">The seed to be offset</param>
/// <returns>A new random state</returns>
public static uint IterateSeed(uint index, uint baseSeed)
{
return ShuffleSeed(index + 1) * baseSeed;
/// <param name="x">Unsigned integer to hash</param>
/// <returns>The calculated hash value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static uint Hash32(uint x) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
/// Returns a shuffled a seed value
/// Based on splitmix64: http://xorshift.di.unimi.it/splitmix64.c
/// <param name="x">64-bit value to hash</param>
/// <returns>The calculated hash value</returns>
static uint ShuffleSeed(uint seed)
static ulong Hash64(ulong x) {
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ul;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebul;
x ^= (x >> 31);
return x;
}
/// <summary>
/// Generates new a new non-zero random state by deterministically hashing a base seed with an iteration index
/// </summary>
/// <param name="index">Usually the current scenario iteration or framesSinceInitialization</param>
/// <param name="baseSeed">The seed to be offset</param>
/// <returns>A new random state</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint IterateSeed(uint index, uint baseSeed)
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
return seed;
var state = (uint)Hash64(((ulong)index << 32) | baseSeed);
return state == 0u ? largePrime : state;
}
/// <summary>

23
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerUtilityTests.cs


using NUnit.Framework;
using UnityEngine.Perception.Randomization.Samplers;
namespace RandomizationTests.SamplerTests
{
[TestFixture]
public class SamplerUtilityTests
{
[Test]
public void SeedIteratedByChangingEitherBaseSeedOrIndex()
{
// Check if changing the index changes the generated random state
var iteratedIndex1 = SamplerUtility.IterateSeed(0, 0);
var iteratedIndex2 = SamplerUtility.IterateSeed(1, 0);
Assert.AreNotEqual(iteratedIndex1, iteratedIndex2);
// Check if changing the seed changes the generated random state
var iteratedSeed1 = SamplerUtility.IterateSeed(0, 0);
var iteratedSeed2 = SamplerUtility.IterateSeed(0, 1);
Assert.AreNotEqual(iteratedSeed1, iteratedSeed2);
}
}
}

3
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerUtilityTests.cs.meta


fileFormatVersion: 2
guid: b2db5be5ad4544b899d7bed088b7b762
timeCreated: 1597985947
正在加载...
取消
保存