/// <summary> /// NOTE: this was copied directly from the Quant 1 model /// Run the quant model with different values for the Oi and Dj zones. /// Uses a guid to store the user defined model run data /// PRE: needs TObs, dis and beta /// TODO: need to instrument this /// TODO: writes out one file, which is the sum of the three predicted matrices produced /// </summary> /// <param name="OiDjHash">Hashmap of zonei index and Oi, Dj values for that area. A value of -1 for Oi or Dj means no change.</param> /// <param name="hasConstraints">Run with random values added to the Dj values.</param> /// <param name="OutFilename">Filename of where to store the resulting matrix, probably includes the GUID of the user directory to store the results</param> public void RunWithChanges(Dictionary <int, float[]> OiDjHash, bool hasConstraints, string OutFilename) { Stopwatch timer = Stopwatch.StartNew(); int N = TObs[0].N; float[] DjObs = new float[N]; float[] OiObs = new float[N]; float Sum; //OiObs for (int i = 0; i < N; i++) { Sum = 0; for (int j = 0; j < N; j++) { for (int k = 0; k < NumModes; k++) { Sum += TObs[k]._M[i, j]; } } OiObs[i] = Sum; } //DjObs for (int j = 0; j < N; j++) { Sum = 0; for (int i = 0; i < N; i++) { for (int k = 0; k < NumModes; k++) { Sum += TObs[k]._M[i, j]; } } DjObs[j] = Sum; } FMatrix[] TPredCons = new FMatrix[NumModes]; for (int k = 0; k < NumModes; k++) //mode loop { TPredCons[k] = new FMatrix(N, N); for (int i = 0; i < N; i++) { //denominator calculation which is sum of all modes double denom = 0; for (int kk = 0; kk < NumModes; kk++) //second mode loop { for (int j = 0; j < N; j++) { denom += DjObs[j] * Math.Exp(-Beta[kk] * dis[kk]._M[i, j]); } } //numerator calculation for this mode (k) for (int j = 0; j < N; j++) { TPredCons[k]._M[i, j] = (float)(B[j] * OiObs[i] * DjObs[j] * Math.Exp(-Beta[k] * dis[k]._M[i, j]) / denom); } } } //now the DjCons - you could just set Zj here? float[] DjCons = new float[N]; for (int j = 0; j < N; j++) { Sum = 0; for (int i = 0; i < N; i++) { for (int k = 0; k < NumModes; k++) { Sum += TPredCons[k]._M[i, j]; } } DjCons[j] = Sum; } // // //constraints initialisation - this is the same as the calibration, except that the B[j] values are initially taken from the calibration, while Z[j] is initialised from Dj[j] as before. float[] Z = new float[N]; for (int j = 0; j < N; j++) { Z[j] = float.MaxValue; if (IsUsingConstraints) { if (Constraints[j] >= 1.0f) //constraints array taking place of Gj (Green Belt) in documentation { //Gj=1 means a high enough percentage of MSOA land is green belt, so can't be built on //set constraint to original Dj //Z[j] = DjObs[j]; Z[j] = DjCons[j]; } } } //end of constraints initialisation - have now set B[] and Z[] based on IsUsingConstraints, Constraints[] and DObs[] //apply changes here from the hashmap foreach (KeyValuePair <int, float[]> KVP in OiDjHash) { int i = KVP.Key; if (KVP.Value[0] >= 0) { OiObs[i] = KVP.Value[0]; } if (KVP.Value[1] >= 0) { DjObs[i] = KVP.Value[1]; } } bool ConstraintsMet = false; do { //residential constraints ConstraintsMet = true; //unless violated one or more times below int FailedConstraintsCount = 0; //run 3 model System.Diagnostics.Debug.WriteLine("Run 3 model"); for (int k = 0; k < NumModes; k++) //mode loop { TPred[k] = new FMatrix(N, N); Parallel.For(0, N, i => //for (int i = 0; i < N; i++) { //denominator calculation which is sum of all modes double denom = 0; for (int kk = 0; kk < NumModes; kk++) //second mode loop { for (int j = 0; j < N; j++) { denom += B[j] * DjObs[j] * Math.Exp(-Beta[kk] * dis[kk]._M[i, j]); } } //numerator calculation for this mode (k) for (int j = 0; j < N; j++) { TPred[k]._M[i, j] = (float)(B[j] * OiObs[i] * DjObs[j] * Math.Exp(-Beta[k] * dis[k]._M[i, j]) / denom); } } ); } //constraints check if (IsUsingConstraints) { System.Diagnostics.Debug.WriteLine("Constraints test"); for (int j = 0; j < N; j++) { float Dj = 0; for (int i = 0; i < N; i++) { Dj += TPred[0]._M[i, j] + TPred[1]._M[i, j] + TPred[2]._M[i, j]; } if (Constraints[j] >= 1.0f) //Constraints is taking the place of Gj in the documentation { //System.Diagnostics.Debug.WriteLine("Test: " + Dj + ", " + Z[j] + "," + B[j]); if ((Dj - Z[j]) >= 0.5) //was >1.0 threshold { B[j] = B[j] * Z[j] / Dj; ConstraintsMet = false; ++FailedConstraintsCount; // System.Diagnostics.Debug.WriteLine("Constraints violated on " + FailedConstraintsCount + " MSOA zones"); // System.Diagnostics.Debug.WriteLine("Dj=" + Dj + " Zj=" + Z[j] + " Bj=" + B[j]); } } } } } while (!ConstraintsMet); //add all three TPred together FMatrix TPredAll = new FMatrix(N, N); //Parallel.For(0, N, i => for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Sum = 0; for (int k = 0; k < NumModes; k++) { Sum += TPred[k]._M[i, j]; } TPredAll._M[i, j] = Sum; } } //); //and store the result somewhere TPredAll.DirtySerialise(OutFilename); System.Diagnostics.Debug.WriteLine("QUANT3Model::RunWithChanges: " + timer.ElapsedMilliseconds + " ms"); }
private void button1_Click(object sender, EventArgs e) { using (var fbd = new FolderBrowserDialog()) { fbd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); //fbd.SelectedPath = @"D:\desktopQuant\data\"; fbd.SelectedPath = @"D:\2020\grantBikes\data\tranus\"; DialogResult result = fbd.ShowDialog(); if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) { string[] files = Directory.GetFiles(fbd.SelectedPath); /*System.Windows.Forms.MessageBox.Show("Directory: " + fbd.SelectedPath.ToString(), "Message"); * System.Windows.Forms.MessageBox.Show("Files found: " + files.Length.ToString(), "Message"); * for (int i=0;i< files.Length;i++) * System.Windows.Forms.MessageBox.Show("Files found: " + files.ElementAt(i), "Message");*/ // Display the ProgressBar control. pBar1.Visible = true; pBar1.Minimum = 1; pBar1.Maximum = 100; pBar1.Value = 1; pBar1.Step = 10; FMatrix[] temp = new FMatrix[3]; //Data input to model. FMatrix[] mtotal = null; FMatrix[] mtemp = null; mtotal = new FMatrix[3]; mtemp = new FMatrix[3]; string[] OutTPred = { "TPred1.csv", "TPred2.csv", "TPred3.csv" }; string OutTPredCombined = "TPred.csv"; int N = 194; if (modelType == 3) { string dijprivate = "privateTimeCode.csv"; string dijprivateS = "dis_roads_min.bin"; string dijpublic = "publicTimeCode.csv"; string dijpublicS = "dis_bus_min.bin"; string tprivate = "viajesODprivadosM.csv"; string tpublicS = "TObs_1.bin"; string tpublic = "viajesODpublicosM.csv"; string tprivateS = "TObs_2.bin"; FMatrix dijPrivate = FMatrix.GenerateTripsLondon(fbd.SelectedPath + "\\" + dijprivate, N); dijPrivate.DirtySerialise(fbd.SelectedPath + "\\" + dijprivateS); FMatrix dijPublic = FMatrix.GenerateTripsLondon(fbd.SelectedPath + "\\" + dijpublic, N); dijPublic.DirtySerialise(fbd.SelectedPath + "\\" + dijpublicS); FMatrix tprivateM = FMatrix.GenerateTripsLondon(fbd.SelectedPath + "\\" + tprivate, N); tprivateM.DirtySerialise(fbd.SelectedPath + "\\" + tpublicS); FMatrix tpublicM = FMatrix.GenerateTripsLondon(fbd.SelectedPath + "\\" + tpublic, N); tpublicM.DirtySerialise(fbd.SelectedPath + "\\" + tprivateS); } int NumModes = 1; switch (this.modelType) { case 3: NumModes = 2; break; default: NumModes = 3; break; } QUANT3ModelProperties q = new QUANT3ModelProperties(); FMatrix[] TObs = new FMatrix[NumModes]; //Data input to model. for (int k = 0; k < NumModes; k++) { TObs[k] = FMatrix.DirtyDeserialise(fbd.SelectedPath + "\\" + q.InTObs[k]); } for (int k = 0; k < NumModes; k++) { mtotal[k] = new FMatrix(N, N); } for (int i = 0; i < nexperiments; i++) { QUANT3Model quant = new QUANT3Model(modelType); temp = quant.LoadAndRun(q, fbd, pBar1, i); //quant.LoadAndRun(q, fbd, pBar1); //mtemp = temp; for (int j = 0; j < NumModes; j++) { //mtemp[j] = temp[j]; //mtemp[j].DirtySerialise(fbd.SelectedPath + "\\" + OutTPred[j] + "_1"); temp[j].WriteCSVMatrix(fbd.SelectedPath + "\\" + OutTPred[j] + "_" + i.ToString() + ".csv"); } FMatrix TPredCombinedtmp = new FMatrix(N, N); float Sum = 0; for (int w = 0; w < TPredCombinedtmp.M; w++) { for (int j = 0; j < TPredCombinedtmp.N; j++) { for (int k = 0; k < NumModes; k++) { Sum += temp[k]._M[w, j]; } TPredCombinedtmp._M[w, j] = Sum; Sum = 0.0f; } } TPredCombinedtmp.WriteCSVMatrix(fbd.SelectedPath + "\\TPred_" + i.ToString() + ".csv"); for (int j = 0; j < NumModes; j++) { mtotal[j] = mtotal[j].SumMatrix(temp[j], N); } System.Diagnostics.Debug.WriteLine("run = " + i); } for (int j = 0; j < NumModes; j++) { mtotal[j] = mtotal[j].Scale(1.0f / nexperiments); } for (int k = 0; k < NumModes; k++) { //mtotal[k].DirtySerialise(fbd.SelectedPath + "\\" + OutTPred[k]); mtotal[k].WriteCSVMatrix(fbd.SelectedPath + "\\" + OutTPred[k]); } FMatrix TPredCombined = new FMatrix(N, N); for (int i = 0; i < TPredCombined.M; i++) { for (int j = 0; j < TPredCombined.N; j++) { float Sum = 0; for (int k = 0; k < NumModes; k++) { Sum += mtotal[k]._M[i, j]; } TPredCombined._M[i, j] = Sum; } } //TPredCombined.DirtySerialise(fbd.SelectedPath + "\\" + OutTPredCombined); TPredCombined.WriteCSVMatrix(fbd.SelectedPath + "\\" + OutTPredCombined); } button2.Enabled = true; } /*int size = -1; * DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. * if (result == DialogResult.OK) // Test result. * { * string file = openFileDialog1.FileName; * try * { * string text = File.ReadAllText(file); * size = text.Length; * } * catch (IOException) * { * } * } * Console.WriteLine(size); // <-- Shows file size in debugging mode. * Console.WriteLine(result); // <-- For debugging use.*/ }
/// <summary> /// Initialise the model from the Quant2ModelProperties data and run it. /// </summary> /// <param name="ConnectionId"></param> /// <param name="q3mp"></param> public void LoadAndRun(QUANT3ModelProperties q3mp, FolderBrowserDialog f, ProgressBar pb) { // HubConnectionId = ConnectionId; //deserialise the distance and TObs matrices // InstrumentStatusText = "Loading matrices"; /*string[] files = Directory.GetFiles(f.SelectedPath); * System.Windows.Forms.MessageBox.Show("Files found: " + files.Length.ToString(), "Message");*/ /*for (int i=0;i< files.Length;i++) * System.Windows.Forms.MessageBox.Show("Files found: " + files.ElementAt(i), "Message");*/ for (int k = 0; k < NumModes; k++) { dis[k] = FMatrix.DirtyDeserialise(f.SelectedPath + "\\" + q3mp.Indis[k]); TObs[k] = FMatrix.DirtyDeserialise(f.SelectedPath + "\\" + q3mp.InTObs[k]); } ////IsUsingConstraints = q3mp.IsUsingConstraints; if (IsUsingConstraints) { //set up the constraints input data from the green belt and land use data table Constraints = new float[TObs[0].N]; //load constraints file here - convert data table to array based on zonei code DataTable dt = (DataTable)Serialiser.Get(q3mp.InConstraints); int ConstraintCount = 0; foreach (DataRow row in dt.Rows) //this does assume that there is a zonei value for every slot in Constraints[] { int ZoneI = (int)row["zonei"]; float Gj = (float)row["Gj"]; Constraints[ZoneI] = Gj; if (Gj >= 1.0) { ++ConstraintCount; } } System.Diagnostics.Debug.WriteLine("ConstraintCount=" + ConstraintCount); } // InstrumentStatusText = "Starting model run"; pb.PerformStep(); Run(pb); pb.PerformStep(); //write out 3 modes of predicted matrix for (int k = 0; k < NumModes; k++) { TPred[k].DirtySerialise(f.SelectedPath + "\\" + q3mp.OutTPred[k]); } //and a combined mode so the visualisation can see the data //TODO: this block needs to change once the GUI is able to visualise the different modes separately FMatrix TPredCombined = new FMatrix(TPred[0].M, TPred[0].N); for (int i = 0; i < TPredCombined.M; i++) { for (int j = 0; j < TPredCombined.N; j++) { float Sum = 0; for (int k = 0; k < NumModes; k++) { Sum += TPred[k]._M[i, j]; } TPredCombined._M[i, j] = Sum; } } TPredCombined.DirtySerialise(f.SelectedPath + "\\" + q3mp.OutTPredCombined); //end combination block //constraints B weights if we're doing that if (IsUsingConstraints) { Serialiser.Put(q3mp.OutConstraintsB, B); } System.Diagnostics.Debug.WriteLine("DONE"); /*InstrumentStatusText = "Computing model statistics"; * StatisticsDataQ3 sd = new StatisticsDataQ3(); * DataTable PopulationTable = (DataTable)Serialiser.Get(q3mp.InPopulationFilename); * sd.ComputeFromModel(this, ref PopulationTable); * sd.SerialiseToXML(q3mp.InOutStatisticsFilename); * InstrumentStatusText = "Finished"; * InstrumentFinished();*/ }