Google minimum cost flows to solve transportation flow - c#

Lets say we have the following data to solve transportation problem:
A1 A2 A3 Supply
T1 0 600 100 700
T2 500 0 300 800
Demand 500 600 400
I want to solve that transportation problem using Google Optimization Tools Minimum Cost Flows. I'm trying to solve that with the following code:
private static void SolveMinCostFlow()
{
// Define four parallel arrays: sources, destinations, capacities, and unit costs
// between each pair. For instance, the arc from node 0 to node 1 has a
// capacity of 15.
// Problem taken From Taha's 'Introduction to Operations Research',
// example 6.4-2.
int numNodes = 5;
int numArcs = 6;
int[] startNodes = { 0, 0, 0, 1, 1, 1 };
int[] endNodes = { 2, 3, 4, 2, 3, 4};
int[] capacities = { 500, 600, 400, 500, 600, 400 };
int[] unitCosts = { 0, 600, 100, 500, 0, 300 };
// Define an array of supplies at each node.
int[] supplies = { 700, 700, 800, 800, 800 };
// Instantiate a SimpleMinCostFlow solver.
MinCostFlow minCostFlow = new MinCostFlow();
// Add each arc.
for (int i = 0; i < numArcs; ++i)
{
int arc = minCostFlow.AddArcWithCapacityAndUnitCost(startNodes[i], endNodes[i],
capacities[i], unitCosts[i]);
if (arc != i) throw new Exception("Internal error");
}
// Add node supplies.
for (int i = 0; i < numNodes; ++i)
{
minCostFlow.SetNodeSupply(i, supplies[i]);
}
//Console.WriteLine("Solving min cost flow with " + numNodes + " nodes, and " +
// numArcs + " arcs, source=" + source + ", sink=" + sink);
// Find the min cost flow.
int solveStatus = minCostFlow.Solve();
if (solveStatus == MinCostFlow.OPTIMAL)
{
long optimalCost = minCostFlow.OptimalCost();
Console.WriteLine("Minimum cost: " + optimalCost);
Console.WriteLine("");
Console.WriteLine(" Edge Flow / Capacity Cost");
for (int i = 0; i < numArcs; ++i)
{
long cost = minCostFlow.Flow(i) * minCostFlow.UnitCost(i);
Console.WriteLine(minCostFlow.Tail(i) + " -> " +
minCostFlow.Head(i) + " " +
string.Format("{0,3}", minCostFlow.Flow(i)) + " / " +
string.Format("{0,3}", minCostFlow.Capacity(i)) + " " +
string.Format("{0,3}", cost));
}
}
else
{
Console.WriteLine("Solving the min cost flow problem failed. Solver status: " +
solveStatus);
}
}
static void Main(string[] args)
{
SolveMinCostFlow();
Console.Read();
}
But I get error: Solving the min cost flow problem failed. Solver status: 4
What am I doing wrong here? I suppose there should be something with defining parameters at the start of SolveMinCostFlow but can't figure it out.

To summarize: a balanced n x m transportation problem can be converted to a max flow problem using or-tools as follows:
n + m nodes with supply and demand (demand modeled as negative supply)
n * m arcs with infinite capacity and costs c(i,j)
Some python code to verify this:
from ortools.graph import pywrapgraph
# A1 A2 A3 Supply
# T1 0 600 100 700
# T2 500 0 300 800
# Demand 500 600 400
numNodes = 5
numArcs = 6;
startNodes = [ 0, 0, 0, 1, 1, 1 ]
endNodes = [ 2, 3, 4, 2, 3, 4 ]
capacities = [9999] * numArcs
unitCosts = [0, 600, 100, 500, 0, 300 ]
supplies = [700,800,-500,-600,-400]
# Instantiate a SimpleMinCostFlow solver.
min_cost_flow = pywrapgraph.SimpleMinCostFlow()
# Add each arc.
for i in range(0, len(startNodes)):
min_cost_flow.AddArcWithCapacityAndUnitCost(startNodes[i], endNodes[i],
capacities[i], unitCosts[i])
# Add node supplies.
for i in range(0, len(supplies)):
min_cost_flow.SetNodeSupply(i, supplies[i])
# Find the minimum cost flow
if min_cost_flow.Solve() == min_cost_flow.OPTIMAL:
print('Minimum cost:', min_cost_flow.OptimalCost())
print('')
print(' Arc Flow / Capacity Cost')
for i in range(min_cost_flow.NumArcs()):
cost = min_cost_flow.Flow(i) * min_cost_flow.UnitCost(i)
print('%1s -> %1s %3s / %3s %3s' % (
min_cost_flow.Tail(i),
min_cost_flow.Head(i),
min_cost_flow.Flow(i),
min_cost_flow.Capacity(i),
cost))
else:
print('There was an issue with the min cost flow input.')
This prints:
Minimum cost: 80000
Arc Flow / Capacity Cost
0 -> 2 500 / 9999 0
0 -> 3 0 / 9999 0
0 -> 4 200 / 9999 20000
1 -> 2 0 / 9999 0
1 -> 3 600 / 9999 0
1 -> 4 200 / 9999 60000
More interesting is a non-balanced transportation problem with sum supply > sum demand. Or-tools min-cost-flow algorithm can handle that also (via min_cost_flow.SolveMaxFlowWithMinCost()).

sorry but I believe that it would be better to add a sink and modify the capacities of the main network. This modification would allow you to optimize your current data and meet the specific requirements.
# A1 A2 A3 Supply
# T1 0 600 100 700
# T2 500 0 300 800
# Demand 500 600 400
numNodes = 6
numArcs = 9;
startNodes = [ 0, 0, 0, 1, 1, 1] + [2,3,4]
endNodes = [ 2, 3, 4, 2, 3, 4 ]+ [5,5,5 ]
capacities = [0,1000,1000,1000,0,1000]+[500,600,400]
unitCosts = [0, 600, 100, 500, 0, 300 ]+[0,0,0]
supplies = [700,800,0,0,0,-1500]
So by adding the extra node (sink) you are making sure your demands are met, and by changing the capacities you make sure you do not send units to the node with a cost unit of 0 ( I assume the cero means nothing goes to that node from that specific source). Hope it helps !
Output:
Costo Minimo: 710000
Ruta Flujo / Capacidad Costo
0 -> 2 0 / 0 0
0 -> 3 600 / 1000 360000
0 -> 4 100 / 1000 10000
1 -> 2 500 / 1000 250000
1 -> 3 0 / 0 0
1 -> 4 300 / 1000 90000
2 -> 5 500 / 500 0
3 -> 5 600 / 600 0
4 -> 5 400 / 400 0
Where costo minimo = minimum cost

Related

C# store and display calculated iteration in one line using messagebox.show

I am working on this program. My purpose is to Store the result of calculated input data in one int[] variable and display it in one line using messagebox.show.
int[] data = new int[] { 65, 66, 67, 32, 100, 90 }; // I declare int[] data it contain my data that I want to work with the length change.
int[] array = new int[6]; // i declare a table length of 6
foreach (var b in data) // for every element in my data I want to do this operations and build my array.
{
array[0] = b / 200;
array[1] = b / 79;
array[2] = b / 27;
array[3] = b / 19;
array[4] = b / 21;
array[5] = b / 3;
Console.WriteLine("{0}", string.Join(" ", array)); // this line is for console application
// output of this line is :
/*
0 0 2 3 3 21
0 0 2 3 3 22
0 0 2 3 3 22
0 0 1 1 1 10
0 1 3 5 4 33
0 1 3 4 4 30 */
MessageBox.Show(" "+ string.Join(" ", array)); // this line is for windowsform application
My purpose is in windowsform application to display my variable using messagebox.show. I aim the calculated to store them in one variable and to display them like this one :
0 0 2 3 3 21 0 0 2 3 3 22 0 0 2 3 3 22 0 0 1 1 1 10 0 1 3 5 4 33 0 1 3 4 4 30
I really appreciate any help.
kind regards
You can simply join the string in loop and then display them outside of the loop in your message box. Use StringBuilder class for appending results.
StringBuilder sb = new StringBuilder();
for(...)
{
...
...
sb.AppendFormat("{0} ", string.Join(" ", array).Trim())
}
MessageBox.Show(sb.ToString());

How the row wise calculation made in the C# wpf?

In the following image the opening balance of the each row is:
the sum of the above column if it is credit
the minus of the above column if it is debit
How can it be done in c#
For example:
credit debit OpenBal
1. 100 - 0 - 100
2. 90 - 0 - 190
3. 100 - 0 - 290
4. 0 - 50 - 240
5. 0 - 100 - 140
6. 150 - 0 - 290
There are many possibilities, depending on when/how/where the values should be calculated.
The following example demonstrates how you could realize the calculation in a very simple case when the OpenBalance is calculated directly after the data is loaded und you don't have to refresh the OpenBalance:
"Load" data:
var tbl = new DataTable();
tbl.Columns.Add( "credit", typeof( decimal ) );
tbl.Columns.Add( "debit", typeof( decimal ) );
tbl.Columns.Add( "OpenBal", typeof( decimal ) );
tbl.Rows.Add( 100, 0 );
tbl.Rows.Add( 90, 0 );
tbl.Rows.Add( 100, 0 );
tbl.Rows.Add( 0, 50 );
tbl.Rows.Add( 0, 100 );
tbl.Rows.Add( 150, 0 );
Calculate Open Balance:
for ( int i = 0; i < tbl.Rows.Count; i++ )
{
var row = tbl.Rows[i];
if ( i == 0 )
row["OpenBal"] = (decimal)row["credit"] - (decimal)row["debit"];
else
{
var previousRow = tbl.Rows[i-1];
row["OpenBal"] = (decimal)previousRow["OpenBal"] + ( decimal)row["credit"] - (decimal)row["debit"];
}
}

Sequence Alignment Algorithm with a group of characters instead of one character

Summary:
I'm beginning with some details about alignment algorithms, and at the end, I ask my question. If you know about alignment algorithm pass the beginning.
Consider we have two strings like:
ACCGAATCGA
ACCGGTATTAAC
There is some algorithms like: Smith-Waterman Or Needleman–Wunsch, that align this two sequence and create a matrix. take a look at the result in the following section:
Smith-Waterman Matrix
§ § A C C G A A T C G A
§ 0 0 0 0 0 0 0 0 0 0 0
A 0 4 0 0 0 4 4 0 0 0 4
C 0 0 13 9 4 0 4 3 9 4 0
C 0 0 9 22 17 12 7 3 12 7 4
G 0 0 4 17 28 23 18 13 8 18 13
G 0 0 0 12 23 28 23 18 13 14 18
T 0 0 0 7 18 23 28 28 23 18 14
A 0 4 0 2 13 22 27 28 28 23 22
T 0 0 3 0 8 17 22 32 27 26 23
T 0 0 0 2 3 12 17 27 31 26 26
A 0 4 0 0 2 7 16 22 27 31 30
A 0 4 4 0 0 6 11 17 22 27 35
C 0 0 13 13 8 3 6 12 26 22 30
Optimal Alignments
A C C G A - A T C G A
A C C G G A A T T A A
Question:
My question is simple, but maybe the answer is not easy as it looks. I want to use a group of character as a single one like: [A0][C0][A1][B1]. But in these algorithms, we have to use individual characters. How can we achieve that?
P.S. Consider we have this sequence: #read #write #add #write. Then I convert this to something like that: #read to A .... #write to B.... #add to C. Then my sequence become to: ABCB. But I have a lot of different words that start with #. And the ASCII table is not enough to convert all of them. Then I need more characters. the only way is to use something like [A0] ... [Z9] for each word. OR to use numbers.
P.S: some sample code for Smith-Waterman is exist in this link
P.S: there is another post that want something like that, but what I want is different. In this question, we have a group of character that begins with a [ and ends with ]. And no need to use semantic like ee is equal to i.
I adapted this Python implementation (GPL version 3 licensed) of both the Smith-Waterman and the Needleman-Wunsch algorithms to support sequences with multiple character groups:
#This software is a free software. Thus, it is licensed under GNU General Public License.
#Python implementation to Smith-Waterman Algorithm for Homework 1 of Bioinformatics class.
#Forrest Bao, Sept. 26 <http://fsbao.net> <forrest.bao aT gmail.com>
# zeros() was origianlly from NumPy.
# This version is implemented by alevchuk 2011-04-10
def zeros(shape):
retval = []
for x in range(shape[0]):
retval.append([])
for y in range(shape[1]):
retval[-1].append(0)
return retval
match_award = 10
mismatch_penalty = -5
gap_penalty = -5 # both for opening and extanding
gap = '----' # should be as long as your group of characters
space = ' ' # should be as long as your group of characters
def match_score(alpha, beta):
if alpha == beta:
return match_award
elif alpha == gap or beta == gap:
return gap_penalty
else:
return mismatch_penalty
def finalize(align1, align2):
align1 = align1[::-1] #reverse sequence 1
align2 = align2[::-1] #reverse sequence 2
i,j = 0,0
#calcuate identity, score and aligned sequeces
symbol = []
found = 0
score = 0
identity = 0
for i in range(0,len(align1)):
# if two AAs are the same, then output the letter
if align1[i] == align2[i]:
symbol.append(align1[i])
identity = identity + 1
score += match_score(align1[i], align2[i])
# if they are not identical and none of them is gap
elif align1[i] != align2[i] and align1[i] != gap and align2[i] != gap:
score += match_score(align1[i], align2[i])
symbol.append(space)
found = 0
#if one of them is a gap, output a space
elif align1[i] == gap or align2[i] == gap:
symbol.append(space)
score += gap_penalty
identity = float(identity) / len(align1) * 100
print 'Identity =', "%3.3f" % identity, 'percent'
print 'Score =', score
print ''.join(align1)
# print ''.join(symbol)
print ''.join(align2)
def needle(seq1, seq2):
m, n = len(seq1), len(seq2) # length of two sequences
# Generate DP table and traceback path pointer matrix
score = zeros((m+1, n+1)) # the DP table
# Calculate DP table
for i in range(0, m + 1):
score[i][0] = gap_penalty * i
for j in range(0, n + 1):
score[0][j] = gap_penalty * j
for i in range(1, m + 1):
for j in range(1, n + 1):
match = score[i - 1][j - 1] + match_score(seq1[i-1], seq2[j-1])
delete = score[i - 1][j] + gap_penalty
insert = score[i][j - 1] + gap_penalty
score[i][j] = max(match, delete, insert)
# Traceback and compute the alignment
align1, align2 = [], []
i,j = m,n # start from the bottom right cell
while i > 0 and j > 0: # end toching the top or the left edge
score_current = score[i][j]
score_diagonal = score[i-1][j-1]
score_up = score[i][j-1]
score_left = score[i-1][j]
if score_current == score_diagonal + match_score(seq1[i-1], seq2[j-1]):
align1.append(seq1[i-1])
align2.append(seq2[j-1])
i -= 1
j -= 1
elif score_current == score_left + gap_penalty:
align1.append(seq1[i-1])
align2.append(gap)
i -= 1
elif score_current == score_up + gap_penalty:
align1.append(gap)
align2.append(seq2[j-1])
j -= 1
# Finish tracing up to the top left cell
while i > 0:
align1.append(seq1[i-1])
align2.append(gap)
i -= 1
while j > 0:
align1.append(gap)
align2.append(seq2[j-1])
j -= 1
finalize(align1, align2)
def water(seq1, seq2):
m, n = len(seq1), len(seq2) # length of two sequences
# Generate DP table and traceback path pointer matrix
score = zeros((m+1, n+1)) # the DP table
pointer = zeros((m+1, n+1)) # to store the traceback path
max_score = 0 # initial maximum score in DP table
# Calculate DP table and mark pointers
for i in range(1, m + 1):
for j in range(1, n + 1):
score_diagonal = score[i-1][j-1] + match_score(seq1[i-1], seq2[j-1])
score_up = score[i][j-1] + gap_penalty
score_left = score[i-1][j] + gap_penalty
score[i][j] = max(0,score_left, score_up, score_diagonal)
if score[i][j] == 0:
pointer[i][j] = 0 # 0 means end of the path
if score[i][j] == score_left:
pointer[i][j] = 1 # 1 means trace up
if score[i][j] == score_up:
pointer[i][j] = 2 # 2 means trace left
if score[i][j] == score_diagonal:
pointer[i][j] = 3 # 3 means trace diagonal
if score[i][j] >= max_score:
max_i = i
max_j = j
max_score = score[i][j];
align1, align2 = [], [] # initial sequences
i,j = max_i,max_j # indices of path starting point
#traceback, follow pointers
while pointer[i][j] != 0:
if pointer[i][j] == 3:
align1.append(seq1[i-1])
align2.append(seq2[j-1])
i -= 1
j -= 1
elif pointer[i][j] == 2:
align1.append(gap)
align2.append(seq2[j-1])
j -= 1
elif pointer[i][j] == 1:
align1.append(seq1[i-1])
align2.append(gap)
i -= 1
finalize(align1, align2)
If we run this with the following input:
seq1 = ['[A0]', '[C0]', '[A1]', '[B1]']
seq2 = ['[A0]', '[A1]', '[B1]', '[C1]']
print "Needleman-Wunsch"
needle(seq1, seq2)
print
print "Smith-Waterman"
water(seq1, seq2)
We get this output:
Needleman-Wunsch
Identity = 60.000 percent
Score = 20
[A0][C0][A1][B1]----
[A0]----[A1][B1][C1]
Smith-Waterman
Identity = 75.000 percent
Score = 25
[A0][C0][A1][B1]
[A0]----[A1][B1]
For the specific changes I made, see: this GitHub repository.
Imagine we have a log file with alphabetic sequences. Like something you said, I converted sequences to A0A1... . For example, if there was a sequence like #read #write #add #write, it converted to A0A1A2A1. Every time, I read two character and compare them but keep score matrix like before. Here is my code in C# for smith-waterman string alignment.
Notice that Cell is a user defined class.
private void alignment()
{
string strSeq1;
string strSeq2;
string strTemp1;
string strTemp2;
scoreMatrix = new int[Log.Length, Log.Length];
// Lists That Holds Alignments
List<char> SeqAlign1 = new List<char>();
List<char> SeqAlign2 = new List<char>();
for (int i = 0; i<Log.Length; i++ )
{
for (int j=i+1 ; j<Log.Length; j++)
{
strSeq1 = "--" + logFile.Sequence(i);
strSeq2 = "--" + logFile.Sequence(j);
//prepare Matrix for Computing optimal alignment
Cell[,] Matrix = DynamicProgramming.Intialization_Step(strSeq1, strSeq2, intSim, intNonsim, intGap);
// Trace back matrix from end cell that contains max score
DynamicProgramming.Traceback_Step(Matrix, strSeq1, strSeq2, SeqAlign1, SeqAlign2);
this.scoreMatrix[i, j] = DynamicProgramming.intMaxScore;
strTemp1 = Reverse(string.Join("", SeqAlign1));
strTemp2 = Reverse(string.Join("", SeqAlign2));
}
}
}
class DynamicProgramming
{
public static Cell[,] Intialization_Step(string Seq1, string Seq2,int Sim,int NonSimilar,int Gap)
{
int M = Seq1.Length / 2 ;//Length+1//-AAA //Changed: /2
int N = Seq2.Length / 2 ;//Length+1//-AAA
Cell[,] Matrix = new Cell[N, M];
//Intialize the first Row With Gap Penality Equal To Zero
for (int i = 0; i < Matrix.GetLength(1); i++)
{
Matrix[0, i] = new Cell(0, i, 0);
}
//Intialize the first Column With Gap Penality Equal To Zero
for (int i = 0; i < Matrix.GetLength(0); i++)
{
Matrix[i, 0] = new Cell(i, 0, 0);
}
// Fill Matrix with each cell has a value result from method Get_Max
for (int j = 1; j < Matrix.GetLength(0); j++)
{
for (int i = 1; i < Matrix.GetLength(1); i++)
{
Matrix[j, i] = Get_Max(i, j, Seq1, Seq2, Matrix,Sim,NonSimilar,Gap);
}
}
return Matrix;
}
public static Cell Get_Max(int i, int j, string Seq1, string Seq2, Cell[,] Matrix,int Similar,int NonSimilar,int GapPenality)
{
Cell Temp = new Cell();
int intDiagonal_score;
int intUp_Score;
int intLeft_Score;
int Gap = GapPenality;
//string temp1, temp2;
//temp1 = Seq1[i*2].ToString() + Seq1[i*2 + 1]; temp2 = Seq2[j*2] + Seq2[j*2 + 1].ToString();
if ((Seq1[i * 2] + Seq1[i * 2 + 1]) == (Seq2[j * 2] + Seq2[j * 2 + 1])) //Changed: +
{
intDiagonal_score = Matrix[j - 1, i - 1].CellScore + Similar;
}
else
{
intDiagonal_score = Matrix[j - 1, i - 1].CellScore + NonSimilar;
}
//Calculate gap score
intUp_Score = Matrix[j - 1, i].CellScore + GapPenality;
intLeft_Score = Matrix[j, i - 1].CellScore + GapPenality;
if (intDiagonal_score<=0 && intUp_Score<=0 && intLeft_Score <= 0)
{
return Temp = new Cell(j, i, 0);
}
if (intDiagonal_score >= intUp_Score)
{
if (intDiagonal_score>= intLeft_Score)
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j - 1, i - 1], Cell.PrevcellType.Diagonal);
}
else
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j , i - 1], Cell.PrevcellType.Left);
}
}
else
{
if (intUp_Score >= intLeft_Score)
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j - 1, i], Cell.PrevcellType.Above);
}
else
{
Temp = new Cell(j, i, intDiagonal_score, Matrix[j , i - 1], Cell.PrevcellType.Left);
}
}
if (MaxScore.CellScore <= Temp.CellScore)
{
MaxScore = Temp;
}
return Temp;
}
public static void Traceback_Step(Cell[,] Matrix, string Sq1, string Sq2, List<char> Seq1, List<char> Seq2)
{
intMaxScore = MaxScore.CellScore;
while (MaxScore.CellPointer != null)
{
if (MaxScore.Type == Cell.PrevcellType.Diagonal)
{
Seq1.Add(Sq1[MaxScore.CellColumn * 2 + 1]); //Changed: All of the following lines with *2 and +1
Seq1.Add(Sq1[MaxScore.CellColumn * 2]);
Seq2.Add(Sq2[MaxScore.CellRow * 2 + 1]);
Seq2.Add(Sq2[MaxScore.CellRow * 2]);
}
if (MaxScore.Type == Cell.PrevcellType.Left)
{
Seq1.Add(Sq1[MaxScore.CellColumn * 2 + 1]);
Seq1.Add(Sq1[MaxScore.CellColumn * 2]);
Seq2.Add('-');
}
if (MaxScore.Type == Cell.PrevcellType.Above)
{
Seq1.Add('-');
Seq2.Add(Sq2[MaxScore.CellRow * 2 + 1]);
Seq2.Add(Sq2[MaxScore.CellRow * 2]);
}
MaxScore = MaxScore.CellPointer;
}
}
}

How to iterate through a grid algorithm

I am looking for an algorithm that can iterate through a grid and transform it into another grid with the indexes in a new order.
Basically, given a grid of size n*m:
1_1 1_2 1_3 ... 1_n
2_1 2_2 2_3 ... 2_n
.
.
.
m_1 m_2 m_3 ... m_m
How could I transform it to:
1_1 1_2 1_4 ...
1_3 1_5 ...
1_6 ...
...
.
.
.
Assume, you iterate through the first grid, going left to right in the top row, then
left to right in the second row, all the way to, left to right in the bottom row.
Basically I pushing the elements into an upper triangle.
Another problem is how do I figure out the length and width of the grid used to store the triangle just by knowing what n and m is?
Is there a formula for that?
For example, a grid of 5*6, gets changed to 8*7...
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
becomes:
1 2 4 7 11 16 22 29
3 5 8 12 17 23 30
6 9 13 18 24
10 14 19 25
15 20 26
21 27
28
The following seems to work for me:
public static T[,] ConvertToUpperTriangle<T>(T[,] arr)
{
// calculate the dimensions
int elements = arr.GetLength(0) * arr.GetLength(1);
double rows = 0.5 * (Math.Sqrt(8 * elements + 1) - 1);
int newHeight = (int)rows;
int newWidth = (int)Math.Ceiling(rows);
// create the new array
var arr2 = new T[newHeight, newWidth];
int j = 0;
int i = 0;
foreach (T element in arr)
{
arr2[j, i] = element;
i--;
j++;
if (i < 0)
{
i = j;
j = 0;
}
}
return arr2;
}
The 0.5 * (Math.Sqrt(8 * elements + 1) - 1) comes from running sum from 1 to n of n and then solve a = 0.5 * n * (n + 1) for n through Wolfram Alpha.
Edit:
You can get the indices i and j for a given index as follows:
int rows = (int)(0.5 * (Math.Sqrt(8 * index + 1) - 1));
int bottomLeft = (int)(0.5 * rows * (rows + 1));
int difference = index - bottomLeft;
int i;
int j;
if (bottomLeft == index)
{
i = 0;
j = rows - 1;
}
else
{
i = rows + 1 - difference;
j = difference - 1;
}
Let's define the "ordinal position" O(i,j) of each grid element (i,j) in a starting grid NxM, which is the function (i,j) -> i*N + j.
Now for the largest triangular number less than O(i,j), call it T == (k(k+1)/2 for some k, the new grid position for our (i,j) will be:
(i,j) -> ( O(i,j) - T, k + T - O(i,j) )
Now substitute for O(i,j) and T to get:
(i,j) -> ( i*N + j - k(k+1)/2, k + (k+1)(k+2)/2 - i*N + j)
= ( i*N + j - k(k+1)/2, (k+1)(k+2)/2 - i*N + j)
That's as far as I can get it just now.
Update:
Note again that k is the side-length for the triangualr number T == k(k+1)/2.

C#: A Bounding Max and Min on Array[]

Suppose I have a List. I'd like to be able to calculate semi-equi-distant max and min bounding points. I don't want to simply get the Max() and Min() its slightly more complicated.
To start, I'd like to specify a point in the list in which the list can be divided. To make it easy for now, suppose that point is 0. I'd then like to specify the number of divisions. Example:
List<int> Array = {-9,-8,-7,-2,-1,0,1,6,9,12};
int Divisions = 4;
int CutOff = 0;
So using these parameters I'd like to walk out to the extremes starting from 0 until there are 4 divisions. In this case the DivisionSize should be 6.
So the algorithm would start at 0 and walk to -6 for 1 Division then walk to -12 for the 2nd division. -12 would then become the bounding Min for the purposes of this algorithm.
The Max would then be calculated by starting at 0 and walking to 6, then 12. The bounding Max would then be 12. Its okay if the Calculate Max and Min are the actual Max and Min of the list, this is just an unlikely case.
I'm basically have some issues calculating the DivisionSize. I started with (Abs(Max)+Abs(Min))/Divisions but I can't seem to get the edge case where the Calculated size of the each division needs to be expanded to actually encompass the original Min and Max. Can somebody provided some guidance?
Edit: I don't necessarily want the BoundedMax and BoundedMin to be symmetrical about the cutoff. I want to add slack to either side of the cutoff until the BoundedMin and BoundedMax are >= and <= the range of the List.
Since your divisions are going to be "semi-equidistant" from the cutoff, your algorithm should only focus on half the divisions (one side from the cutoff). The next step would be to determine which of the "sides" of the cutoff is larger.
Next, we divide the larger side by half the division, and get the Ceiling of the value (round to next higher integer). This will give us the size of each division of the larger side which would encompass all the values on both sides of the cutoff.
The following algorithm would give you the DivisionSize of 6 when applied to the example you provided:
int NewMax = Abs(Max - CutOff);
int NewMin = Abs(Min - CutOff);
int DivisionSize = (int)Math.Ceiling(NewMax > NewMin ? NewMax/(Divisions/2) : NewMin/(Divisions/2));
L = abs(min(A)-cut)
R = abs(max(A)-cut)
size = max(L,R) # ate least two divisions
while divisions >= (1+(L-1)/size + 1+(R-1)/size)
size = size-1
size = size+1
Lets try it out:
L = 9
R = 12
size = 12
d = 1 + (9-1)/12 + 1 + (12-1)/12 = 1 + 1 = 2
size = 11
d = 1 + (9-1)/11 + 1 + (12-1)/11 = 1 + 2 = 3
size = 10
d = 1 + (9-1)/10 + 1 + (12-1)/10 = 1 + 2 = 3
size = 9
d = 1 + (9-1)/9 + 1 + (12-1) / 9 = 1 + 2 = 3
size = 8
d = 1 + (9-1)/8 + 1 + (12-1) / 8 = 2 + 2 = 4
size = 7
d = 1 + (9-1)/7 + 1 + (12-1) / 7 = 2 + 2 = 4
size = 6
d = 1 + (9-1)/6 + 1 + (12-1) / 6 = 2 + 2 = 4
size = 5
d = 1 + (9-1)/5 + 1 + (12-1) / 5 = 2 + 3 = 5
--> size = 6
Note that the integer divisions must be floored (not rounded).
For optimization, you can use a binary search between 1 and R for the size.
I think the key is to determine how many of your divisions you want either side of the CutOff point, by taking the ratio of each side's length to the total length.
In your example, the sides are 9 and 12, giving (approx) 1.7 and 2.2 divisions either side. The actual numbers must be integers, so try (1,3) and (2,2). 1 division on the left means the size must be 9, 2 divisions on either side allow you to use division size 6.
Wrote some C# to illustrate this. Not particularly elegant, but it seems to work.
public class RangeDivider
{
public int Min;
public int CutOff;
public int Max;
public int NumDivisions;
public RangeDivider(int min, int cutOff, int max, int numDivisions)
{
Min = min;
CutOff = cutOff;
Max = max;
NumDivisions = numDivisions;
System.Diagnostics.Debug.Assert(Min < CutOff && CutOff < Max && numDivisions >= 2);
}
public int LeftSize { get { return CutOff - Min; } }
public int RightSize { get { return Max - CutOff; } }
public int WholeSize { get { return Max - Min; } }
private static int divCeil(int dividend, int divisor) { return 1 + (dividend - 1)/divisor; }
private int ReturnSize(int leftDivisions)
{
int rightDivisions = NumDivisions - leftDivisions;
if (leftDivisions > 0 && rightDivisions > 0)
{
return Math.Max(divCeil(LeftSize, leftDivisions), divCeil(RightSize, rightDivisions));
}
else
{ //Must have at least 1 division each side of cutoff
return int.MaxValue;
}
}
public int GetSize()
{
var leftDivisions = NumDivisions * LeftSize / WholeSize;
var size = Math.Min(ReturnSize(leftDivisions), ReturnSize(leftDivisions + 1));
Console.WriteLine("Min {0}, CutOff {1}, Max {2}, NumDivisions {3} gives a Division Size of {4}", Min, CutOff, Max, NumDivisions, size);
return size;
}
public static int Get(int min, int cutOff, int max, int numDivisions)
{
return new RangeDivider(min, cutOff, max, numDivisions).GetSize();
}
public static void Test()
{
Get(-7,0,57,4);
Get(-9, 0, 12, 4);
Get(-1, 0, 7, 6);
}
}
Min -7, CutOff 0, Max 57, NumDivisions 4 gives a Division Size of 19
Min -9, CutOff 0, Max 12, NumDivisions 4 gives a Division Size of 6
Min -1, CutOff 0, Max 7, NumDivisions 6 gives a Division Size of 2

Categories