0

I'm new to scala (from python) and I'm trying to create a Json object that has dynamic keys. I would like to use some starting number as the top-level key and then combinations involving that number as second-level keys.

From reading the play-json docs/examples, I've seen how to build these nested structures. While that will work for the top-level keys (there are only 17 of them), this is a combinatorial problem and the power set contains ~130k combinations that would be the second-level keys so it isn't feasible to list that structure out. I also saw the use of a case class for structures, however the parameter name becomes the key in those instances which is not what I'm looking for.

Currently, I'm considering using HashMaps with the MultiMap trait so that I can map multiple combinations to the same original starting number and then second-level keys would be the combinations themselves.

I have python code that does this, but it takes 3-4 days to work through up-to-9-number combinations for all 17 starting numbers. The ideal final format would look something like below.

Perhaps it isn't possible to do in scala given the goal of using immutable structures. I suppose using regex on a string of the output might be an option as well. I'm open to any solutions regarding data structures to hold the info and how to approach the problem. Thanks!

{
  "2": {
    "(2, 3, 4, 5, 6)": {
        "best_permutation": "(2, 4, 3, 5, 6)",
        "amount": 26.0
      },
    "(2, 4, 5, 6)": {
      "best_permutation": "(2, 5, 4, 6)",
      "amount": 21.0
    }
  },
  "3": {
    "(3, 2, 4, 5, 6)": {
      "best_permutation": "(3, 4, 2, 5, 6)",
      "amount": 26.0
    },
    "(3, 4, 5, 6)": {
      "best_permutation": "(3, 5, 4, 6)",
      "amount": 21.0
    }
  }
}

EDIT: There is no real data source other than the matrix I'm using as my lookup table. I've posted the links to the lookup table I'm using and the program if it might help, but essentially, I'm generating the content myself within the code.

For a given combination, I have a function that basically takes the first value of the combination (which is to be the starting point) and then uses the tail of that combination to generate a permutation.

After that I prepend the starting location to the front of each permutation and then use sliding(2) to work my way through the permutation looking up the amount which is in a breeze.linalg.DenseMatrix by using the two values to index the matrix I've provided below and summing the amounts gathered by indexing the matrix with the two sliding values (subtracting 1 from each value to account for the 0-based indexing).

At this point, it is just a matter of gathering the information (starting_location, combination, best_permutation and the amount) and constructing the nested HashMap. I'm using scala 2.11.8 if it makes any difference.

MATRIX: see here. PROGRAM:see here.

Jason Wolosonovich
  • 494
  • 1
  • 10
  • 13
  • What does your origin data structure look like (i.e. where are you getting the keys/values from) post an example of the Scala or python you have with the source data? – zcourts Aug 09 '17 at 05:05
  • @zcourts I've posted the links to the matrix and my program. Thank you for taking a look! Open to any suggestions for libraries, approaches (coding style, etc., lol) – Jason Wolosonovich Aug 09 '17 at 05:47

0 Answers0