Opened 5 months ago

Closed 4 months ago

#2976 closed defect (done)

SampleProportionalWithoutRepetition with Windowing may cause a NullReferenceException

Reported by: bburlacu Owned by: bburlacu
Priority: medium Milestone: HeuristicLab 3.3.16
Component: Random Version: trunk
Keywords: Cc:

Description

In some cases trying to sample proportionally from a sequence with windowing causes an exception.

The following code demonstrates the issue:

using System;
using System.Linq;
using System.Collections.Generic;

using HeuristicLab.Core;
using HeuristicLab.Common;
using HeuristicLab.Random;

public class MyScript : HeuristicLab.Scripting.CSharpScriptBase {
  public override void Main() {
    var rand = new FastRandom(1234);
    
    var weights = new[] { 0.1, 0.1, 0.1, 0.9, 0.9, 0.1, 0.9, 0.9 };
    var seq = Enumerable.Range(0, weights.Length).ToArray();
    
    var sampled = seq.SampleProportionalWithoutRepetition(rand, seq.Length, weights, windowing: true).ToArray();
  }
}

Exception:

HeuristicLab version: 3.3.15.15589
NullReferenceException: Object reference not set to an instance of an object.
   at HeuristicLab.Random.RandomEnumerable.<SampleProportionalWithoutRepetition>d__9`1.MoveNext() in C:\Projects\hl-core\trunk\HeuristicLab.Random\3.3\RandomEnumerable.cs:line 210
   at System.Linq.Enumerable.<TakeIterator>d__25`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at MyScript.Main() in c:\Users\Bogdan\AppData\Local\Temp\j5j04ynv.0.cs:line 16
   at HeuristicLab.Scripting.ExecutableScript.Execute() in C:\Projects\hl-core\trunk\HeuristicLab.Scripting\3.3\ExecutableScript.cs:line 80

Change History (4)

comment:1 Changed 5 months ago by bburlacu

  • Status changed from new to accepted

comment:2 Changed 5 months ago by bburlacu

  • Owner changed from bburlacu to gkronber
  • Status changed from accepted to reviewing

r16496: Add null check to avoid NullReferenceException.

comment:3 Changed 5 months ago by abeham

  • Owner changed from gkronber to bburlacu
  • Status changed from reviewing to readytorelease

I reviewed the change in r16496. This may happen due to numerical instabilities. The null check prevents looping past the last element.

comment:4 Changed 4 months ago by bburlacu

  • Resolution set to done
  • Status changed from readytorelease to closed

r16580: Merged r16496 into stable.

Note: See TracTickets for help on using tickets.