Related
Sorry if the title's worded a bit weird but this is what I'm trying to get to.
Lets say you have an array of integers like this:
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
What would be the best way to use this array to return a new array but with the values of each pair summed together, so using the array above, it would return
{ 3, 7, 11, 15, 19 }
Obviously you can so something basic like
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var x = new List<int>();
for (int i=0;i<arr.Length;i+=2)
x.Add(arr[i] + arr[i+1]);
But if you want to keep your code concise and/or work with different items, this doesn't really seem like the best option.
So is there any other/better way to do this? I was testing some ideas with Enumerable.Aggregate but couldn't come up with anything, please share ideas.
I would argue that your approach is perfectly fine, but for LINQ you can do some stuff with Chunk:
var x = arr
.Chunk(2)
.Where(c => c.Length == 2)
.Select(c => c.Sum())
.ToArray();
Well, your code is readable and efficien; however, you can generalize your current solution a bit:
Last array item(s) can well have no pair
We can combine not only pairs, but three or four items etc.
If you are looking for such kind of code for
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
the modifiction of your solution can be:
const int size = 2;
int[] result = new int[arr.Length / size + Math.Sign(arr.Length % size)];
for (int i = 0; i < arr.Length; ++i)
result[i / size] += arr[i];
For instance, if we set size = 3; then for we'll get { 6, 15, 24, 10 } as the result. Note, that the last group is incomplete:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
| | | | | | ||
6 15 24 10
I am trying to implement VRPTW of Google OR-Tools. But I am facing an issue. When I pass the dynamic Time matrix then the solution object is null but when I pass the Time matrix which is given in example then it worked.
Here is my code
public class DataModel
{
public long[,] DistanceMatrix { get; set; }
public long[,] TimeMatrix = {
//commented matrix is dynamic generated
// {0,5,20,10,0,5},
//{5,0,25,10,5,5},
//{20,25,0,30,20,20},
//{10,10,30,0,10,15},
//{0,5,20,10,0,5},
//{5,5,20,15,5,0},
{0, 6, 9, 8, 7, 3},
{6, 0, 8, 3, 2, 6},
{9, 8, 0, 11, 10, 6},
{8, 3, 11, 0, 1, 7},
{7, 2, 10, 1, 0, 6},
{3, 6, 6, 7, 6, 0},
};
public long[,] TimeWindows = {
{0, 5}, // depot
{7, 12}, // 1
{10, 15}, // 2
{16, 18}, // 3
{10, 13}, // 4
{0, 5}, // 5
};
public int VehicleNumber = 3;
public int Depot = 0;
};
Here is the main function code
DataModel data = new DataModel();
// data.TimeMatrix = TimeMatrix;
// Create Routing Index Manager
RoutingIndexManager manager = new RoutingIndexManager(
data.TimeMatrix.GetLength(0),
data.VehicleNumber,
data.Depot);
// Create a Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Create and register a transit callback.
int transitCallbackIndex = routing.RegisterTransitCallback(
(long fromIndex, long toIndex) =>
{
// Convert from routing variable Index to distance matrix NodeIndex.
var fromNode = manager.IndexToNode(fromIndex);
var toNode = manager.IndexToNode(toIndex);
return data.TimeMatrix[fromNode, toNode];
}
);
// Define the cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Add Distance constraint.
routing.AddDimension(
transitCallbackIndex, // transit callback
30, // allow waiting time
30, // vehicle maximum capacities
false, // start cumul to zero
"Time");
RoutingDimension timeDimension = routing.GetMutableDimension("Time");
// Add time window constraints for each location except depot.
for (int i = 1; i < data.TimeWindows.GetLength(0); ++i)
{
long index = manager.NodeToIndex(i);
timeDimension.CumulVar(index).SetRange(
data.TimeWindows[i, 0],
data.TimeWindows[i, 1]);
}
// Add time window constraints for each vehicle start node.
for (int i = 0; i < data.VehicleNumber; ++i)
{
long index = routing.Start(i);
timeDimension.CumulVar(index).SetRange(
data.TimeWindows[0, 0],
data.TimeWindows[0, 1]);
}
// Instantiate route start and end times to produce feasible times.
for (int i = 0; i < data.VehicleNumber; ++i)
{
routing.AddVariableMinimizedByFinalizer(
timeDimension.CumulVar(routing.Start(i)));
routing.AddVariableMinimizedByFinalizer(
timeDimension.CumulVar(routing.End(i)));
}
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
operations_research_constraint_solver.DefaultRoutingSearchParameters();
searchParameters.FirstSolutionStrategy =
FirstSolutionStrategy.Types.Value.PathCheapestArc;
// Solve the problem.
Assignment solution = routing.SolveWithParameters(searchParameters); //it is null when dynamic time matrix is used, but it is not null when time matrix mentioned in example is used.
The problem seems to be in AddDimension Method. I am struck in this but could not find any solution. Please suggest any solution.
Solution is null indicates that the solver was not able to find any feasible solution. Most likely your time windows are too tight.
Either try relaxing your time windows
or make sure nodes are optional (using addDisjunction).
I'm trying to solve a non square linear system with Math.net.
But I get an error Matrix dimensions must agree: 3x7.
Here is some example code:
using MathNet.Numerics.LinearAlgebra;
var mBuilder = Matrix<double>.Build;
var vBuilder = Vector<double>.Build;
var A = mBuilder.DenseOfArray(new double[,]
{
{ 3, 2, 1, 5, -1, 0, 0 },
{ 2, 1, 1, 2, 0, -1, 0 },
{ 5, 1, 3, 4, 0, 0, -1 }
});
var b = vBuilder.DenseOfArray(new double[] { -3, -5, -2 });
Vector<double> x;
x = A.Solve(b);
Cleary the system has a solution (e.g. X = {0, 0, 0, 0, 3, 5, 2}).
How can I solve such a system with Math.Net?
You can not use the Matrix.Solve function with a non-square matrix because there is no inverse and no unique solutions for a rectangular matrix. Google "inverse of rectangular matrix" for explanations galore. You can use pseudoinverse however, as shown below.
var mBuilder = Matrix<double>.Build;
var A = mBuilder.DenseOfArray(new double[,]
{
{ 3, 2, 1, 5, -1, 0, 0 },
{ 2, 1, 1, 2, 0, -1, 0 },
{ 5, 1, 3, 4, 0, 0, -1 }
});
Matrix<double> b = Matrix<double>.Build.Dense(3, 1);
b[0, 0] = -3.0;
b[1, 0] = -5.0;
b[2, 0] = -2.0;
var p = A.PseudoInverse();
var x = p * b;
// verify
var o = A * x;
I'm trying to reverse engineer a calculation that was done on an old program but can't quite get it. I need a count of how many values are in the top 27%, middle 46%, and bottom 27%.
I have the following data sets with eleven values in each and what the program yields in regards to the percentages and the number of values that fall into those percentiles.
Upper 27%: 4, Middle 46%: 4, Lower 27%: 3
values: 8,9,10,11,11,11,11,12,12,12,13
Upper 27%: 5, Middle 46%: 4, Lower 27%: 2
values: 2,3,4,4,4,4,5,5,5,5,5
Upper 27%: 2, Middle 46%: 8, Lower 27%: 1
values: 2,4,4,4,4,4,4,4,4,5,5
Upper 27%: 2, Middle 46%: 6, Lower 27%: 3
values: 13,17,17,18,19,19,19,21,21,23,24
I have found formulas such as (n * p) where n is the number of values and p is the percentile but it doesn't seem to work across all of these data sets to give the same results. I'm a little lost and haven't found anything that produces the results here.
I have tested code that I have found on the internet but none have worked on the different data sets.
Code sample that I tried:
internal static double percentile(double[] sortedData, double p)
{
if (p >= 100.0d) return sortedData[sortedData.Length - 1];
double position = (double)(sortedData.Length + 1) * p / 100.0;
double leftNumber = 0.0d, rightNumber = 0.0d;
double n = p / 100.0d * (sortedData.Length - 1) + 1.0d;
if (position >= 1)
{
leftNumber = sortedData[(int)System.Math.Floor(n) - 1];
rightNumber = sortedData[(int)System.Math.Floor(n)];
}
else
{
leftNumber = sortedData[0]; // first data
rightNumber = sortedData[1]; // first data
}
if (leftNumber == rightNumber)
return leftNumber;
else
{
double part = n - System.Math.Floor(n);
return leftNumber + part * (rightNumber - leftNumber);
}
}
Is there a formula or a name for what I am trying to do? Am I on the right track with percentile ranks?
You're on the right path. This is indeed the percentile rank formula. My initial thought was to get the value at the 27th percentile (which looking at your code it looks like you started down this path as well) and figure out how many values were greater than or less than; but the values you provided don't support these numbers very well.
Therefore the approach I took was to calculate the percentile rank of each number and put it into a count if they matched the percentile you have above. This seems to be the approach they took.
Formula (check out this website for more info):
PR% = L + ( 0.5 x S ) / N
Where,
L = Number of below rank,
S = Number of same rank,
N = Total numbers.
Code:
var lower = 0;
var middle = 0;
var upper = 0;
// var values = new int[] { 8, 9, 10, 11, 11, 11, 11, 12, 12, 12, 13 };
// var values = new int[] { 2, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
// var values = new int[] { 2, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5 };
var values = new int[] { 13, 17, 17, 18, 19, 19, 19, 21, 21, 23, 24 };
var n = values.Length;
foreach(var i in values)
{
var pr = ((values.Count(v => v < i) + (.5 * values.Count(v => v == i))) / n);
if (pr < .27)
lower += 1;
else if (pr > .73)
upper += 1;
else
middle += 1;
}
Console.WriteLine("Upper: " + upper);
Console.WriteLine("Middle: " + middle);
Console.WriteLine("Lower: " + lower);
I should have 5 columns which stack the second and third values.
Instead, I get two columns:
The first stacking the second value of each column.
The second column stacking the last 4 values.
I'm not sure exactly what I'm doing wrong. I have been battling with Syncfusion for the past week trying to get, what I would have thought to be, fairly basic slides built for a client. However, this has been a nightmare so far.
// Creates the two charts that go on the presentation
IPresentationChart chart = slide.Charts.AddChart(150, 100, 300, 125);
// Set data values
chart.ChartData.SetValue(1, 1, date1);
chart.ChartData.SetValue(2, 1, date2);
chart.ChartData.SetValue(3, 1, date3);
chart.ChartData.SetValue(4, 1, date4);
chart.ChartData.SetValue(5, 1, date5);
chart.ChartData.SetValue(1, 2, mains1);
chart.ChartData.SetValue(2, 2, mains2);
chart.ChartData.SetValue(3, 2, mains3);
chart.ChartData.SetValue(4, 2, mains4);
chart.ChartData.SetValue(5, 2, mains5);
chart.ChartData.SetValue(1, 3, variance1);
chart.ChartData.SetValue(2, 3, variance2);
chart.ChartData.SetValue(3, 3, variance3);
chart.ChartData.SetValue(4, 3, variance4);
chart.ChartData.SetValue(5, 3, variance5);
// Chart 1
// Set data range, Title and Category settings
chart.PrimaryCategoryAxis.CategoryType = OfficeCategoryType.Category;
chart.ChartTitle = "";
chart.ChartArea.Fill.Transparency = 0.5;
IOfficeChartSerie serie= chart.Series.Add(date1);
serie.Values = chart.ChartData[1, 2, 1, 3];
serie.SerieType = OfficeChartType.Column_Stacked;
IOfficeChartSerie serie2 = chart.Series.Add(date2);
serie2.Values = chart.ChartData[2, 2, 2, 3];
serie2.SerieType = OfficeChartType.Column_Stacked;
IOfficeChartSerie serie3 = chart.Series.Add(date3);
serie3.Values = chart.ChartData[3, 2, 3, 3];
serie3.SerieType = OfficeChartType.Column_Stacked;
IOfficeChartSerie serie4 = chart.Series.Add(date4);
serie4.Values = chart.ChartData[4, 2, 4, 3];
serie4.SerieType = OfficeChartType.Column_Stacked;
IOfficeChartSerie serie5 = chart.Series.Add(date5);
serie5.Values = chart.ChartData[5, 2, 5, 3];
serie5.SerieType = OfficeChartType.Column_Stacked;
chart.PlotArea.Layout.ManualLayout.Height = 0.9;
chart.PlotArea.Layout.ManualLayout.Width = 1;
chart.PlotArea.Layout.ManualLayout.Left = 0;
chart.PlotArea.Layout.ManualLayout.Top = 0;
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[1, 1, 5, 1];
chart.Legend.IncludeInLayout = false;
chart.HasLegend = false;
The number of columns in the column-stacked chart depends on the number of categories available in the data range. In your code snippet, there are only two category values. So, two columns are displayed in the column - stacked chart.
Example: serie.Values = chart.ChartData[1, 2, 1, 3];
Here the category values are [ 1, 2 ] and [ 1 , 3 ].
We have also manually created a chart in Microsoft PowerPoint for your scenario (in your code snippet) and attached the created PowerPoint presentation in below link.
http://www.syncfusion.com/downloads/support/directtrac/general/pp/Chart1370190114.pptx
We have also modified your code snippet to display the 5 columns. Please find the code snippet as below.
IPresentationChart chart = slide.Charts.AddChart(150, 100, 300, 125);
chart.ChartType = OfficeChartType.Column_Stacked;
chart.ChartData.SetValue(1, 1, "4355");
chart.ChartData.SetValue(2, 1, "4356");
chart.ChartData.SetValue(3, 1, "4357");
chart.ChartData.SetValue(4, 1, "4358");
chart.ChartData.SetValue(5, 1, "4359");
chart.ChartData.SetValue(1, 2, "6");
chart.ChartData.SetValue(2, 2, "7");
chart.ChartData.SetValue(3, 2, "8");
chart.ChartData.SetValue(4, 2, "9");
chart.ChartData.SetValue(5, 2, "10");
chart.ChartData.SetValue(1, 3, "11");
chart.ChartData.SetValue(2, 3, "12");
chart.ChartData.SetValue(3, 3, "13");
chart.ChartData.SetValue(4, 3, "14");
chart.ChartData.SetValue(5, 3, "15");
//Set data range, Title and category settings
chart.PrimaryCategoryAxis.CategoryType = OfficeCategoryType.Category;
chart.ChartTitle = "";
chart.ChartArea.Fill.Transparency = 0.5;
IOfficeChartSerie serie = chart.Series.Add("date1");
//Selecting data from first row second column to fifth row second column
//ChartData[startRow,startColumn,endRow,endColumn]
serie.Values = chart.ChartData[1, 2, 5, 2]; //Modified the data range to have 5 columns
serie.SerieType = OfficeChartType.Column_Stacked;
IOfficeChartSerie serie2 = chart.Series.Add("date2");
//Selection data from first row third column to fifth row third column
serie2.Values = chart.ChartData[1, 3, 5, 3]; //Modified the data range to have 5 columns
serie2.SerieType = OfficeChartType.Column_Stacked;
chart.PlotArea.Layout.ManualLayout.Height = 0.9;
chart.PlotArea.Layout.ManualLayout.Width = 1;
chart.PlotArea.Layout.ManualLayout.Left = 0;
chart.PlotArea.Layout.ManualLayout.Top = 0;
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[1, 1, 5, 1];
chart.Legend.IncludeInLayout = true;
chart.HasLegend = false;
Output document for the above modified code snippet:
http://www.syncfusion.com/downloads/support/directtrac/general/pp/Output-549061229.pptx
Please let us know if you need further assistance on this,
Note : I work for Syncfusion Software Private Limited