d0c-s4vage/gramfuzz

Thanks for implementing the feature but there is a small bug

docfate111 opened this issue · 8 comments

After creating the issue, checkboxes will appear where [] label exist in the
markdown. You can check/uncheck them to fill out the environment section.

Checklist

  • [ x ] I have included the [relevant portions of the] grammar used that caused the bug
  • I have filled out the environment section

Environment

Platform

  • Windows
  • [ x ] Mac
  • Linux
  • Other (please specify)

Python Version

  • [ x ] Python 2.7
  • Python 3.4
  • Python 3.5
  • Python 3.6
  • Python 3.7
  • Python 3.8
  • Other (please specify)

Describe the bug

A clear and concise description of what the bug is.
I am getting does not add to 1.0 when the probabilities do.
I am currently using my own weightedOr but when I switch to the one in gramfuzz under Python2.7 I get errors. This code builds in main.py
https://github.com/mgree/smoosh-fuzz/blob/master/src/posix/words.py

Steps to reproduce

Expected Behavior

A clear and concise description of what you expected to happen.
I expected the grammar to run. Also I was using Python3 and am having trouble installing the package. In Python2.7 it works fine but I get the bug about probabilities not summing to 1.

Thanks for reporting this! I'll take a look

Okay

I tested the words.py grammar and didn't see any errors (I removed the from weightedOr import * and uncommented the uncomment this when gramfuzz updates lines).

Are you still seeing errors about probabilities summing to 1.0?

Yes
Screen Shot 2020-04-23 at 11 20 28 PM
Screen Shot 2020-04-23 at 11 20 43 PM

Did you run python main.py or python3 main.py?
https://github.com/mgree/smoosh-fuzz in src/scriptGeneration directory on macOS

When I use my own WeightedOr:
Screen Shot 2020-04-23 at 11 24 52 PM
This is the output expected.

I missed the notification emails for this - I'll take a look again!

Ah, I got it. The problem is when it it bails early with shortest=True in the WeightedOr::build() function:

    def build(self, pre=None, shortest=False):
        """
        :param list pre: The prerequisites list
        :param bool shortest: Whether or not the shortest reference-chain (most minimal) 
            version of the field should be generated.
        """
        if pre is None:
            pre = []

        # self.shortest_vals will be set by the GramFuzzer and will
        # contain a list of value options that have a minimal reference chain
        # 
        # see https://narly.me/posts/controlling-recursion-depth-in-grammars/
        # for an in-depth discussion
        if shortest and self.shortest_vals is not None:
            chosen_weights = [self.weights[idx] for idx in self.shortest_indices]
            chosen_vals = self.shortest_vals
        else:
            chosen_weights = self.weights
            chosen_vals = self.values

        val = rand.weighted_choice(chosen_vals, chosen_weights)
        return utils.val(val, pre, shortest=shortest)

The weights need to be scaled if shortest is set to True and the shortest chosen vals aren't the same as the normal values.