static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Potreban je jedan argument, put do datoteke s vrijednostima i cijenama!"); } else { KnapsackGroupsCore core = new KnapsackGroupsCore(); try { Stopwatch stopwatch = Stopwatch.StartNew(); DynamicTable table = core.ParseInitialTableFromFile(args[0]); core.SolveProblem(false); Console.WriteLine("Traženi zbroj cijena: " + table.TargetCost); Console.WriteLine("Odabrani:"); table.BestTableObjects.Reverse(); foreach (int tableObjectIndex in table.BestTableObjects) { DynamicTableObject tableObject = table.TableObjects[tableObjectIndex]; Console.WriteLine(table.GroupNames[tableObject.GroupIndex] + ": " + tableObject); } Console.WriteLine("---------"); Console.WriteLine("Ukupna vrijednost: " + table.BestValue); Console.WriteLine("Ukupna cijena: " + table.BestCost); stopwatch.Stop(); Console.WriteLine("Vrijeme izvrsavanja: " + ((float)stopwatch.ElapsedMilliseconds / 1000) + " s"); } catch (Exception) { Console.WriteLine("Datoteka ne postoji ili nije ispravno formatirana!"); } } }
private void PopulateMainGrid() { //reset grid gridMain.EnableHeadersVisualStyles = false; gridMain.Rows.Clear(); gridMain.Columns.Clear(); gridMain.Refresh(); lbBest.Items.Clear(); table.Reset(); gridMain.Columns.Add("C0", ""); int c = 1; foreach (DynamicTableObject tableObject in table.TableObjects) { gridMain.Columns.Add("C" + c, tableObject.Name); string groupName = table.GroupNames[tableObject.GroupIndex]; DataGridViewColumn column = gridMain.Columns[c]; column.ToolTipText = groupName; column.DefaultCellStyle.BackColor = tableObject.GroupIndex % 2 == 1 ? Color.White : Color.LightCyan; column.HeaderCell.Style.BackColor = tableObject.GroupIndex % 2 == 1 ? Color.LightGray : Color.LightSteelBlue; column.SortMode = DataGridViewColumnSortMode.NotSortable; c += 1; } rows = knapsackGroupsCore.SolveProblem(true); for (int currentCost = 0; currentCost <= table.TargetCost; currentCost++) { gridMain.Rows.Add(currentCost.ToString()); for (int objectIndex = 0; objectIndex < table.TableObjects.Count; objectIndex++) { string groupName = table.GroupNames[table.TableObjects[objectIndex].GroupIndex]; gridMain.Rows[currentCost].Cells[objectIndex + 1].Value = table.CostList[objectIndex][currentCost]; gridMain.Rows[currentCost].Cells[objectIndex + 1].ToolTipText = groupName; } } MarkKeyCells(); }