hello,
I have this code, which is a Harmony Search Algorithm code, and I integrated it with a data base, but I'm having this error (attached picture) and the full code are below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Data; using System.Data.OleDb; namespace MultiObjectiveHS { public class CostOptimizer : MOHS.IFunction { #region Fields MOHS hs; int nVar; double[] xlb; double[] xub; int Iteration = 0; public string address = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory); StreamWriter sw1; double[] bestSolution; string str1 = DateTime.Now.ToLongDateString(); string str2 = (DateTime.Now.Hour).ToString(); string str3 = (DateTime.Now.Minute).ToString(); string str4 = (DateTime.Now.Second).ToString(); string name2, name1; #endregion public void Solve() { name1 = address + "\\Report.txt"; // Input number of variables (1) nVar = 2; // number of variables xlb = new double[nVar]; xub = new double[nVar]; RunHS(); } public double[] F(double[] x) { int[] y; y = new int[x.Length]; for (int i = 0; i < x.Length; ++i) { Console.WriteLine(x[i]); y[i] = Convert.ToInt32(x[i]); } double[][] cost = new double[x.Length][]; for (int i = 0; i < y.Length; ++i) { OleDbConnection connect = new OleDbConnection(); connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dropbox\Bushra\code for test with database\test.accdb;Persist Security Info=False;"; connect.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = connect; command.CommandText = "select TC from calculations"; OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.Write(reader[0].ToString()); } connect.Close(); cost[i] = new double[2]; } double[][] carbonFootprint = new double[x.Length][]; for (int i = 0; i < carbonFootprint.Length; ++i) { OleDbConnection connect = new OleDbConnection(); connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dropbox\Bushra\code for test with database\test.accdb;Persist Security Info=False;"; connect.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = connect; command.CommandText = "select TCE from calculations"; OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.Write(reader[0].ToString()); } connect.Close(); carbonFootprint[i] = new double[2]; } double totalPrice = TotalPrice(cost, y); double totalCarbonFootprint = TotalCarbonFootprint(carbonFootprint, y); double[] f = new double[hs.nObjectives]; Iteration++; try { f[0] = totalPrice; // objective 1 f[1] = totalCarbonFootprint; // objective 2 } catch (Exception e) // in case of error...! { // Final Outputs f[0] = f[1] = double.MaxValue; Console.WriteLine(e.Message); } #region Report try { if (Iteration == 1) { File.Delete(name1); } sw1 = File.AppendText(name1); sw1.Write(Iteration.ToString() + "\t"); sw1.Write(f[0].ToString("e") + "\t" + f[1].ToString("e") + "\t"); for (int i = 0; i < bestSolution.Length - 2; i++) sw1.Write(x[i].ToString() + "\t"); sw1.WriteLine(); sw1.Close(); if (Iteration % 10 == 0) Console.WriteLine("# " + Iteration.ToString() + " f0 = " + (f[0]).ToString("e") + " f1 = " + (f[1]).ToString("e")); if (Iteration % 50 == 0) { hs.choosePareto(); Utility.WriteMatrix(hs.ParetoSet, "Pareto Front", "e3"); } } catch (Exception e) { Console.WriteLine(e.Message); } #endregion return f; } double TotalPrice(double[][] cost, int[] y) { double totalPrice = 0; for (int i = 0; i < y.Length; ++i) { Console.WriteLine("y = " + y[i]); totalPrice += cost[y[i]][23]; } return totalPrice; } double TotalCarbonFootprint(double[][] carbonFootprint, int[] y) { double totalCarbonFootprint = 0; for (int i = 0; i < y.Length; ++i) { totalCarbonFootprint += carbonFootprint[y[i]][23]; } return totalCarbonFootprint; } public void RunHS() { hs = new MOHS(); //hs.BW = 5.0e-2; hs.nObjectives = 2; // I HAVE NOT TESTED THIS CODE FOR MORE THAN 2 OBJECTIVES!!! hs.isVariableBW = true; hs.isVariablePAR = true; hs.BWmax = 5e-1; hs.BWmin = 5e-3; hs.PARmin = .4; hs.PARmax = .9; hs.NVAR = nVar; hs.HMCR = .8; hs.HMS = 80; hs.maxIter = 10000; hs.setBounds(xlb, xub); hs.VarType = 0; // integer //hs.IntegerBW = 1; // //hs.IntegerPermuteRange = true; int[] type = new int[nVar]; bestSolution = new double[nVar + hs.nObjectives]; #region SetVariableRange type[0] = 0; //Input Ranges (2) double[] var0 = { 1,2,3,4,5,6,7,8,9,10,11 }; // allowable values for x0 hs.Ranges.Add(var0); type[1] = 0; double[] var1 = { 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,23 }; hs.Ranges.Add(var1); //xlb[0] = 0; //xub[0] = 0.0001; #endregion hs.Type = type; hs.Solve(this); } } }
, whatever I changed in this line, it is still there, I couldn't figure what's wrong.
I appreciate your help.
programming is too much for my brain :(