Created
December 28, 2017 08:52
-
-
Save galek/0e3bf0e1b2ddeaaecf8b5287ff974ff0 to your computer and use it in GitHub Desktop.
LR2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace LR2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
DateTime t0 = DateTime.Now; | |
int Count = 1000000; | |
double[,] gm = new double[4, 4] | |
{ | |
{0.25,-1,-1,-1}, | |
{1.25,0.25,-1,-1}, | |
{1,1.25,0.25,-1}, | |
{1,1,1.25,0.25} | |
}; | |
Console.WriteLine(); | |
for (var i = 0; i < gm.GetLength(0); i++) | |
{ | |
for (var j = 0; j < gm.GetLength(1); j++) | |
{ | |
Console.Write(string.Format("{0,5}", gm[i, j])); | |
} | |
Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
List<vector> A = new List<vector>(), B = new List<vector>(); | |
for (var i = 0; i < gm.GetLength(0); i++) | |
{ | |
vector v = new vector(gm.GetLength(1)); | |
for (var j = 0; j < v.len; j++) | |
{ | |
v[j] = gm[i, j]; | |
} | |
A.Add(v); | |
} | |
for (var i = 0; i < gm.GetLength(1); i++) | |
{ | |
vector v = new vector(gm.GetLength(0)); | |
for (var j = 0; j < v.len; j++) | |
{ | |
v[j] = gm[j, i]; | |
} | |
B.Add(v); | |
} | |
BRrow r = new BRrow(A, B); | |
Console.WriteLine(r.ToString()); | |
for (var i = 0; i < Count - 1; i++) | |
{ | |
r.Step(); | |
if (i > Count - 100) | |
Console.WriteLine(r.ToString()); | |
} | |
Console.WriteLine("Counts for A:"); | |
for (var i = 0; i < r.appsA.len; i++) | |
{ | |
Console.Write(string.Format("{0,5}:{1,12}:{2,12:f5}\r\n", "A" + (i + 1).ToString(), r.appsA[i], r.appsA[i] / r.appsA.sum)); | |
} | |
Console.WriteLine("Counts for B:"); | |
for (var i = 0; i < r.appsB.len; i++) | |
{ | |
Console.Write(string.Format("{0,5}:{1,12}:{2,12:f5}\r\n", "B" + (i + 1).ToString(), r.appsB[i], r.appsB[i] / r.appsB.sum)); | |
} | |
Console.WriteLine((DateTime.Now - t0).TotalSeconds); | |
Console.ReadKey(); | |
} | |
class Vec | |
{ | |
private double[] vals; | |
/** | |
*/ | |
public Vec(int l) | |
{ | |
vals = new double[l]; | |
} | |
/** | |
*/ | |
public Vec Copy() | |
{ | |
Vec v = new Vec(vals.Length); | |
for (var i = 0; i < len; i++) | |
{ | |
v[i] = vals[i]; | |
} | |
return v; | |
} | |
/** | |
*/ | |
public int len | |
{ | |
get | |
{ | |
return vals.Length; | |
} | |
} | |
/** | |
*/ | |
public double this[int i] | |
{ | |
get { return vals[i]; } | |
set { vals[i] = value; } | |
} | |
/** | |
*/ | |
public void Add(Vec b) | |
{ | |
for (var i = 0; i < vals.Length; i++) | |
{ | |
vals[i] += b.vals[i]; | |
} | |
} | |
/** | |
*/ | |
public double min { get { return vals.Min(); } } | |
public double max { get { return vals.Max(); } } | |
public double sum { get { return vals.Sum(); } } | |
/** | |
*/ | |
public int Indmin | |
{ | |
get | |
{ | |
return Array.IndexOf(vals, min); | |
} | |
} | |
/** | |
*/ | |
public int Indmax | |
{ | |
get | |
{ | |
return Array.IndexOf(vals, max); | |
} | |
} | |
} | |
class BRrow | |
{ | |
/** | |
*/ | |
public BRrow(List<Vec> pA = null, List<Vec> pB = null) | |
{ | |
BRrow.lA = pA; | |
BRrow.lB = pB; | |
k = 1; | |
i = lA.IndexOf(lA.First(e => e.max == lA.Max(r => r.max))); | |
B = lA[i].Copy(); | |
j = this.B.Indmin; | |
A = lB[j].Copy(); | |
appsA = new Vec(lA.Count); | |
appsB = new Vec(lB.Count); | |
} | |
/** | |
*/ | |
public static List<Vec> lA; | |
public static List<Vec> lB; | |
public int k; | |
public int i; | |
public Vec B; | |
public int j; | |
public Vec A; | |
public Vec appsA, appsB; | |
/** | |
*/ | |
public double Vu { get { return A.max / k; } } | |
/** | |
*/ | |
public double Vd { get { return B.min / k; } } | |
/** | |
*/ | |
public double V { get { return (Vu + Vd) / 2; } } | |
/** | |
*/ | |
public void Step() | |
{ | |
k++; | |
i = A.Indmax; B.Add(lA[i]); appsA[i] += 1; | |
j = B.Indmin; A.Add(lB[j]); appsB[j] += 1; | |
} | |
/** | |
*/ | |
public override string ToString() | |
{ | |
string t = ""; | |
if (k == 1) | |
{ | |
t += string.Format("{0,8}{1,3}", "k", "i"); | |
for (int i = 0; i < B.len; i++) t += string.Format("{0,9}", "B" + (i + 1).ToString()); | |
t += string.Format("{0,3}", "j"); | |
for (int i = 0; i < A.len; i++) t += string.Format("{0,9}", "A" + (i + 1).ToString()); | |
t += string.Format("{0,9}{1,9}{2,9}\r\n", "Vu", "Vd", "V"); | |
} | |
t += string.Format("{0,8}{1,3}", k, i + 1); | |
for (var i = 0; i < B.len; i++) | |
{ | |
t += string.Format("{0,9}", B[i]); | |
} | |
t += string.Format("{0,3}", j + 1); | |
for (var i = 0; i < A.len; i++) | |
{ | |
t += string.Format("{0,9}", A[i]); | |
} | |
t += string.Format("{0,9:f4}{1,9:f4}{2,9:f4}", Vu, Vd, V); | |
return t; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment