Free cookie consent management tool by TermsFeed Policy Generator

Opened 5 years ago

Closed 5 years ago

Last modified 4 years 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:


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();


HeuristicLab version:
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 (5)

comment:1 Changed 5 years ago by bburlacu

  • Status changed from new to accepted

comment:2 Changed 5 years 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 years 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 5 years ago by bburlacu

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

r16580: Merged r16496 into stable.

comment:5 Changed 4 years ago by gkronber

r17161: add info that r16496 has been merged from trunk to stable (in r16580)

Note: See TracTickets for help on using tickets.