Sample source code for the agents in C# 1. Sample code of Experiment Design Agent Design Method Interface public interface IdesignMethods { experiment sample{get;set;} List<experiment> workingexps{get;set;} bool finish {get;set;} int iteration_count{get;set;} string name{ get; set; } void method(); } Simplex Search Method using using using using System; System.Collections.Generic; System.Text; utilities; namespace design_agent { class simplex_alg : IdesignMethods { //****************************** private double accurate_index = 0.0005; private double reflection_rate=1; private double expansion_rate=2; private double contraction_rate=-0.5; private double reduction_rate=0.5; //****************************** //******INTERFACE********** public List<experiment> workingexps { get; set; } public int iteration_count { get; set; } public experiment sample { get; set; } public bool finish { get; set; } public string name { get; set; } public double currentobj { get; set; } //************************* private private private private experiment[] simplex; bool firstrun = true; int inputnum; experiment gravity_exp; public simplex_alg() { finish=false; workingexps = new List<experiment>(); name="simplex"; } private void init_exps() { Random rand = new Random(); inputnum = 0; foreach (parameter p in sample.exp_settting.paras) { if (p.isInput) inputnum++; } simplex = new experiment[inputnum+1]; experiment e = new experiment(); for (int i = 0; i < inputnum+1; i++) { e = new experiment(); e =sample.clone(); e.ID = Guid.NewGuid(); e.name="simplex_"+iteration_count; foreach (parameter x in e.exp_settting.paras) { if (x.isInput) { int value=rand.Next(0,100); x.setvalue((double)value / 100 * (x.max - x.min) + x.min); } else x.value = -1; } e.done = false; workingexps.Add(e); simplex[i] = e; } } private void stepalg() { for (int i = 0; i < inputnum + 1;i++ ) { experiment temp = new experiment(); temp = ifready_exp(simplex[i]); if (temp == null) { return; } simplex[i] = temp; } if (this.ss() <= accurate_index) { finish = true; List<experiment> finallist=new List<experiment>(); for(int i=0;i<=inputnum;i++) { finallist.Add(simplex[i]); } util.exportExperiments(finallist, "final simplex", "simplex result"); return; } Console.WriteLine(iteration_count+" this.ss()); simplex=util.sort_exparray(simplex); gravity_exp = this.gravity(); experiment r = this.reflection(); experiment c = this.contraction(); experiment e = this.expansion(); experiment x1=simplex[0]; Current best: {0}; ss={1}", simplex[0].obj_value, experiment xn=simplex[inputnum-1]; experiment tempr = this.ifready_exp(r); if (tempr!=null) { r = tempr; if (r.obj_value >= x1.obj_value && r.obj_value <= xn.obj_value) { Console.WriteLine(" reflection"); simplex[inputnum] = r; iteration_count++; stepalg(); } else if (r.obj_value < x1.obj_value) { experiment tempe = this.ifready_exp(e); if (tempe!=null) { e = tempe; if (e.obj_value > r.obj_value) { Console.WriteLine(" espansion"); simplex[inputnum]=e; iteration_count++; stepalg(); } else { Console.WriteLine(" reflection"); simplex[inputnum]=r; iteration_count++; stepalg(); } } else { //Console.WriteLine(" espansion not finish."); return; } } else { experiment tempc = this.ifready_exp(c); if (tempc!=null) { c = tempc; if (c.obj_value < simplex[inputnum].obj_value) { Console.WriteLine(" constraction"); simplex[inputnum] = c; iteration_count++; stepalg(); } else { Console.WriteLine(" reduction"); this.reduction(); iteration_count++; stepalg(); } } else { //Console.WriteLine("constraction not finish."); return; } } } else { //Console.WriteLine("reflection not finish."); return; } } private bool ifready(experiment e) { bool add = false; foreach (experiment s in workingexps) { if (s.compare_ignore_result(e)) { if (s.done) { e = s; return true; } } else add = true; } if (add) workingexps.Add(e); return false; } private experiment ifready_exp(experiment e) { bool add = false; foreach (experiment s in workingexps) { if (s.compare_ignore_result(e)) { if (s.done) { return s; } } else add = true; } if (add) workingexps.Add(e); return null; } private experiment reflection() { experiment e = sample.clone(); e.ID = Guid.NewGuid(); e.name = "simplex_" + iteration_count; List<parameter> x0 = gravity_exp.exp_settting.paras; List<parameter> xnplus = simplex [inputnum].exp_settting.paras; for (int i = 0; i < e.exp_settting.paras.Count; i++) { e.exp_settting.paras[i].setvalue(x0[i].value+reflection_rate*(x0[i].valuexnplus[i].value)); } return e; } private experiment contraction() { experiment e = sample.clone(); e.ID = Guid.NewGuid(); e.name = "simplex_" + iteration_count; List<parameter> x0 = gravity_exp.exp_settting.paras; List<parameter> xnplus = simplex[inputnum+1-1].exp_settting.paras; for (int i = 0; i < e.exp_settting.paras.Count; i++) { e.exp_settting.paras[i].setvalue(x0[i].value + contraction_rate * (x0[i].value xnplus[i].value)); } return e; } private experiment expansion() { experiment e = sample.clone(); e.ID = Guid.NewGuid(); e.name = "simplex_" + iteration_count; List<parameter> x0 = gravity_exp.exp_settting.paras; List<parameter> xnplus = simplex[inputnum+1-1].exp_settting.paras; for (int i = 0; i < e.exp_settting.paras.Count; i++) { e.exp_settting.paras[i].setvalue(x0[i].value + expansion_rate * (x0[i].value xnplus[i].value)); } return e; } private void reduction() { for (int j = 1; j < inputnum+1; j++ ) { for (int i = 0; i < simplex[j].exp_settting.paras.Count; i++) { if(simplex[j].exp_settting.paras[i].isInput) simplex[j].exp_settting.paras[i].setvalue(simplex[j].exp_settting.paras[i].value + reduction_rate * (simplex[j].exp_settting.paras[i].value - simplex[0].exp_settting.paras[i].value)); } simplex[j].ID = Guid.NewGuid(); } } private experiment gravity() { experiment g = sample.clone(); g.name = "simplex_" + iteration_count; foreach(parameter p in g.exp_settting.paras) { if (p.isInput) p.value = 0; } for(int j=0; j<inputnum; j++) { experiment e = simplex[j]; for (int i = 0; i < inputnum; i++) { if(g.exp_settting.paras[i].isInput) g.exp_settting.paras[i].value = g.exp_settting.paras[i].value + e.exp_settting.paras[i].value; } } foreach (parameter p in g.exp_settting.paras) { if (p.isInput) p.setvalue(p.value / (inputnum)); } return g; } public void method() { if (firstrun) { init_exps(); firstrun = false; return; } stepalg (); } private double ss() { double s = 0; double avg = 0; double sum = 0; foreach (experiment x in simplex) { double r = x.obj_value; sum = sum + r; } avg = sum / (inputnum+1); foreach (experiment x in simplex) { double r = x.obj_value; s = s + (r - avg) * (r - avg); } return Math.Pow(s,0.5); } } } Artificial Neural Network Method using using using using using using using using System; System.Collections.Generic; System.Text; NeuronDotNet.Core; NeuronDotNet.Core.Backpropagation; NeuronDotNet.Core.Initializers; Util; utilities; namespace design_agent { class ann_alg : IdesignMethods { //******INTERFACE********** public List<experiment> workingexps { get; set; } public int iteration_count { get; set; } public experiment sample { get; set; } public bool finish { get; set; } public string name { get; set; } public double currentobj { get; set; } //************************* List<experiment> ReadyToExcuteExps; private bool firstrun = true; private double accurate_index = 0.005; private int numOfParameter = 0; private int batchsize = 5; //ann private private private private private private private private private List<experiment> testexps; LinearLayer inputlayer; SigmoidLayer hiddenlayer; SigmoidLayer outputlayer; BackpropagationNetwork network; int neuronCount = 10; double learningRate = 0.25; int cycles = 1000; TrainingSet trainingset; public ann_alg() { foreach (parameter p in sample.exp_settting.paras) { if(p.isInput) numOfParameter ++; } } private void method() { if (firstrun) { init(); firstrun = false; return; } annalg(); } private void init() { Random rand = new Random(); experiment e = new experiment(); for (int i = 0; i < batchsize; i++) { e = new experiment(); e = sample.clone(); e.ID = Guid.NewGuid(); e.name = "ann_" + iteration_count; foreach (parameter x in e.exp_settting.paras) { if (x.isInput) { int value = rand.Next(0, 100); x.setvalue((double)value / 100 * (x.max - x.min) + x.min); } else x.value = -1; } e.done = false; workingexps.Add(e); } inputlayer = new LinearLayer(numOfParameter); hiddenlayer = new SigmoidLayer(neuronCount); new BackpropagationConnector(inputlayer, hiddenlayer).Initializer = new RandomFunction(0d, 0.3d); new BackpropagationConnector(hiddenlayer, outputlayer).Initializer = new RandomFunction(0d, 0.3d); network = new BackpropagationNetwork(inputlayer, outputlayer); network.SetLearningRate(learningRate); trainingset = new TrainingSet(numOfParameter, 1); } private void annalg() { for (int i = 0; i < batchsize; i++) { experiment temp = new experiment(); temp = ifready_exp(testexps[i]); if (temp == null) { return; } testexps[i] = temp; } testfinish(); if (finish) return; testexps = new List<experiment>(); for (int i = 0; i < workingexps.Count; i++) { experiment temp = new experiment(); temp = ifready_exp(testexps[i]); if (temp == null) { return; } workingexps[i] = temp; double[] input = new double[numOfParameter]; int j = 0; foreach (parameter p in workingexps[i].exp_settting.paras) { if (p.isInput) { input[j] = p.value / p.max; j++; } } double[] output={workingexps[i].calobj()}; trainingset.Add(new TrainingSample(input, output)); } network.Learn(trainingset, cycles); Random rand = new Random(); experiment e = new experiment(); for (int i = 0; i < batchsize; i++) { e = new experiment(); e = sample.clone(); e.ID = Guid.NewGuid(); e.name = "ann_" + iteration_count; foreach (parameter x in e.exp_settting.paras) { if (x.isInput) { int value = rand.Next(0, 100); x.setvalue((double)value / 100 * (x.max - x.min) + x.min); } else x.value = -1; } e.done = false; workingexps.Add(e); testexps.Add(e); } } private void testfinish() { double ss = 0; for (int i = 0; i < batchsize;i++ ) { double[] input=new double[numOfParameter]; int j = 0; foreach (parameter p in testexps[i].exp_settting.paras) { if (p.isInput) { input[j] = p.value / p.max; j++; } } double[] output; output = network.Run(input); testexps[i].calobj(); ss = ss + (output[0] - testexps[i].obj_value) * (output[0] - testexps[i].obj_value); } ss = ss / batchsize; if (ss <= accurate_index) finish = true; } private experiment ifready_exp(experiment e) { bool add = false; foreach (experiment s in workingexps) { if (s.compare_ignore_result(e)) { if (s.done) { return s; } } else add = true; } if (add) workingexps.Add(e); return null; } } } 2. Sample code for Execution Agent Experimentation Interface public interface IExperimentation { List<experiment> exps { set; get; } experimentSetting standardset { set; get; } plate[] exp_plates { get; set; } void start(); } Script Composing code public Script precip_script(Script scr, layout ly, operations op) { //gettips string trans = ""; bool[] tips = new bool[8]; for (int i = 0; i < 8; i++) { tips[i] = false; } for (int i = 0; i < experiments.Length; i++) { tips[i] = true; } trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //explocation[] currentlocs = this.allocate(); //add salt double[] saltcon = new double[experiments.Length]; double[] volumn = new double[12]; string[] wells = new string[experiments.Length]; for (int i = 0; i < experiments.Length; i++) { foreach (parameter x in experiments[i].exp_settting.paras) { if (x.name.Contains("salt")) saltcon[i] = x.value; } volumn[i] = saltcon[i] * totalvolumn / salt_init_con; wells[i] = currentlocs[i].wells; } trans = op.aspirate(tips, "ZZ Water", volumn, salt_grid, salt_site, salt_location); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", volumn, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); //change tips trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //add buffer buffer bf = new buffer(); double min = 0; double max = 0; foreach (parameter x in standardset.paras) { if (x.name == "ph") { min = x.min; max = x.max; } } bf.conn(buffertype, max, min); double[] ph = new double[experiments.Length]; double[] ratio = new double[experiments.Length]; double[] chemicalA = new double[12]; double[] chemicalB = new double[12]; for (int i = 0; i < experiments.Length; i++) { foreach (parameter x in experiments[i].exp_settting.paras) { if (x.name.Contains( "ph")) { ph[i] = x.value; } } ratio[i] = bf.getRatio(initAcon, ph[i]); chemicalA[i] = totalvolumn * bufferstrength / initAcon * (1 - (1 / (ratio[i] + 1))); chemicalB[i] = totalvolumn * bufferstrength / initBcon * (1 / (ratio[i] + 1)); } //add chemicalA trans = op.aspirate(tips, "ZZ Water", chemicalA, bufferA_grid, bufferA_site, bufferA_location); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", chemicalA, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); //change tips trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //add chemicalB trans = op.aspirate(tips, "ZZ Water", chemicalB, bufferB_grid, bufferB_site, bufferB_location); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", chemicalB, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); //change tips trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //add sample double[] tempsamplevolume = new double[12]; for (int i = 0; i < experiments.Length; i++) { tempsamplevolume[i] = samplevolume; } trans = op.aspirate(tips, "ZZ Water", tempsamplevolume, samplegrid, samplesite, samplelocation); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", tempsamplevolume, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); //change tips trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //add Water double[] Watervolume = new double[12]; for (int i = 0; i < experiments.Length; i++) { Watervolume[i] = totalvolumn - samplevolume - chemicalA[i] - chemicalB[i] - volumn[i]; } trans = op.aspirate(tips, "ZZ Water", Watervolume, Watergrid, Watersite, Waterlocation); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", Watervolume, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); //change tips trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //mix double[] mixvolume = new double[12]; for (int i = 0; i < experiments.Length; i++) { mixvolume[i] = totalvolumn / 4; } trans = op.mix(tips, "ZZ Water", mixvolume, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells), 3); scr.AddScriptLine(trans); trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); //transfer explabware to reader for shaking scr.AddScriptLine("FACTS(\"ReaderNETwork\",\"ReaderNETwork_Open\",\"\",\"0\",\"\");"); trans = op.transfer(currentlocs[0].grid, 45, currentlocs[0].site + 1, 1, "MP 3Pos", "Infinite 200", "ZZ 96 Well Microplate tianyi"); scr.AddScriptLine(trans); scr.AddScriptLine("FACTS(\"ReaderNETwork\",\"ReaderNETwork_Close\",\"\",\"0\",\"\");"); //shake shaker s = new shaker(); scr.AddScriptLine(s.scr); //transfer plateback scr.AddScriptLine("FACTS(\"ReaderNETwork\",\"ReaderNETwork_Open\",\"\",\"0\",\"\");"); trans = op.transfer(45, currentlocs[0].grid, 1, currentlocs[0].site + 1, "Infinite 200", "MP 3Pos", "ZZ 96 Well Microplate tianyi"); scr.AddScriptLine(trans); scr.AddScriptLine("FACTS(\"ReaderNETwork\",\"ReaderNETwork_Close\",\"\",\"0\",\"\");"); //dilute solution 30 fold if (first) { filterlocs = currentlocs; first = false; } this.allocate(); //change tips trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); //first for (int i = 0; i < experiments.Length; i++) { volumn[i] = totalvolumn / 6; wells[i] = filterlocs[i].wells; } trans = op.aspirate(tips, "ZZ Water", volumn, filterlocs[0].grid, filterlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); for (int i = 0; i < experiments.Length; i++) { wells[i] = currentlocs[i].wells; } trans = op.dispense(tips, "ZZ Water", volumn, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); for (int i = 0; i < experiments.Length; i++) { Watervolume[i] = totalvolumn * 5 / 6; } trans = op.aspirate(tips, "ZZ Water", Watervolume, Watergrid, Watersite, Waterlocation); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", Watervolume, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); explocation[] filterlocs_2 = currentlocs; this.allocate(); //second for (int i = 0; i < experiments.Length; i++) { volumn[i] = totalvolumn / 6; wells[i] = filterlocs_2[i].wells; } trans = op.aspirate(tips, "ZZ Water", volumn, filterlocs_2[0].grid, filterlocs_2[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); for (int i = 0; i < experiments.Length; i++) { wells[i] = currentlocs[i].wells; } trans = op.dispense(tips, "ZZ Water", volumn, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); for (int i = 0; i < experiments.Length; i++) { Watervolume[i] = totalvolumn * 5 / 6; } trans = op.aspirate(tips, "ZZ Water", Watervolume, Watergrid, Watersite, Waterlocation); scr.AddScriptLine(trans); trans = op.dispense(tips, "ZZ Water", Watervolume, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); //transfer liquid to filter plate trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); trans = op.getditi2(tips, "DiTi 1000ul"); scr.AddScriptLine(trans); for (int i = 0; i < experiments.Length; i++) { volumn[i] = totalvolumn * 0.9; wells[i] = currentlocs[i].wells; } trans = op.aspirate(tips, "ZZ Water", volumn, currentlocs[0].grid, currentlocs[0].site, op.wellselection_96(wells)); scr.AddScriptLine(trans); for (int i = 0; i < experiments.Length; i++) { wells[i] = filterlocs[i].wells; } trans = op.dispense(tips, "ZZ Water", volumn, filtergrid, filtersite, op.wellselection_96(wells)); scr.AddScriptLine(trans); trans = op.drop_tips(tips, ly.getditiwastegrid(), ly.getditiwastesite(), 70, 10, 0); scr.AddScriptLine(trans); //transfer to vaccum pump trans = op.transfer(filtergrid, 36, filtersite + 1, 4, "MP 3Pos", "Te-VacS SC", "ZZ 96 Well Microplate tianyi"); scr.AddScriptLine(trans); //do filtration VacS v = new VacS(); scr.AddScriptLine(v.setpressdiff(700)); scr.AddScriptLine(v.applyvaccumfront(700)); scr = v.setduration(60, scr); scr.AddScriptLine(v.ventfront()); scr.AddScriptLine(v.deactive()); //move explabware back trans = op.transfer(36, filtergrid, 4, filtersite + 1, "Te-VacS SC", "MP 3Pos", "ZZ 96 Well Microplate tianyi"); scr.AddScriptLine(trans); //remove VacS cap scr = op.moveVaScap(scr, 36); explabwaresite = currentlocs[0].site; explabwaregrid = currentlocs[0].grid; filterlocs = currentlocs; return scr; } 3. Sample code for assay agent Sample code for controlling platereader public void fixed_uvab(string label, int numofreads, int settletime, int wavelength) { if (m_oHelper.HasAbsorbanceFixed()) { // ---------------------------------------// ATTENTION: For script creators // The ID´s must be unique and ascending!!! // ---------------------------------------int nInnerID = 1000; bool bDeviceIsM1000 = (m_oServer.ConnectedReader.Information.GetInstrumentName().Equals("S3")); assay_agent.platereaderUtil.Absorbance oAbs = new assay_agent.platereaderUtil.Absorbance(m_oHelper, nInnerID, bDeviceIsM1000); oAbs.composescript(label, numofreads, settletime, wavelength); // Get the measurement reading object MeasurementReading oMeasurementReading = oAbs.MeasurementReading; int nID = 0; // Build measurement script file TecanFile oFile = Helper.CreateTecanFile(); TecanMeasurement oMeasurement = Helper.CreateTecanMeasurement(++nID); MeasurementManualCycle oCycle = Helper.CreateManualCycle(++nID); String sPlateName = m_oPlate.FilenameWithoutExtension; CyclePlate oPlate = Helper.CreatePlate(sPlateName, ++nID); string sRange = m_sAvailablePlateRange; PlateRange oRange = Helper.CreateRange(sRange, ++nID); MeasurementAbsorbance oAbsMeas = new MeasurementAbsorbance(); oAbsMeas.Name = "ABS"; oAbsMeas.ID = ++nID; Well oWell = Helper.CreateWell(++nID); oWell.Actions.Add(oMeasurementReading); oAbsMeas.Actions.Add(oWell); oPlate.Actions.Add(oRange); oRange.Actions.Add(oAbsMeas); oCycle.Actions.Add(oPlate); oMeasurement.Actions.Add(oCycle); oFile.DocumentContent = oMeasurement; m_oCurrentMeasurementFile = oFile; string filepath = utilities.util.getfilepath("abs.xml"); FileHandling.Save(oFile, filepath); // Convert the Tecan file object to a string XmlSupport objXML = new XmlSupport(); XmlNode objNode = oFile.GetXmlNode(objXML); objXML.AddXmlNode(objNode); m_sScript = objXML.XmlDocumentAsString(); } } Sample code for HPLC peaks clustering class hplc { class CSVparser { const double maxRET = 10; const double minRET = 5; const int init_cluster_num = 10; const bool reduce_cluster = true; //for calibration curve List<string[]> parseCSV(string path) { List<string[]> parsedData = new List<string[]>(); try { using (StreamReader readFile = new StreamReader(path)) { string line; string[] row; while ((line = readFile.ReadLine()) != null) { row = line.Split(','); parsedData.Add(row); } } } catch (Exception e) { Console.Write(e.ToString()); Console.Read(); } return parsedData; } //int[0]: RetTime //int[1]: Area //int[2]: Area(%) List<double[]> retrievedata() { string aPath = Environment.CurrentDirectory; aPath = aPath + "\\hplc"; DirectoryInfo dir = new DirectoryInfo(aPath); DirectoryInfo[] subdir = dir.GetDirectories(); List<double[]> hplcdata = new List<double[]>(); int index = 0; foreach (DirectoryInfo d in subdir) { String temp = aPath + "\\" + d.Name + "\\REPORT02.CSV"; List<string[]> csvdata = this.parseCSV(temp); String[] getid = d.Name.Split(new char[] { '.' }); index = int.Parse(getid[0].Substring(6)); //Console.WriteLine(d.Name+" "+index); foreach (string[] s in csvdata) { double[] data = new double[4]; data[0] = Double.Parse(s[1]); data[1] = Double.Parse(s[4]); data[2] = index; data[3] = 0; if (data[0] > minRET && data[0] < maxRET) hplcdata.Add(data); } //index++; } return hplcdata; } //double[0] cluster center //double[1] x //double[2] c public List<double[]> concentrations(double[] s) { List<double[]> hplcdata = this.retrievedata(); cluster_alg cluster = new cluster_alg(hplcdata, s); return hplcdata; } public static int idforsearch; private static bool getdatabyid(double[] hplcdata) { bool b = false; if (hplcdata[2] == idforsearch) { } return b; } } class cluster_alg { int k; List<double[]> data; double[] centers; double[] number; public cluster_alg(int k1, List<double[]> data1, bool reduce) { this.k = k1; this.data = data1; data.TrimExcess(); centers = new double[k]; data.Sort ( delegate(double[] a1, double[] a2) { return a1[0].CompareTo(a2[0]); } ); this.cluster_init(); double before = 99999; double after = 0; while (before != after) { before = sd(data); this.cluster_main(); after = sd(data); } if (reduce) { for (int i = 0; i < 1000; i++) { optcluster(); before = 99999; after = 0; while (before != after) { before = sd(data); this.cluster_main(); after = sd(data); } } } } public cluster_alg(List<double[]> data1, double[] c) { this.k = c.Length; this.data = data1; data.TrimExcess(); centers = new double[k]; data.Sort ( delegate(double[] a1, double[] a2) { return a1[0].CompareTo(a2[0]); } ); centers = c; double before = 99999; double after = 0; while (before != after) { before = sd(data); this.cluster_main(); after = sd(data); } } void cluster_init() { Random r = new Random(); for (int i = 0; i < k; i++) { int index = (int)r.Next(data.Count / k * (i), data.Count / k * (i + 1)); double[] temp = data[index]; temp[3] = i; centers[i] = temp[0]; } } void cluster_main() { for (int i = 0; i < data.Count; i++) { double[] aRecord = data[i]; double distance = 9999999999; for (int j = 0; j < k; j++) { if (distance > (Math.Abs(centers[j] - aRecord[0]))) { distance = Math.Abs(centers[j] - aRecord[0]); aRecord[3] = j; } } } number = new double[k]; for (int j = 0; j < k; j++) { double[] sum = new double[k]; sum[j] = 0; int n = 0; for (int i = 0; i < data.Count; i++) { double[] aRecord = data[i]; if (aRecord[3] == j) { sum[j] = sum[j] + aRecord[0]; n++; } } centers[j] = sum[j] / n; number[j] = n; } } double sd(List<double[]> sample) { double sdd = 0; for (int i = 0; i < k; i++) { double temp = 0; for (int j = 0; j < sample.Count; j++) { double[] aRecord = sample[j]; if ((int)aRecord[3] == i) { temp = temp + (centers[i] - aRecord[0]) * (centers[i] - aRecord[0]); } } sdd = temp + sdd; } return sdd; } void optcluster() { List<double[]> centerinfo = new List<double[]>(); for (int i = 0; i < k; i++) { double[] temp = new double[2]; temp[0] = centers[i]; temp[1] = number[i]; centerinfo.Add(temp); } centerinfo.TrimExcess(); centerinfo.Sort( delegate(double[] a1, double[] a2) { return (a2[1].CompareTo(a1[1])); } ); int count = 0; int n = 0; for (int i = 0; i < k; i++) { if (n < 0.8 * data.Count) { double[] aInfo = centerinfo[i]; n = n + (int)aInfo[1]; count = i + 1; } } centers = new double[count]; for (int i = 0; i < count; i++) { double[] aInfo = centerinfo[i]; centers[i] = aInfo[0]; } k = count; number = new double[k]; } } 4. Sample code for Coordinate Agent Defining experiment public class experiment { public Guid ID; public double obj_value; public string name; public experimentSetting exp_settting; public bool done; public experiment clone() { experiment e = new experiment(); e.ID = this.ID; e.name = this.name; e.obj_value = this.obj_value; ; e.exp_settting.obj_function = this.exp_settting.obj_function; e.exp_settting.paras = new List<parameter>(); e.exp_settting.proteins = new List<protein>(); foreach (parameter p in this.exp_settting.paras) { parameter ap = new parameter(); ap.alias = p.alias; ap.interval = p.interval; ap.isInput = p.isInput; ap.max = p.max; ap.min = p.min; ap.name = p.name; ap.value = p.value; e.exp_settting.paras.Add(ap); } foreach (protein p in this.exp_settting.proteins) { protein ap = new protein(); ap.name = p.name; ap.retentiontime = p.retentiontime; e.exp_settting.proteins.Add(ap); } return e; } public experiment clone(experiment e) { e = new experiment(); e.ID = this.ID; e.name = this.name; e.obj_value = this.obj_value; ; e.exp_settting.obj_function = this.exp_settting.obj_function; e.exp_settting.paras = new List<parameter>(); e.exp_settting.proteins = new List<protein>(); foreach (parameter p in this.exp_settting.paras) { parameter ap = new parameter(); ap.alias = p.alias; ap.interval = p.interval; ap.isInput = p.isInput; ap.max = p.max; ap.min = p.min; ap.name = p.name; ap.value = p.value; e.exp_settting.paras.Add(ap); } foreach (protein p in this.exp_settting.proteins) { protein ap = new protein(); ap.name = p.name; ap.retentiontime = p.retentiontime; e.exp_settting.proteins.Add(ap); } return e; } public experiment () { //ID=999999999; obj_value=-9999999999; exp_settting=new experimentSetting(); done=false; ID = new Guid(); } public XmlElement getXmlElement (XmlDocument temp) { if (this.done) this.obj_value=this.calobj(); XmlElement exp=temp.CreateElement("experiment"); exp.SetAttribute("ID", this.ID.ToString()); exp.SetAttribute("obj_value", this.obj_value.ToString()); exp.SetAttribute("name", this.name); exp.SetAttribute("done", this.done.ToString()); exp.AppendChild(this.exp_settting.getXmlElement(temp)); return exp; } public bool compare(experiment e) { if (e.exp_settting.compare(this.exp_settting)) return true; else return false; } public bool compare_ignore_result(experiment e) { if(e.exp_settting.compare_ignore_result(this.exp_settting)) return true; else return false; } public double calobj () { Assembly code=this.objclass(); Type tp=code.GetType("objcalculation.calobj"); MethodInfo mi=tp.GetMethod("cal"); double objvalue=(double)mi.Invoke(null,null); this.obj_value = Math.Round(objvalue, 2); return this.obj_value; } private string trans_objfunc() { string code = "using System;\n"+ "namespace objcalculation\n"+ "{\n"+ "public static class calobj \n"+ "{ \n"; code = code + "\n" + "public static double cal() \n" + "{\n" ; foreach (parameter p in this.exp_settting.paras) { if (p.isInput) { code = code + "double " + p.alias +"="+ p.value +";\n"; } } code = code + "return " + this.exp_settting.obj_function + ";\n" + "}\n"+ "}\n"+ "}\n"; return code; } private Assembly objclass () { CSharpCodeProvider provider = new CSharpCodeProvider (); CompilerParameters ps = new CompilerParameters (); ps.GenerateExecutable = false; ps.GenerateInMemory = true; StringBuilder classSource = new StringBuilder (); classSource.Append (this.trans_objfunc ()); //Console.WriteLine (classSource.ToString ()); CompilerResults result = provider.CompileAssemblyFromSource (ps, classSource.ToString ()); //Console.WriteLine(result.Errors.Count); Assembly assembly = result.CompiledAssembly; return assembly; } public void writetodatabase() { if (!this.done) { Console.WriteLine("Write experiment data failed! The experiment has not been finished!"); return; } List<experiment> exps = new List<experiment>(); String conn_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\tecan_database.mdb"; System.Data.OleDb.OleDbConnection dataCon = new System.Data.OleDb.OleDbConnection(); dataCon.ConnectionString = conn_string; dataCon.Open(); String query_string; OleDbCommand query=null; OleDbDataReader reader; //parameters List<int> paraids = new List<int>(); List<double> values = new List<double>(); foreach (parameter p in this.exp_settting.paras) { query_string = "select * from [paras] where name='" + p.name + "'"; query = new OleDbCommand(query_string, dataCon); reader = query.ExecuteReader(); if (reader.Read()) { paraids.Add((int)reader.GetValue(0)); values.Add(p.value); } else { query_string = "insert into" + "[paras] ([name], [max], [min], [interval],[isinput],[alias]) values (" + "'" + p.name + "', " + p.max + ", " + p.min + ", " + p.interval + ", " + p.isInput + ", '" + p.alias + "')"; query = new OleDbCommand(query_string, dataCon); query.ExecuteNonQuery(); query = new OleDbCommand("SELECT @@IDENTITY", dataCon); reader = query.ExecuteReader(); reader.Read(); int id = (int)reader.GetValue(0); paraids.Add(id); values.Add(p.value); } } //proteins List<int> proids=new List<int>(); foreach (protein p in this.exp_settting.proteins) { //reader = new OleDbDataReader(); query_string = "select * from [protein] where name='" + p.name + "'"; query = new OleDbCommand(query_string, dataCon); reader = query.ExecuteReader(); if (reader.Read()) { proids.Add((int)reader.GetValue(0)); } else { query_string = "insert into" + "[protein] ([name], [retention]) values (" + "'" + p.name + "', " + p.retentiontime + ")"; query = new OleDbCommand(query_string, dataCon); query.ExecuteNonQuery(); query = new OleDbCommand("SELECT @@IDENTITY", dataCon); reader = query.ExecuteReader(); reader.Read(); int id = (int)reader.GetValue(0); proids.Add(id); } } //exp-setting int setid; query_string = "select * from [exp_setting] where obj='" + this.exp_settting.obj_function + "'"; query = new OleDbCommand(query_string, dataCon); reader = query.ExecuteReader(); if (reader.Read()) { setid = (int)reader.GetValue(0); } else { query_string = "insert into" + "[exp_setting] ([obj]) values (" + "'" + this.exp_settting.obj_function + "' " + ")"; query = new OleDbCommand(query_string, dataCon); query.ExecuteNonQuery(); query = new OleDbCommand("SELECT @@IDENTITY", dataCon); //reader = new OleDbDataReader(); reader = query.ExecuteReader(); reader.Read(); setid = (int)reader.GetValue(0); //setting-protein for (int i = 0; i < proids.Count; i++) { query_string = "insert into" + "[setting-proteins] ([proid],[setid]) values (" + proids[i] + "," + setid + ")"; query = new OleDbCommand(query_string, dataCon); query.ExecuteNonQuery(); } } //exp query_string = "insert into" + "[exp] ([name],[exp_setting_id],[obj_value]) values (" + "'" + this.name + "', " + setid + ", " + this.calobj() + ")"; query = new OleDbCommand(query_string, dataCon); query.ExecuteNonQuery(); query = new OleDbCommand("SELECT @@IDENTITY", dataCon); reader = query.ExecuteReader(); reader.Read(); int expid = (int)reader.GetValue(0); //para-exp for (int i=0;i<paraids.Count;i++) { query_string = "insert into" + "[para-exp] ([exp_id],[para_id],[value]) values (" + expid + "," + paraids[i] +","+ values[i] + ")"; query = new OleDbCommand(query_string, dataCon); query.ExecuteNonQuery(); } query.Dispose(); reader.Dispose(); dataCon.Dispose(); } } public class experimentSetting { public string obj_function; public List<parameter> paras; public List<protein> proteins; public experimentSetting () { paras=new List<parameter>(); proteins=new List<protein>(); } public XmlElement getXmlElement (XmlDocument temp) { XmlElement setting=temp.CreateElement("setting"); foreach (parameter p in paras) { setting.AppendChild (p.getXmlElement (temp)); } foreach (protein p in proteins) { setting.AppendChild (p.getXmlElement(temp)); } setting.SetAttribute("obj_function", obj_function); return setting; } public bool compare(experimentSetting es) { if (es.obj_function == this.obj_function && this.compare_paras(es.paras) && this.compare_proteins(es.proteins)) { return true; } return false; } public bool compare_ignore_result(experimentSetting es) { if (es.obj_function == this.obj_function && this.compare_paras_ignore_result(es.paras) && this.compare_proteins(es.proteins)) { return true; } return false; } public bool compare_paras(List<parameter> ps) { if (this.paras.Count == ps.Count) { foreach (parameter pa in ps) { bool a = false; foreach (parameter pb in this.paras) { if (pa.compare(pb)) { a = true; break; } } if (!a) return false; } } return true; } public bool compare_paras_ignore_result(List<parameter> ps) { if (this.paras.Count == ps.Count) { foreach (parameter pa in ps) { bool a = false; foreach (parameter pb in this.paras) { if (pa.compare_ignore_result(pb)) { a = true; break; } } if (!a) return false; } } return true; } public bool compare_proteins(List<protein> ps) { if (this.paras.Count == ps.Count) { foreach (protein pa in ps) { bool a = false; foreach (protein pb in this.proteins) { if (pa.compare(pb)) { a = true; break; } } if (!a) return false; } } return true; } } public class parameter { public string name; public double value; public double max; public double min; public double interval; public bool isInput; public string alias; public XmlElement getXmlElement(XmlDocument temp) { XmlElement para=temp.CreateElement("parameter"); para.SetAttribute("name",this.name); para.SetAttribute("value", this.value.ToString()); para.SetAttribute("max", this.max.ToString()); para.SetAttribute("min", this.min.ToString()); para.SetAttribute("interval", this.interval.ToString()); para.SetAttribute("isInput", this.isInput.ToString()); para.SetAttribute("alias", this.alias); return para; } public void setvalue(double v) { if (v >= this.max) { this.value = max; return; } if (v <= this.min) { this.value = min; return; } if (this.isInput) { double test = max; while (test > v) { test = test - interval; } double left = v - test; double right = test + interval - v; if (left >= right) { this.value = test + interval; } else { this.value = test; } } string delimStr = "."; char[] delimiter = delimStr.ToCharArray(); string words = this.interval.ToString(); string[] split = null; split = words.Split(delimiter); this.value=Math.Round(this.value,split[1].Length); } public bool compare(parameter p) { bool b = false; if (p.alias == this.alias && p.interval == this.interval && p.isInput == this.isInput && p.max == this.max && p.min == this.min && p.name == this.name && p.value == this.value) b = true; return b; } public bool compare_ignore_result(parameter p) { bool b = false; if (p.alias == this.alias && p.interval == this.interval && p.isInput == this.isInput && p.max == this.max && p.min == this.min && p.name == this.name && !this.isInput) b = true; else if (p.alias == this.alias && p.interval == this.interval && p.isInput == this.isInput && p.max == this.max && p.min == this.min && p.name == this.name && Math.Abs(p.value-this.value)<this.interval) return true; return b; } } public class protein { public string name; public double retentiontime; public XmlElement getXmlElement(XmlDocument temp) { XmlElement pro=temp.CreateElement("protein"); pro.SetAttribute("name",this.name); pro.SetAttribute("retention_time", this.retentiontime.ToString()); return pro; } public bool compare(protein p) { bool b = false; if (p.name == this.name && p.retentiontime == this.retentiontime) b = true; return b; } } Defining platform layout public class explocation { //public string labwarename; public int grid; public int site; public string wells; public bool used; } public class layout { public explocation[,] locs; public class carrier_info { public int name; public int grid; public List<labware_info> labwares; } public class labware_info { public int name; public int site; public string nickname; public bool ifexplocation; } public class exp_location { //public string name; public int site; public int grid; } public List<carrier_info> layoutlist; private bool first = true; public List<exp_location> explocs; public void load_layout(int id) { String conn_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\tecan_database.mdb"; System.Data.OleDb.OleDbConnection dataCon = new System.Data.OleDb.OleDbConnection(); dataCon.ConnectionString = conn_string; dataCon.Open(); String query_string; query_string = "select * from layout_tree where layout_id=" + id + " order by carrier, carrier_grid"; OleDbCommand query = new OleDbCommand(query_string, dataCon); OleDbDataAdapter adapter = new OleDbDataAdapter(query_string, dataCon); OleDbDataReader reader = query.ExecuteReader(); carrier_info temp_carrier = new carrier_info(); int carrier_name = 0; int carrier_grid = 0; carrier_info aCarrier = new carrier_info(); layoutlist = new List<carrier_info>(); bool a = true; while (reader.Read()) { //print(reader.GetValue(0) + " " + reader.GetValue(2)); if ((int)reader.GetValue(0) == carrier_name && (int)reader.GetValue(2) == carrier_grid) { labware_info aLabware = new labware_info(); aLabware.name = (int)reader.GetValue(1); aLabware.site = (int)reader.GetValue(3); aLabware.nickname = (string)reader.GetValue(5); aLabware.ifexplocation = (bool)reader.GetBoolean(6); aCarrier.labwares.Add(aLabware); } else { if (!a) { layoutlist.Add(aCarrier); } a = false; aCarrier = new carrier_info(); aCarrier.labwares = new List<labware_info>(); aCarrier.name = (int)reader.GetValue(0); carrier_name = (int)reader.GetValue(0); aCarrier.grid = (int)reader.GetValue(2); carrier_grid = (int)reader.GetValue(2); labware_info aLabware = new labware_info(); aLabware.name = (int)reader.GetValue(1); aLabware.site = (int)reader.GetValue(3); aLabware.nickname = (string)reader.GetValue(5); aLabware.ifexplocation = (bool)reader.GetBoolean(6); aCarrier.labwares.Add(aLabware); } } layoutlist.Add(aCarrier); reader.Dispose(); query.Dispose(); adapter.Dispose(); dataCon.Close(); dataCon.Dispose(); } public Script layout_script(Script scr) { explocs = new List<exp_location>(); String conn_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\tecan_database.mdb"; System.Data.OleDb.OleDbConnection dataCon = new System.Data.OleDb.OleDbConnection(); dataCon.ConnectionString = conn_string; dataCon.Open(); String query_string; foreach (carrier_info x in layoutlist) { query_string = "select * from carrier where ID=" + x.name; OleDbCommand query = new OleDbCommand(query_string, dataCon); OleDbDataAdapter adapter = new OleDbDataAdapter(query_string, dataCon); OleDbDataReader reader = query.ExecuteReader(); string name; while (reader.Read()) { name = (string)reader.GetValue(1); scr.AddCarrier(name, x.grid); } //int i=0; foreach (labware_info y in x.labwares) { query_string = "select * from labware where ID=" + y.name; OleDbCommand query2 = new OleDbCommand(query_string, dataCon); OleDbDataAdapter adapter2 = new OleDbDataAdapter(query_string, dataCon); OleDbDataReader reader2 = query2.ExecuteReader(); string carriername = ""; while (reader2.Read()) { carriername = (string)reader2.GetValue(1); } if (y.site != 0) scr.AddLabware(carriername, x.grid, y.site, y.nickname); //i++; if (y.ifexplocation) { exp_location el = new exp_location(); el.site = y.site; el.grid = x.grid; explocs.Add(el); } query2.Dispose(); adapter2.Dispose(); reader2.Dispose(); } query.Dispose(); adapter.Dispose(); reader.Dispose(); } dataCon.Close(); dataCon.Dispose(); if (first) { locs = new explocation[explocs.Count, 96]; for (int i = 0; i < explocs.Count; i++) { for (int j = 0; j < 96; j++) { locs[i, j] = new explocation(); } } layout.exp_location[] templocs = explocs.ToArray(); for (int i = 0; i < explocs.Count; i++) { for (int j = 0; j < 96; j++) { locs[i, j].grid = templocs[i].grid; locs[i, j].site = templocs[i].site; locs[i, j].used = false; locs[i, j].wells = transwellstring(j); } } first = false; } return scr; } private string transwellstring(int well) { string wellstring = ""; int x = (int)well / 8; int y = well - x * 8; x = x + 1; wellstring = Convert.ToChar(65 + y) + "" + x; return wellstring; } public int getditiwastegrid() { int grid = 0; foreach (layout.carrier_info x in layoutlist) { foreach (layout.labware_info y in x.labwares) { if (y.nickname == "diti_waste") { grid = x.grid; } } } return grid; } public int getditiwastesite() { int site = 0; foreach (layout.carrier_info x in layoutlist) { foreach (layout.labware_info y in x.labwares) { if (y.nickname == "diti_waste") { site = y.site - 1; } } } return site; } public int getwastegrid() { int wastegrid = 0; foreach (layout.carrier_info x in layoutlist) { foreach (layout.labware_info y in x.labwares) { if (y.nickname == "waste") { wastegrid = x.grid; } } } return wastegrid; } public int getwastesite() { int wastesite = 0; foreach (layout.carrier_info x in layoutlist) { foreach (layout.labware_info y in x.labwares) { if (y.nickname == "waste") { wastesite = y.site - 1; } } } return wastesite; } public int getcleanergrid() { int cleanergrid = 0; foreach (layout.carrier_info x in layoutlist) { foreach (layout.labware_info y in x.labwares) { if (y.nickname == "shallow") { cleanergrid = x.grid; } } } return cleanergrid; } public int getcleanersite() { int cleanersite = 0; foreach (layout.carrier_info x in layoutlist) { foreach (layout.labware_info y in x.labwares) { if (y.nickname == "shallow") { cleanersite = y.site - 1; } } } return cleanersite; } } Define buffer and related calculation public class buffer { public class buffertype { public int id; public int[] charge = new int[2]; public double[] K = new double[3]; public String[] type = new String[2]; } List<buffertype> bufferResults = new List<buffertype>(); private double ratio; public void conn(String perference, double phmax, double phmin) { String conn_string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\tecan_database.mdb"; //print(conn_string); System.Data.OleDb.OleDbConnection dataCon = new System.Data.OleDb.OleDbConnection(); dataCon.ConnectionString = conn_string; dataCon.Open(); String query_string; if (perference != "N/A") query_string = "select * from buffer_system where buffer_name='" + perference + "' and maxpH>" + phmax + " and minpH<" + phmin; else query_string = "select * from buffer_system where maxpH>" + phmax + " and minpH<" + phmin; OleDbCommand query = new OleDbCommand(query_string, dataCon); OleDbDataAdapter adapter = new OleDbDataAdapter(query_string, dataCon); OleDbDataReader reader = query.ExecuteReader(); // decision module to deal wth the situation when more than one result show up while (reader.Read()) { buffertype aBuffer = new buffertype(); //read chemicals int caid = (int)reader.GetValue(1); int cbid = (int)reader.GetValue(2); String chemicalquery = "select chemical.charge, chemical.base, type.type from chemical inner join type on chemical.type=type.ID where chemical.ID=" + caid + " or chemical.ID=" + cbid; query = new OleDbCommand(chemicalquery, dataCon); adapter = new OleDbDataAdapter(chemicalquery, dataCon); OleDbDataReader aReader = query.ExecuteReader(); int baseid = 0; int i = 0; while (aReader.Read()) { aBuffer.charge[i] = (int)aReader.GetValue(0); aBuffer.type[i] = (String)aReader.GetValue(2); aBuffer.id = (int)reader.GetValue(0); if (aBuffer.type[i].Equals("salt")) baseid = (int)reader.GetValue(1); i++; } // read K values of the base String Kquery = "select * from base where ID=" + baseid; query = new OleDbCommand(Kquery, dataCon); adapter = new OleDbDataAdapter(Kquery, dataCon); OleDbDataReader bReader = query.ExecuteReader(); while (bReader.Read()) { aBuffer.K[0] = (double)bReader.GetValue(2); aBuffer.K[1] = (double)bReader.GetValue(3); aBuffer.K[2] = (double)bReader.GetValue(4); } bufferResults.Add(aBuffer); aReader.Dispose(); bReader.Dispose(); } dataCon.Close(); this.selectbuffer(bufferResults); } //assume concentrations of two solutions are same private void calvolume(double c, double ph) { buffertype aBuffer = new buffertype(); int charge = 0; aBuffer = bufferResults[0]; int typenum = 0; if (aBuffer.type[0].Equals(aBuffer.type[1])) typenum = 0; else if (aBuffer.type[0].Equals("acid") || aBuffer.type[1].Equals("acid")) typenum = 1; else typenum = 2; for (int i = 0; i < 2; i++) { if (aBuffer.type[i].Equals("salt")) { charge = aBuffer.charge[i]; } } switch (typenum) { case 0: this.calSalt(c, ph, aBuffer.K[0], aBuffer.K[1], aBuffer.K[2]); break; case 1: this.calAcid(c, ph, aBuffer.K[0], aBuffer.K[1], aBuffer.K[2], charge); break; case 2: this.calAlkali(c, ph, aBuffer.K[0], aBuffer.K[1], aBuffer.K[2], charge); break; } } private void selectbuffer(List<buffertype> bufferResults) { //if (bufferResults.Count == 1) print("selected!"); } private double calSalt(double c, double ph, double k1, double k2, double k3) { double hyd = Math.Pow(10, -ph); k1 = Math.Pow(10, -k1); k2 = Math.Pow(10, -k2); k3 = Math.Pow(10, -k3); double double double double r0 r1 r2 r3 = = = = 1; hyd / k1; hyd / k2; hyd / k3; r1 = r0 / r1; r2 = r1 / r2; r3 = r2 / r3; double sum = r0 + r1 + r2 + r3; r0 r1 r2 r3 = = = = r0 r1 r2 r3 / / / / sum sum sum sum * * * * c c c c / / / / 1000; 1000; 1000; 1000; ratio = (r0 + r1) / (r2 + r3); return ratio; } private double calAcid(double c, double ph, double k1, double k2, double k3, int charge) { double hyd = Math.Pow(10, -ph); k1 = Math.Pow(10, -k1); k2 = Math.Pow(10, -k2); k3 = Math.Pow(10, -k3); double double double double r0 r1 r2 r3 = = = = 1; hyd / k1; hyd / k2; hyd / k3; r1 = r0 / r1; r2 = r1 / r2; r3 = r2 / r3; double sum = r0 + r1 + r2 + r3; r0 r1 r2 r3 = = = = r0 r1 r2 r3 / / / / sum sum sum sum * * * * c c c c / / / / 1000; 1000; 1000; 1000; sum = r0 + r1 + r2 + r3; double Cl = hyd + c * charge / 1000 - Math.Pow(10, ph - 14) - sum; ratio = Cl / sum; return ratio; } private double calAlkali(double c, double ph, double k1, double k2, double k3, int charge) { double hyd = Math.Pow(10, -ph); k1 = Math.Pow(10, -k1); k2 = Math.Pow(10, -k2); k3 = Math.Pow(10, -k3); double double double double r0 r1 r2 r3 = = = = 1; hyd / k1; hyd / k2; hyd / k3; r1 = r0 / r1; r2 = r1 / r2; r3 = r2 / r3; double sum = r0 + r1 + r2 + r3; r0 r1 r2 r3 = = = = r0 r1 r2 r3 / / / / sum sum sum sum * * * * c c c c / / / / 1000; 1000; 1000; 1000; sum = r0 + r1 + r2 + r3; double OH = sum + Math.Pow(10, ph - 14) - hyd - charge * c / 1000; ratio = OH / sum; return ratio; } public double getRatio(double c, double ph) { this.calvolume(c, ph); return ratio; } }
© Copyright 2024