public MaxAttemptsResultModel MultiStartHillClimbingMethod(KnapsackModel model, int maxAttemts, bool objDetails, double alpha) { var maxAttemptsResultModel = new MaxAttemptsResultModel(); maxAttemptsResultModel.AttemptStatistics = new SortedDictionary <double, double>(); maxAttemptsResultModel.ObjectivesDetails = new List <List <double> >(); for (int attempt = 0; attempt < maxAttemts; attempt++) { var result = HillClimbingMethod(model, alpha, objDetails); if (maxAttemptsResultModel.AttemptStatistics.ContainsKey(result.ResultC)) { maxAttemptsResultModel.AttemptStatistics[result.ResultC] += 1; } else { maxAttemptsResultModel.AttemptStatistics.Add(result.ResultC, 1); } maxAttemptsResultModel.ObjectivesDetails.Add(result.ObjectiveDetails); } maxAttemptsResultModel.Delta = maxAttemptsResultModel.AttemptStatistics.Keys.Last() - maxAttemptsResultModel.AttemptStatistics.Keys.First(); return(maxAttemptsResultModel); }
public KnapsackModel ReadFile(string path) { string[] lines = File.ReadAllLines(path); KnapsackModel model = new KnapsackModel(); model.N = Convert.ToInt32(lines[0]); model.C = Convert.ToDouble(lines[lines.Length - 1]); for (int i = 1; i < lines.Length - 1; i++) { var splitedLine = lines[i].Split(new[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); var tupel = new Tuple <double, double>(Convert.ToDouble(splitedLine[1]), Convert.ToDouble(splitedLine[2])); if (model.Items == null) { model.Items = new List <Tuple <double, double> >() { tupel }; } else { model.Items.Add(tupel); } } return(model); }
private void defaultFileButton_Click(object sender, EventArgs e) { string path = "C:\\Users\\User\\Downloads\\StartTest.in"; FileProcess fileProcess = new FileProcess(); knapsackModel = fileProcess.ReadFile(path); textBoxN.Text = Convert.ToString(knapsackModel.N); textBoxTotalC.Text = Convert.ToString(knapsackModel.C); }
public TotalModel CalculateNewObjective(BitArray X, KnapsackModel model, double alpha) { var totalModel = new TotalModel(); for (int i = 0; i < model.N; i++) { totalModel.TotalCost += model.Items[i].Item1 * Convert.ToDouble(X[i]); totalModel.TotalWeight += model.Items[i].Item2 * Convert.ToDouble(X[i]); } totalModel.TotalCost = totalModel.TotalCost - alpha * Math.Max(totalModel.TotalWeight - model.C, 0); return(totalModel); }
private void findFileButton_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string path = openFileDialog.FileName; FileProcess fileProcess = new FileProcess(); knapsackModel = fileProcess.ReadFile(path); textBoxN.Text = Convert.ToString(knapsackModel.N); textBoxTotalC.Text = Convert.ToString(knapsackModel.C); } }
public TotalModel CalculateObjectiveValue(BitArray X, KnapsackModel model) { var totalModel = new TotalModel(); for (int i = 0; i < model.N; i++) { totalModel.TotalCost += model.Items[i].Item1 * Convert.ToDouble(X[i]); totalModel.TotalWeight += model.Items[i].Item2 * Convert.ToDouble(X[i]); if (totalModel.IsModelAppropriate && totalModel.TotalWeight > model.C) { totalModel.IsModelAppropriate = false; } } return(totalModel); }
public void Initialize() { // create example Items var csv = new CsvReader(File.OpenText("knapsackItems.csv")); csv.Configuration.Delimiter = ";"; csv.Configuration.CultureInfo = new CultureInfo("en-US"); csv.Configuration.RegisterClassMap <KnapsackItemMap>(); _items = csv.GetRecords <KnapsackItem>().ToList(); // maximum weight of all the items _maxWeight = 10.8; // Use long names for easier debugging/model understanding. var config = new Configuration { NameHandling = NameHandlingStyle.UniqueLongNames, ComputeRemovedVariables = true }; using (var scope = new ModelScope(config)) { // create a model, based on given data and the model scope var knapsackModel = new KnapsackModel(_items, _maxWeight); // Get a solver instance, change your solver using (var solver = new GurobiSolver()) { // solve the model var solution = solver.Solve(knapsackModel.Model); // import the results back into the model knapsackModel.Model.VariableCollections.ForEach(vc => vc.SetVariableValues(solution.VariableValues)); foreach (var knapsackItem in _items) { knapsackItem.IsPacked = Math.Abs(knapsackModel.y[knapsackItem].Value - 1) < scope.EPSILON; } } } }
public TotalModel NextObjectiveValue(int[] NFlip, BitArray X, TotalModel prevTotalModel, KnapsackModel model) { var totalModel = new TotalModel(); totalModel.TotalCost = prevTotalModel.TotalCost; totalModel.TotalWeight = prevTotalModel.TotalWeight; //for each changed bit in start array foreach (var bitNumber in NFlip) { if (bitNumber != 0) //stop because there is no bitNumber elements left { //if add element if (X[bitNumber - 1] == false) { totalModel.TotalCost += model.Items[bitNumber - 1].Item1; totalModel.TotalWeight += model.Items[bitNumber - 1].Item2; } //if delete element else { totalModel.TotalCost -= model.Items[bitNumber - 1].Item1; totalModel.TotalWeight -= model.Items[bitNumber - 1].Item2; } } } if (totalModel.TotalWeight > model.C) { totalModel.IsModelAppropriate = false; } return(totalModel); }
public ResultModel NewFirstAscendHillClimbingMethod(KnapsackModel model, double alpha, bool needObjectiveDetails = false) { //selec inital X at random BitArray X = XInit != null ? XInit : new RandomBitGeneration().GenerateBitArray(model.N); //initialize settings var neighborhood = new Neighborhood(); neighborhood.InitNFlip(1, model.N); var objectiveCalculation = new ObjectiveCalculation(); var xTotalModel = objectiveCalculation.CalculateNewObjective(X, model, alpha); new FileProcess().WriteFile(X, xTotalModel, "ResultFAHillClimbing"); //for objective Details var objectiveDetails = new List <double>(); if (needObjectiveDetails) { objectiveDetails.Add(xTotalModel.IsModelAppropriate ? xTotalModel.TotalCost : -1); } while (!neighborhood.stop) { BitArray Y = neighborhood.GetNextNeighborhood(X); var yTotalModel = objectiveCalculation.CalculateNewObjective(Y, model, alpha); if (xTotalModel.TotalCost < yTotalModel.TotalCost) { X = (BitArray)Y.Clone(); xTotalModel = new TotalModel { TotalCost = yTotalModel.TotalCost, TotalWeight = yTotalModel.TotalWeight, IsModelAppropriate = yTotalModel.IsModelAppropriate }; neighborhood.InitNFlip(1, model.N); new FileProcess().WriteFile(X, xTotalModel, "ResultFAHillClimbing"); if (needObjectiveDetails) { objectiveDetails.Add(xTotalModel.IsModelAppropriate ? xTotalModel.TotalCost : -1); } } else { neighborhood.MoveNext(); } } var resultModel = new ResultModel(); resultModel.ResultC = xTotalModel.TotalCost; resultModel.ResultW = xTotalModel.TotalWeight; resultModel.X = (BitArray)X.Clone(); if (needObjectiveDetails) { resultModel.ObjectiveDetails = objectiveDetails; } return(resultModel); }
public HillClimbing(KnapsackModel model) { XInit = new RandomBitGeneration().GenerateBitArray(model.N); }