Skip to content

Inverse sampling can fail #2

@figlerg

Description

@figlerg

This is a separate issue to #1. It is possible that the root finding procedure terminates (no npmath problems) but somehow fails to find a solution after the loop has terminated. Investigate this.

cmd command:

python.exe main.py -p experiments/spec_23_traffic_flow.tre -n 15 --nr_samples 4 --visualize 
Parsed Expression:
(<keepLane>_[25,50]+<changeLaneOnce>_[25,50])*
Traceback (most recent call last):
  File "C:\Users\giglerf\PycharmProjects\VolTRE\main.py", line 99, in <module>
    experiment()
  File "C:\Users\giglerf\PycharmProjects\VolTRE\main.py", line 85, in experiment
    w = sample(ctx, n=args.length, T=args.duration, mode=mode, budget=args.budget)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\giglerf\PycharmProjects\VolTRE\sample\sample.py", line 126, in sample
    w,rej = smart_sampling(phi_dis, n, T, mode, lambdas,node, f)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\giglerf\PycharmProjects\VolTRE\sample\sample.py", line 136, in smart_sampling
    w = sample_unambig(node=phi_dis, n=n, T=T, mode=mode, lambdas=lambdas, top=True)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\giglerf\PycharmProjects\VolTRE\sample\sample.py", line 190, in sample_unambig
    T = pdf.inverse_sampling()
        ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\giglerf\PycharmProjects\VolTRE\volume\VolumePoly.py", line 635, in inverse_sampling
    raise InverseSamplingException()
misc.helpers.InverseSamplingException: Inverse sampling failed.

Process finished with exit code 1

The problem is likely in the loop over segments in VolumePoly.inverse_sampling(). This may be the case for the other pdf/cdf classes too.

def inverse_sampling(self):
  """
  Assumes this is a pdf and simplified.
  :return: A sample of the distribution.
  """

  u = random.uniform(0, 1)

  assert abs(self.pdf.total_volume() -1) < 0.0001, ("Invalid distribution. This is no pdf. "
                                                "It is likely this is caused by a bug."
                                                "\n Note: This function is usually used through the function sample in sample.py. Tho use it directly as a method, call vol_pol.pdf().inverse_sampling()")

  cdf = self.pdf.integral()

  for (a, b), p in cdf.pairs:

      # this is the only case we need to consider, immediately return
      if p(a) <= u <= p(b):
          # solutions = sympy.solve(p - u, T)  # would fail for large n

          # solutions = sympy.polys.polytools.real_roots(p - u)  # weird output format and errors

          solutions = sympy.polys.polytools.nroots(p - u, n=10, maxsteps=50, cleanup=True)

          # ignore complex solutions, cast to float
          solutions = [float(sol) for sol in solutions if sol.is_real]

          for sol in solutions:
              if a <= sol <= b:
                  ## the solutions appear to be good
                  # plt.plot(sol, u, 'ro')
                  # cdf.plot()
                  # plt.show()
                  return sol
  # print(self)
  # print(u)
  # self.plot()
  # self.integral().plot()
  raise InverseSamplingException()

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions