/// <summary> /// Conventional 2-approximation algorithm for 0-1KP /// Result, r holds to outcome that optimal solution, z* is less than or equal to 2r /// i.e. 2r provides upper bound for profit /// </summary> /// <returns></returns> public static int Conventional2Approx_KP(List <Item> items, Knapsack knapsack) { ItemGroup group = new ItemGroup(); List <Item> sortedItems = UtilFunctions.SortByUnitProfitDescending(items); // Fill until we reach critical item foreach (Item item in sortedItems) { if (group.TotalWeight() + item.Weight <= knapsack.Capacity) { group.AddItem(item); } else // we have reached critical item { var criticalItem = item; // 2-approximation solution is one with higher profit value of group or criticalItem if (criticalItem.Value > group.TotalValue()) { group = (ItemGroup) new ItemGroup().Add(criticalItem); } break; } } return(group.TotalValue()); }
/// <summary> /// Uses greedy algorithm to get approximation of highest value for profit /// </summary> /// <returns></returns> public static double LPRelaxedApprox_KP(List <Item> items, Knapsack knapsack) { ItemGroup group = new ItemGroup(); List <Item> sortedItems = UtilFunctions.SortByUnitProfitDescending(items); Item criticalItem = new Item("", 0, 0); double criticalItemFraction = 0; // Fill until we reach critical item foreach (Item item in sortedItems) { if (group.TotalWeight() + item.Weight <= knapsack.Capacity) { group.AddItem(item); } else // we have reached critical item { criticalItem = item; criticalItemFraction = (knapsack.Capacity - group.TotalWeight()) / (double)criticalItem.Weight; break; } } // Add fractional value of next item double maxProfitValue = group.TotalValue() + criticalItem.Value * criticalItemFraction; return(maxProfitValue); }
private void InternalShowItem(int itemId, string itemAssetName, ItemGroup itemGroup, object itemInstance, bool isNewInstance, float duration, object userData) { try { IItem item = m_ItemHelper.CreateItem(itemInstance, itemGroup, userData); if (item == null) { throw new GameFrameworkException("Can not create item in helper."); } ItemInfo itemInfo = ItemInfo.Create(item); m_ItemInfos.Add(itemId, itemInfo); itemInfo.Status = ItemStatus.WillInit; item.OnInit(itemId, itemAssetName, itemGroup, isNewInstance, userData); itemInfo.Status = ItemStatus.Inited; itemGroup.AddItem(item); itemInfo.Status = ItemStatus.WillShow; item.OnShow(userData); itemInfo.Status = ItemStatus.Showed; if (m_ShowItemSuccessEventHandler != null) { ShowItemSuccessEventArgs showItemSuccessEventArgs = ShowItemSuccessEventArgs.Create(item, duration, userData); m_ShowItemSuccessEventHandler(this, showItemSuccessEventArgs); ReferencePool.Release(showItemSuccessEventArgs); } } catch (Exception exception) { if (m_ShowItemFailureEventHandler != null) { ShowItemFailureEventArgs showItemFailureEventArgs = ShowItemFailureEventArgs.Create(itemId, itemAssetName, itemGroup.Name, exception.ToString(), userData); m_ShowItemFailureEventHandler(this, showItemFailureEventArgs); ReferencePool.Release(showItemFailureEventArgs); return; } throw; } }