public bool Read(string theFileName) { myKnapsack.Clear(); var aFileStream = File.OpenRead(theFileName); if (aFileStream == null) { return(false); } StreamReader aReader = new StreamReader(aFileStream); string aLine = ""; while ((aLine = aReader.ReadLine()) != null) { KnapsackObject anObj = new KnapsackObject(); int i = 0; anObj.ID = GetNextValue(ref i, aLine); anObj.Price = GetNextValue(ref i, aLine); anObj.Weight = GetNextValue(ref i, aLine); myKnapsack.Add(anObj); } return(true); }
private KnapsackObject GetMax_C(List <KnapsackObject> theKnap) { int aMax = -1; KnapsackObject aRes = new KnapsackObject(); foreach (var anObj in theKnap) { if (anObj.Price > aMax) { aMax = anObj.Price; aRes = anObj; } } return(aRes); }
public List <int> GenerateParent(int theWeightLimit) { List <int> S = new List <int>(); for (int i = 0; i < myKnapsack.Count; i++) { S.Add(0); } int Q = 0; int SumW = 0; KnapsackObject[] aCopy = new KnapsackObject [myKnapsack.Count]; myKnapsack.CopyTo(aCopy); List <KnapsackObject> aCopyList = new List <KnapsackObject>(); aCopyList.AddRange(aCopy); for (int i = 0; i < myKnapsack.Count; i++) { var aCurrentMaxCi = GetMax_C(aCopyList); if ((SumW + aCurrentMaxCi.Weight) <= theWeightLimit) { int aCurrentIndex = myKnapsack.IndexOf(aCurrentMaxCi); aCopyList.Remove(aCurrentMaxCi); Q = Q + aCurrentMaxCi.Price; SumW = SumW + aCurrentMaxCi.Weight; S[aCurrentIndex] = 1; } else { break; } } return(S); }