MySQL Rollup with multiple levels - mysql

Hello I am attempting to do a roll-up of an x number of levels. The hierarchy is something like this:
1 - Total Depth 1
1.1, 1.2, 1.3 - Depth 2
1.1.1, 1.1.2, 1.1.3, 1.2.1, 1.2.2 - Depth 3
And so on and so forth...
I thought of using a Substring Index to compare and rollup but I can't seem to figure out how to formulate the query to not care how many levels there are.
For example if I want 1.1.1, I would also like to retrieve 1.1 and 1. Getting all the results would show the drill-down classification.
Any suggestions would be greatly appreciated!
This is what I am currently using as a workaround
Path is the 1, 1.1, 1.1.1 field
IF(depth=3, SUBSTRING(path, 1, 3) , SUBSTRING(path, 1, 1)) as `Depth3`,
IF(depth=2, SUBSTRING(path, 1, 5) , SUBSTRING(path, 1, 1)) as `Depth2`
This will give me 1.1 and 1 along with the path field of 1.1.1 but it required me to specify the depth.

Related

reinforcement learning model design - how to add upto 5

I am experimenting with reinforcement learning in python using Keras. Most of the tutorials available use OpenAI Gym library to create the environment, state, and action sets.
After practicing with many good examples written by others, I decided that I want to create my own reinforcement learning environment, state, and action sets.
This is what I think will be fun to teach the machine to do.
An array of integers from 1 to 4. I will call these targets.
targets = [[1, 2, 3, 4]]
Additional numbers list (at random) from 1 to 4. I will call these bullets.
bullets = [1, 2, 3, 4]
When I shoot a bullet to a target, the target's number will be the sum of original target num + bullet num.
I want to shoot a bullet (one at a time) at one of the targets to make
For example, given targets [1 2 3 4] and bullet 1, I want the machine to predict the correct index to shoot at.
In this case, it should be index 3, because 4 + 1 = 5
curr_state = [[1, 2, 3, 4]]
bullet = 1
action = 3 (<-- index of the curr_state)
next_state = [[1, 2, 3, 5]]
I have been picking my brain to think of the best way to construct this into a reinforcement design. I tried some, but the model result is not very good (meaning, it most likely fails to make number 5).
Mostly because the state is a 2D: (1) targets; (2) bullet at that time. The method I employed so far is to convert the state as the following:
State = 5 - targets - bullet
I was wondering if anyone can think of a better way to design this model?
Thanks in advance!
Alright, so it looks like no one is helping you out, so I just wrote a Python environment file for you as you described. I also made it as much OpenAI style for you as possible, here is the link to it, it is in my GitHub repository. You can copy the code or fork it. I will explain it below:
https://github.com/RuiNian7319/Miscellaneous/blob/master/ShootingRange.py
States = [0, 1, 2, ..., 10]
Actions = [-2, -1, 0, 1, 2]
So the game starts at a random number between 0 - 10 (you can change this easily if you want), and the random number is your "target" you described above. Given this target, your AI agent can fire the gun, and it shoots bullets corresponding to the numbers above. The objective is for your bullet and the target to add up to 5. There are negatives in case your AI agent overshoots 5, or if the target is a number above 5.
To get a positive reward, the agent has to get 5. So if the current value is 3, and the agent shoots 2, then the agent will get a reward of 1 since he got the total value of 5, and that episode will end.
There are 3 ways for the game to end:
1) Agent gets 5
2) Agent fails to get 5 in 15 tries
3) The number is above 10. In this case, we say the target is too far
Sometimes, you need to shoot multiple times to get 5. So, if your agent shoots, its current bullet will be added to the state, and the agent tries again from that new state.
Example:
Current state = 2. Agent shoots 2. New state is 4. And the agent starts at 4 at the next time step. This "sequential decision making" creates a reinforcement learning environment, rather than a contextual bandit.
I hope this makes sense, let me know if you have any questions.

Additional nested IIF

I'm really hoping someone can help with this, I'm not really hot on the finer points of SSRS and I'm struggling to find any articles that answer my query, or at least point me in the right direction.
I am trying to write an report that looks at the grades a student gets for each unit they take, change this from a "D" or "M' etc... to a value; 1 or 2 etc... and then gives me the average based upon the number of units they completed.
I've managed to get this far, but I now need to output a field that shows if the average value is between 1.0 and 1.5 display "Pass", between 1.6 and 2.5 display "Merit" and between 2.6 and 3.0 display "Distinction".
This is where I've got thus far:
=SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0))))/Fields!CompletedUnits.Value
I think I may need another IIF in front of the SUM, but I'm really not too sure.
Any help would be massively appreciated
Thanks
S
Try using this expression:
=Switch(
(SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0)))) /
Fields!CompletedUnits.Value) > 2.6, "Distinction",
(SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0)))) /
Fields!CompletedUnits.Value) > 1.5, "Merit",
(SUM(IIF(Fields!ActualGrade.Value="D",3,IIF(Fields!ActualGrade.Value="M",2,IIF(Fields!ActualGrade.Value="P",1,0)))) /
Fields!CompletedUnits.Value) > 0.9, "Pass",
true,"Nothing"
)
Also if you want to show the average in a column and the determined string (Metir, Pass, and Distinction) you can reference the text value by doing someting like this:
=Switch(
ReportItems!TextBox14.Value > 2.6, "Distinction",
ReportItems!TextBox14.Value > 1.5, "Merit",
ReportItems!TextBox14.Value > 0.9, "Pass",
true,"Nothing"
)
Where TextBox14 is the textbox where you put the Sum(IIF(...))/YouField expression.
Hope this helps.

proper translation of multi-dimensional array to JSON (and back)

Is there a standard way of translating multi-dimensional arrays to JSON and back? Does this depend on the language and the relationship between byte ordering and rows/columns/pages etc.? I am working working in Matlab. In Matlab, an array of integers, with values of say 1 through 10, with a shape of 5 x 2, would have the following 2-D layout:
1 6
2 7
3 8
4 9
5 10
as opposed to:
1 2
3 4
5 6
7 8
9 10
The question is, if I translate the 2-D array into a JSON string, should I get:
[[1,2,3,4,5],[6,7,8,9,10]]
or
[[1,6],[2,7],[3,8],[4,9],[5,10]]
My preference would be the first case, since it is sequential in terms of memory access.
So the explicit question then is, which way are n-d arrays written?
By memory - innermost should be sequential in memory
high to low dimensions - innermost is lowest dimension (which in this case matches the memory for Matlab)
low to high dimensions - innermost is highest dimension
If you say you have a 5x2 array, in JS you probably mean you have an array of length of 5, each element of which is an array of length 2. So the "layout" you get is:
[ [a00, a01],
[a10, a11],
[a20, a21],
[a30, a31],
[a40, a41] ]
Decoding that in terms of 1..10 is entirely up to you.
an array of integers, with values of say 1 through 10, with a shape of 5 x 2
There is no single correct way to put values 1..10 into an array 5x2. So [[1,6],[2,7],[3,8],[4,9],[5,10]] would be just as good as [[1,2],[3,4],[5,6],[7,8],[9,10]].

Sorting by preferred average value

I'm making a game that involves people downloading and rating user-created maps. They have the option to upvote/downvote the map if they like/dislike it, as well as rate its difficult on a scale of 1-10.
In the map browser, they have the option to sort maps by highest rated. This is done using Laplace smoothing so that it factors both the number of upvotes as well as the total number of ratings into the sorting, sorting by (upvotes + 1)/(numRatings + 2). This works fine.
Now, there's also an option to sort by preferred difficulty, where people can choose a value from 1-10, and it will sort maps by how close the average difficulty rating is to the preferred rating. At first I was sorting by ABS(preferred_difficulty - average_difficulty), but that didn't factor in the number of ratings. Right now I'm using ((numRatings + 1) * (10 - ABS(average_difficulty - preferred_difficulty)) + 1) / (numRatings + 1.5) out of sheer trial and error, which kinda works, but sometimes the number of ratings outweighs the preferred difficulty and the results look strange.
This is what I need help with - I can't figure out how to sort by smallest difference between preferred and average difficulty while incorporating number of ratings into the mix, since I want a low difficulty delta with a high rating count to be the best result instead of a high upvote count with a high rating count, like it is with the ratings.
For example, if this is the data:
AvgDifficulty NumRatings
6.0 1
4.0 25
6.8 4
6.2 3
6.5 20
6.2 1
6.4 3
And someone chooses a preferred difficulty of 6.4, I'd want it to sort something like this:
AvgDifficulty NumRatings
6.5 20
6.4 3
6.2 3
6.8 4
6.2 1
6.0 1
4.0 25
Basically I want results that are close to the preferred difficulty at the top, but I'd rather show results that are 0.1 off with lots of ratings over exact matches with very few ratings. I understand getting "right" results may not be very concrete in this case, I'm just looking for a starting point.
Thanks for the help!
You have a bit of a nebulous question. You need some way to combine the rating and the count. The basic query is:
order by abs(avg_difficulty - 6.4)
But, you want to include the count as well. You can define a fixed band at the top and order these by the ranking:
order by (case when abs(avg_difficulty - 6.4) < 0.1) then numratings end) desc,
abs(avg_difficulty - 6.4)
This expression combines everything from 6.3 - 6.5 in one group and sorts these by the number of ratings. The second key sorts everything else by the difference.

What is the name of this data structure or technique of using relative difference between sequence members

Let's say I have a sequence of values (e.g., 3, 5, 8, 12, 15) and I want to occasionally decrease all of them by a certain value.
If I store them as the sequence (0, 2, 3, 4, 3) and keep a variable as a base of 3, I now only have to change the base (and check the first items) whenever I want to decrease them instead of actually going over all the values.
I know there's an official term for this, but when I literally translate from my native language to English it doesn't come out right.
Differential Coding / Delta Encoding?
I don't know a name for the data structure, but it's basically just base+offset :-)
An offset?
If I understand your question right, you're rebasing. That's normally used in reference to patching up addresses in DLLs from a load address.
I'm not sure that's what you're doing, because your example seems to be incorrect. In order to come out with { 3, 5, 8, 12, 15 }, with a base of 3, you'd need { 0, 2, 5, 9, 12 }.
I'm not sure. If you imagine your first array as providing the results of some function of an index value f(i) where f(0) is 3, f(1) is 5, and so forth, then your second array is describing the function f`(i) where f(i+1) = f(i) + f'(i) given f(0) = 3.
I'd call it something like a derivative function, where the process of retrieving your original data is simply the summation function.
What will happen more often, will you be changing f(0) or retrieving values from f(i)? Is this technique rooted in a desire to optimize?
Perhaps you're looking for a term like "Inductive Sequence" or "Induction Sequence." (I just made that up.)