Ejemplo n.º 1
0
        private static bool Decompose_by_branching_and_bounding(int objectiveValue, int capacity, SortedItem[] sortedItems, int i, Stack<int> selectedItemIndexes)
        {
            for (var k = i + 1; k < sortedItems.Length; k++)
            {
                nodes_visited++;
                if (nodes_visited % 10000 == 0) File.AppendAllText("hybridsolver.log", string.Format("B&B: {0}, {1}\r\n", nodes_visited, DateTime.Now));

                var rest_value = objectiveValue - sortedItems[k].Value;
                var rest_capacity = capacity - sortedItems[k].Weight;

                if (rest_value < 0) continue; // current val too large; try next one which will be smaller
                if (rest_capacity < 0) continue; // no more capacity left in knapsack
                if (rest_value > sortedItems[k].MaxTotal) return false; // the rest cannot possibly be covered by the remaining items

                selectedItemIndexes.Push(sortedItems[k].Index); // remember candidate value index

                if (rest_value <= _errorMargin && rest_capacity >= 0) return true; // goal reached!

                // continue with next value
                if (Decompose_by_branching_and_bounding(rest_value, rest_capacity, sortedItems, k, selectedItemIndexes))
                    return true;

                selectedItemIndexes.Pop(); // this candidate did not help
            }
            return false;
        }
Ejemplo n.º 2
0
 private void ButtonAdd_Click(object sender, EventArgs e)
 {
     if (int.TryParse(TextboxAdd.Text, out int value))
     {
         var item = new SortedItem(value, items.Count);
         items.Add(item);
     }
     RefreshItems();
     TextboxAdd.Text = "";
 }
Ejemplo n.º 3
0
        private bool RemoveLowestFee(Transaction tx)
        {
            SortedItem min = _sorted_items.Min;

            if (min != null && min.tx.FeePrice < tx.FeePrice)
            {
                TryRemove(min.tx.Hash, out _);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
 private void AddListButton_Click(object sender, EventArgs e)
 {
     if (int.TryParse(TextboxAddList.Text, out int value))
     {
         var rnd = new Random();
         for (int i = 0; i < value; i++)
         {
             var item = new SortedItem(rnd.Next(100), items.Count);
             items.Add(item);
         }
     }
     RefreshItems();
     TextboxAddList.Text = "";
 }
Ejemplo n.º 5
0
            private int _findIndex(SortedItem item)
            {
                var index = -1;

                for (var i = 0; i < _items.Count; i++)
                {
                    if (ReferenceEquals(_items[i], item))
                    {
                        index = i;
                        break;
                    }
                }
                return(index);
            }
Ejemplo n.º 6
0
        private void _add(int viewModelIndex, bool isRaiseCountChanged)
        {
            var        viewModel = _originalCollection[viewModelIndex];
            SortedItem item;

            if (viewModelIndex < _items.Count)
            {
                item = _items[viewModelIndex];
            }
            else
            {
                item = new SortedItem();
                _items.Add(item);
                item.Index = _items.Count - 1;
            }

            item.ViewModel = viewModel;
            _cacheViewModelAndItems.Add(viewModel, item);

            _incrementItemsCursorCount(isRaiseCountChanged);
        }
Ejemplo n.º 7
0
 public SortedItemEventArgs(SortedItem sortedItem)
 {
     this._sortedItem = sortedItem;
 }
Ejemplo n.º 8
0
        /// <summary>Process a verify label request for a carton.</summary>
        /// <param name="cartonID">The carton's 24 position unique [Argix] identifier.</param>
        /// <param name="inputs">A array of barcode scans including the Argix outbound label barcode.</param>
        /// <param name="verifyCode">A verify code for the carton (Use PandaService verify constants: PandaService.VERIFY_PASS, PandaService.VERIFY_FAIL, PandaService.VERIFY_NOREAD).</param>
        /// <exceptions cref="ApplicationException">Thrown for unexpected errors.</exceptions>
        /// <returns>Returns a verify flag for the carton (PandaService.VERIFY_YES, PandaService.VERIFY_NO).</returns>
        public string ProcessVerifyLabelRequest(string cartonID, string[] inputs, string verifyCode)
        {
            //Verify the requested carton
            string verifyFlag        = VERIFY_NO;
            ApplicationException aex = null;

            try {
                //Begin processing a new message request
                string _inputs = "";
                for (int i = 0; i < inputs.Length; i++)
                {
                    _inputs += (i + 1).ToString() + "=" + inputs[i] + "; ";
                }
                ArgixTrace.WriteLine(new TraceMessage("VERIFY LABEL REQUEST: ID=" + cartonID + "; " + _inputs + "verify code=" + verifyCode, AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));

                //Validate
                if (!(cartonID.Length == 14 || cartonID.Length == 24))
                {
                    throw new ApplicationException("Invalid cartonID length (" + cartonID.Length.ToString() + "; must be 14 0r 24)...");
                }

                //Find the carton
                SortedItem item = this.mOperator.GetSortedItem(cartonID.Length == 24 ? cartonID.Substring(10, 13) : cartonID.Substring(0, 13));
                if (item != null)
                {
                    //Item found
                    if (verifyCode == VERIFY_PASS)
                    {
                        //Pass: Save carton
                        try {
                            this.mOperator.StoreSortedItem(item);
                            verifyFlag = VERIFY_YES;
                            ArgixTrace.WriteLine(new TraceMessage("Carton save succeeded...", AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));
                        }
                        catch (Exception ex) {
                            verifyFlag = VERIFY_NO;
                            aex        = new ApplicationException("Carton save failed...-->" + ex.Message, ex);
                            ArgixTrace.WriteLine(new TraceMessage(aex.Message, AppLib.PRODUCTNAME, LogLevel.Error, "PandaSvc"));
                        }
                    }
                    else if (verifyCode == VERIFY_FAIL || verifyCode == VERIFY_NOREAD)
                    {
                        //Fail, No Read: Cancel carton
                        verifyFlag = VERIFY_NO;
                        this.mOperator.CancelSortedItem(item.LabelSeqNumber);
                        ArgixTrace.WriteLine(new TraceMessage("Verify flag was fail or no read; carton cancelled...", AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));
                    }
                    else
                    {
                        //Fail, No Read: Cancel carton
                        verifyFlag = VERIFY_NO;
                        this.mOperator.CancelSortedItem(item.LabelSeqNumber);
                        ArgixTrace.WriteLine(new TraceMessage("Verify flag was unknown; carton cancelled...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                    }
                }
                else
                {
                    //Carton not found
                    verifyFlag = VERIFY_NO;
                    ArgixTrace.WriteLine(new TraceMessage("Carton not found...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                }
            }
            catch (ApplicationException ex) {
                verifyFlag = VERIFY_NO;
                aex        = ex;
                ArgixTrace.WriteLine(new TraceMessage(ex.Message, AppLib.PRODUCTNAME, LogLevel.Error, "PandaSvc"));
            }
            catch (Exception ex) {
                verifyFlag = VERIFY_NO;
                aex        = new ApplicationException("An unexpected error occurred while processing verify label request-->" + ex.Message + "...", ex);
                ArgixTrace.WriteLine(new TraceMessage("An unexpected error occurred while processing verify label request-->" + ex.Message + "...", AppLib.PRODUCTNAME, LogLevel.Error, "PandaSvc"));
            }
            finally {
                ArgixTrace.WriteLine(new TraceMessage("VERIFY LABEL RESPONSE: " + cartonID + "; " + verifyFlag, AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));
                if (this.VerifyLabelRequestComplete != null)
                {
                    this.VerifyLabelRequestComplete(this, new PandaVerifyLabelEventArgs(cartonID, verifyFlag, aex));
                }
            }
            return(verifyFlag);
        }
Ejemplo n.º 9
0
        /// <summary>Process a label data request for a scanned carton.</summary>
        /// <param name="inputs">Carton barcode data.</param>
        /// <param name="weight">Carton weight.</param>
        /// <exceptions cref="ApplicationException">Thrown for unexpected errors.</exceptions>
        /// <returns>An instance of Tsort.PandA.PandaDS containing the label data response (in the Tsort.PandA.PandaDS.PandaTable).</returns>
        public PandaDS ProcessLabelDataRequest(string[] inputs, decimal weight)
        {
            //Create a new carton
            string cartonID = "", zpl = "";
            int    statusCode        = STATUS_NONE;
            ApplicationException aex = null;
            PandaDS ds = new PandaDS();

            //Validate for non-carton requests (scanner tripped)
            bool   hasInputData = false;
            string _inputs      = "";

            for (int i = 0; i < inputs.Length; i++)
            {
                if (inputs[i].Trim().Length > 0)
                {
                    hasInputData = true;
                }
                _inputs += (i + 1).ToString() + "=" + inputs[i] + "; ";
            }
            ArgixTrace.WriteLine(new TraceMessage("LABEL DATA REQUEST: " + _inputs + "weight=" + weight.ToString(), AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));
            if (!hasInputData && weight == 0)
            {
                ArgixTrace.WriteLine(new TraceMessage("LABEL DATA RESPONSE: no inputs; no weight; processing aborted...", AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));
                return(ds);
            }
            try {
                //Begin processing a new message request
                if (this.LabelDataRequest != null)
                {
                    this.LabelDataRequest(null, EventArgs.Empty);
                }
                try {
                    //Create a default carton identifier
                    DateTime dt  = DateTime.Now;
                    int      sec = ((3600 * dt.Hour) + (60 * dt.Minute) + dt.Second);
                    cartonID = dt.ToString("yyyy").Substring(3, 1) + dt.ToString("MM") + dt.ToString("dd") + sec.ToString("00000") + "0000";

                    //Get an outbound label for this carton
                    SortedItem sortedItem = this.mOperator.ProcessInputs(inputs, weight);
                    if (sortedItem != null)
                    {
                        try {
                            cartonID = sortedItem.LabelNumber;
                            zpl      = sortedItem.LabelFormat;
                            sortedItem.ThrowException();
                            statusCode = STATUS_CARTON_OK;
                            ArgixTrace.WriteLine(new TraceMessage("Carton processing successful [label data]...", AppLib.PRODUCTNAME, LogLevel.Debug, "PandaSvc"));
                        }
                        catch (Tsort.InboundLabelException ex) {
                            //Test for various inbound label exceptions
                            bool allInvalid = true;
                            for (int i = 0; i < inputs.Length; i++)
                            {
                                if (inputs[i].Trim().Length > 0)
                                {
                                    allInvalid = false; break;
                                }
                            }
                            if (allInvalid)
                            {
                                statusCode = STATUS_SCANERROR_NODATA;
                                ArgixTrace.WriteLine(new TraceMessage("Message data validation failed [no data from scanner]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                            }
                            allInvalid = true;
                            for (int i = 0; i < inputs.Length; i++)
                            {
                                if (inputs[i].Replace("?", "").Trim().Length > 0)
                                {
                                    allInvalid = false; break;
                                }
                            }
                            if (allInvalid && statusCode == STATUS_NONE)
                            {
                                statusCode = STATUS_SCANERROR_NOREAD;
                                ArgixTrace.WriteLine(new TraceMessage("Message data validation failed [no read: ?]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                            }
                            allInvalid = true;
                            for (int i = 0; i < inputs.Length; i++)
                            {
                                if (inputs[i].Replace("#", "").Trim().Length > 0)
                                {
                                    allInvalid = false; break;
                                }
                            }
                            if (allInvalid && statusCode == STATUS_NONE)
                            {
                                statusCode = STATUS_SCANERROR_CONFLICT;
                                ArgixTrace.WriteLine(new TraceMessage("Message data validation failed [label conflict: #]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                            }
                            if (statusCode == STATUS_NONE)
                            {
                                statusCode = STATUS_SCANERROR_LABELFAILED;
                                ArgixTrace.WriteLine(new TraceMessage("Message data validation failed [inbound label exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                            }
                        }
                        catch (Tsort.ZeroWeightException ex) {
                            statusCode = STATUS_SCANERROR_WEIGHTBAD;
                            ArgixTrace.WriteLine(new TraceMessage("Message data validation failed [zero weight exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                        }
                        catch (Tsort.OverWeightException ex) {
                            statusCode = STATUS_SCANERROR_WEIGHTBAD;
                            ArgixTrace.WriteLine(new TraceMessage("Message data validation failed [over weight exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                        }
                        catch (Tsort.DestinationRoutingException ex) {
                            statusCode = STATUS_SCANERROR_LABELFAILED;
                            ArgixTrace.WriteLine(new TraceMessage("Sorted item processing failed [destination/routing exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                        }
                        catch (Exception ex) {
                            statusCode = STATUS_SCANERROR_LABELFAILED;
                            ArgixTrace.WriteLine(new TraceMessage("Sorted item processing failed [unexpected exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                        }
                        zpl = zpl.Replace(Tsort.Labels.TokenLibrary.STATUSCODE, statusCode.ToString());
                    }
                    else
                    {
                        aex        = new ApplicationException("Carton processing failed [no sorted item]...");
                        statusCode = STATUS_SCANERROR_LABELFAILED;
                        zpl        = createErrorLabelFormat(STATUS_SCANERROR_LABELFAILED, aex);
                        ArgixTrace.WriteLine(new TraceMessage(aex.Message, AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                    }
                }
                catch (Exception ex) {
                    aex        = new ApplicationException("Carton processing failed [sorted item processing threw an exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", ex);
                    statusCode = STATUS_SCANERROR_LABELFAILED;
                    zpl        = createErrorLabelFormat(STATUS_SCANERROR_LABELFAILED, ex);
                    ArgixTrace.WriteLine(new TraceMessage(aex.Message, AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
                }
                ds.PandaTable.AddPandaTableRow(cartonID, statusCode, zpl);
                ds.AcceptChanges();
            }
            catch (Exception ex) {
                aex        = new ApplicationException("Carton processing failed [unexpected exception]-->" + ex.Message + (ex.InnerException != null?"-->" + ex.InnerException.Message:"") + "...", ex);
                statusCode = STATUS_ERROR_UNKNOWN;
                zpl        = createErrorLabelFormat(STATUS_ERROR_UNKNOWN, ex);
                ds.PandaTable.AddPandaTableRow(cartonID, statusCode, zpl);
                ds.AcceptChanges();
                ArgixTrace.WriteLine(new TraceMessage(aex.Message, AppLib.PRODUCTNAME, LogLevel.Warning, "PandaSvc"));
            }
            finally {
                ArgixTrace.WriteLine(new TraceMessage("LABEL DATA RESPONSE: " + cartonID + "; " + statusCode.ToString(), AppLib.PRODUCTNAME, LogLevel.Information, "PandaSvc"));
                if (this.LabelDataRequestComplete != null)
                {
                    PandaDS _ds = new PandaDS();
                    _ds.CartonTable.AddCartonTableRow(cartonID, (inputs.Length > 0?inputs[0]:""), (inputs.Length > 1?inputs[1]:""), (inputs.Length > 2?inputs[2]:""), (inputs.Length > 3?inputs[3]:""), (inputs.Length > 4?inputs[4]:""), (inputs.Length > 5?inputs[5]:""), weight, statusCode, zpl, "", (aex != null?aex.Message:""), "");
                    this.LabelDataRequestComplete(this, new PandaLabelDataEventArgs(_ds.CartonTable[0]));
                }
            }
            return(ds);
        }