get "path" of connected ids - lua

I'm just confusing myself over and over ...
I have the following table containing the information which id is connected to what other ids. And I need to find the "cheapest" connection from ID1 to ID2
local ids = {
[15] = {
[18] = {
},
[23] = {
},
[24] = {
},
},
[18] = {
[15] = {
},
[21] = {
},
[50] = {
},
[248] = {
},
[330] = {
},
[378] = {
},
[914] = {
},
[1185] = {
},
},
[21] = {
[18] = {
},
[20] = {
},
},
[248] = {
[18] = {
},
},
}
The expected result is:
local table_path = {
15,
18,
21,
}

I guess the cheapest connection should be the shortest, so what you want to do is a Breadth-first search. you need to save the path to each node additionally since this is what you want to get. Once you have found ID2 you can stop and take the path. Since this is Breadth-first, the first time you find ID2 is also guaranteed to be the shortest path.
https://en.wikipedia.org/wiki/Breadth-first_search
I just searched a bit more and found that:
BFS algorithm using Lua that finds the shortest path between 2 nodes
In the answer to this there is an implemented Breadth-first search in Lua. You could start from this and make changes for your use case if needed.

Related

Trying to figure out how to Grab part of a table. (hard to explain fully)

So, what i am trying to do is... well it's best to show. oh and This is with Fivem and trying to modify an existing resource, but...
`BMProducts = {
{
["lostItems"] = {
[1] = { name = "weapon_shotgun", price = 1500, crypto = 2500, amount = 1 },
},
}
}
table.insert(BMProducts, {
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
})
Config.Products = BMProducts`
And I have a another config, that i need to pull the Correct table, but now sure entirely how
`["products"] = Config.Products["ballasItems"],`
Is what I have but it won't read it, due to what I assume is what i saw when debugging, that when inserting to the table, it assigns a number, ie;
[1] = {lostitems = {... [2] = {ballasItems = {...
One that works, but what my ultimate goal is to make the code plug and play with the table inserts, is this
`BMProducts =
{
["lostItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
}`
which works with the config above because the way just above does not assign numbers and not inserting into a table. Any ideas how i can go about setting that config for the correct Products table?
When i try it with the table insert, and with the
`["products"] = Config.Products["ballasItems"],`
It can't find the table, which is due to what i assume the table format being different than what it was, which was the code block at the bottom
so my main thing, is to get
`["products"] = Config.Products["ballasItems"],`
to = the correct table when there is a table insert.
If you don't want to table.insert, then don't table.insert:
BMProducts["ballasItems"] = {
{ name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
}
Now I think you told to not modify the config, so another approach is to just use that additional array index when indexing:
["products"] = Config.Products[2]["ballasItems"]
Which obviously assumes that the config never changes and the entry with ballasItems is on index 2.
If you do not know the index, you may want to iterate over Config.Products and look for the right product.
for i,v in ipairs(Config.Products) do
if v["ballasItems"] then
end
end
You may also consider the case where two or more products match.

How to insert data and tables into an existing one?

I have a trouble inserting new rows and tables into an already existing one.
Lets call the source SourceFile.lua and its simplified contents:
SourceFile = {};
SourceFile.list = {
BrandName1 = {
number = 10,
products = {
"Product1", 3,
"Product2", 4,
"Product3", 7,
},
},
BrandName2 = {
number = 5,
products = {
"Product1", 10,
"Product2", 3,
"Product3", 6,
},
},
-- and so on
}
I want to do something like this:
require 'SourceFile'
local myData = {
BrandName2 = { -- add new products for the existing brand
products = {
"Product4", 2,
},
},
MyBrandName1 = { -- add new brand
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
},
-- and so on
}
table.insert(SourceFile.list, myData)
However there's something wrong in my code and I get the following result (printed with inspect):
{
list = { {
BrandName2 = {
products = { "Product4", 2 }
},
MyBrandName1 = {
number = 12,
products = { "MyProduct1", 21, "MyProduct2", 95 }
}
},
BrandName1 = {
number = 10,
products = { "Product1", 3, "Product2", 4, "Product3", 7 }
},
BrandName2 = {
number = 5,
products = { "Product1", 10, "Product2", 3, "Product3", 6 }
}
}
}
What am I doing wrong?
I'm new to lua and pretty sure that it's something obvious, but not for me. Please, help me.
Addition
After these answers I've also found a way to insert new brand names one by one:
SourceFile.list.MyBrandName1 = {
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
}
This does not fully answer my question, but might be useful to someone new to lua.
table.insert adds its second argument to an array (its first argument). Your SourceFile.list is only supposed to have string keys, so it can't work as an array. You'll need a recursive function to merge the data from one table into the other:
local function meld(data, newData)
for k, v in pairs(newData) do
local oldValue = data[k]
if type(oldValue) ~= 'table' or type(v) ~= 'table' then
-- One of the values is not a table, so let's clobber the old value.
data[k] = v
else
-- Both are tables.
meld(oldValue, v)
end
end
end
meld(SourceFile.list, myData)
You are pushing a table of brandnames into a list of brandnames.
Which makes it a list of brandnames + table with brandnames.
table.insert(SourceFile.list, myData)
This inserts myData to SourceFile.list. myData is a table with brandnames.
SourceFile.list is also a table with brandnames.
List in list.
You have 2 choices to solve this:
Insert each brandname separately
Make a function to merge contents of myData to SourceFile.list

Metric math alarms: How can I use a for_each expression to loop over metrics within a dynamic block?

I am trying to create dynamic metric math alarms, that are configurable with a JSON.
I am struggling with looping over the metric alarm with a for_each expression as this is a loop within a loop.
Here is an example of what I am trying to do:
resource "aws_cloudwatch_metric_alarm" "Percentage_Alert" {
for_each = var.percentage_error_details
locals { alarm_details = each.value }
alarm_name = "${terraform.workspace}-${each.key}"
comparison_operator = local.alarm_details["Comparison_Operator"]
evaluation_periods = "1"
threshold = local.alarm_details["Threshold"]
metric_query {
id = "e1"
expression = local.alarm_details["Expression"]
label = local.alarm_details["Label"]
return_data = "true"
}
dynamic "metric_query" {
for metric in each.value["Metrics"]{
id = metric.key
metric_name = metric.value
period = local.alarm_details["Period"]
stat = local.alarm_details["Statistic"]
namespace = local.full_namespace
unit = "Count"
}
}
}
And this is the sample JSON
{
"locals": {
"Name": {
"Name": "metric_math",
"Metrics": {
"m1": "Sucess",
"m2": "Failure"
},
"Expression": "100*(m2/(m1+m2))",
"Threshold" : 1,
"Period": 25,
"Priority": "critical",
"Statistic": "Sum",
"Label": "label",
"Comparison_Operator": "GreaterThanOrEqualToThreshold"
}
}
}
And this is the error message i'm getting:
Error: Invalid block definition
On ../modules/cloudwatch/metriclogfilter/main.tf line 89: Either a quoted
string block label or an opening brace ("{") is expected here.
Any help would be much appreciated.

Sankey Diagram unable to deliver using the json output

Using data from database I am trying to simulate the sankey diagram working JSFiddle.
I am assembling my data using the below code
// sdata.php
<?php
$con = sqlsrv_connect($server, $options);
if (!$con){die('Could not connect: ' . sqlsrv_error());}
$sql_query = "select * from test_data";
$result = sqlsrv_query($con, $sql_query);
$series = array();
$series['type'] = 'sankey';
$series['name'] = 'Gendata';
$series['keys'] = '[\'from\',\'to\',\'weight\']';
while($r = sqlsrv_fetch_array($result))
{
$series1 = array();
$series1[] = $r['PARENT'];
$series1[] = $r['CHILD'];
$series1[] = $r['DGEN'];
$series['data'][] = $series1;
}
$result = array();
array_push($result,$series);
print json_encode($result, JSON_NUMERIC_CHECK);
sqlsrv_close($con);
?>
My JSON looks like
[{
"type":"sankey",
"name":"Gendata",
"keys":"['from','to','weight']",
"data":[
["GROUP","COAL",24.46], ["GROUP","GAS",11.96],["GROUP","HYDRO",19.36],
["HYDRO","HYD",19.36], ["COAL","ER2",22.4],["GAS","NR",19]
]
}]
My Chart rending code looks like
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
showAxes: true
},
yAxis: [{
lineWidth: 1,
tickPositions: [0, 1, 2, 3]
}],
title: {
text: 'Sankey Diagram'
},
series: []
}
$.getJSON("sdata.php", function(resp) {
console.log(resp);
options.series[0] = resp[1]; //option 1 to assign the data in series
//options.series.push.resp; //option 2 to push the data in series
chart = new Highcharts.Chart(options);
});
});
but I am failing. I am unable to find the error I am missing
Kindly help me.
Let me know if I can be of any further information.
From the code you have posted here, the error is your keys assignment.
You have:
"keys":"['from','to','weight']",
But it needs to be:
"keys": ['from','to','weight'],
That is, the array should not be surrounded by quotes, because then it will be interpreted as a string.
In your PHP that would mean:
$series['keys'] = '[\'from\',\'to\',\'weight\']';
Needs to be:
$series['keys'] = ['from', 'to', 'weight'];
Working example: https://jsfiddle.net/ewolden/aeh02djx/

Highcharts Sunburst levels radius

I'm making a sunburst with Highcharts .NET,
This is how i setup the chart:
Highcharts higcharts = new Highcharts
{
Chart = new Chart
{
Type = ChartType.Sunburst,
Width = 700,
Height = 700
},
Title = new Title
{
Text = "Monthly Average Temperature",
X = -20
},
Subtitle = new Subtitle
{
Text = "Source: WorldClimate.com",
X = -20
},
Legend = new Legend
{
Layout = LegendLayout.Vertical,
Align = LegendAlign.Right,
VerticalAlign = LegendVerticalAlign.Middle,
BorderWidth = 0
},
Series = new List<Series>
{
new SunburstSeries
{
Name ="Test",
Data = data,
//LevelSize = new SunburstSeriesLevelSize
//{
// Unit = SunburstSeriesLevelSizeUnit.Percentage,
// Value = 100
//},
Levels = new List<SunburstSeriesLevels>
{
new SunburstSeriesLevels
{
LevelSize = new SunburstSeriesLevelsLevelSize{
Unit = SunburstSeriesLevelsLevelSizeUnit.Percentage,
Value = 90
}
},
new SunburstSeriesLevels
{
LevelSize = new SunburstSeriesLevelsLevelSize{
Unit = SunburstSeriesLevelsLevelSizeUnit.Percentage,
Value = 10
}
}
}
}
}
};
I tried many ways but the levels radius never change, did i miss something?
The only one working is the levelsize of the entire serie but i need to set the size for a specific level.
I tried to search but it looks like nobody already encountered any problem.
Level's object levelSize is able do control the size of individual level. It has two properties: unit (pixels / percentage / weight) and value (determined by the unit):
levels: [{
level: 1,
levelIsConstant: false,
levelSize: {
unit: 'pixels',
value: 30
}
}, {
level: 2,
colorByPoint: true,
dataLabels: {
rotationMode: 'parallel'
}
}, {
level: 3,
levelIsConstant: true,
levelSize: {
unit: 'weight',
value: 2
}
}, {
level: 4,
levelIsConstant: true,
levelSize: {
unit: 'percentage',
value: 30
}
}]
Live demo: http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/sunburst-levelsize/
API reference: https://api.highcharts.com/highcharts/series.sunburst.levelSize

Resources