Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Best Possible Combination

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Best Possible Combination

    The scenario is:
    A table has a numeric field which has values ranging from .01 to 10,000. there is no rhyme or reasons to these values.
    A user inputs a value, say in a variable.
    How to find the combination of different values from the table, the sum of which is equal to or closest to but not more than the value entered by the user?
    There is another wrinkle to this: the optimal solution is one that finds the least number of values that make up the desired sum.
    I came up with a concept for a function to do that (I will share the concept shortly) but something in the back of my mind tells me I must be missing or forgetting some algebraic concept that optimizes this solution.

    Any and all ideas are welcome and appreciated.
    Thanks.

    #2
    Re: Best Possible Combination

    Looking for pairs only? Or can the sum be the result of 3 or more values?

    Comment


      #3
      Re: Best Possible Combination

      Any number of values.

      Comment


        #4
        Re: Best Possible Combination

        How many records in the table?
        Mike W
        __________________________
        "I rebel in at least small things to express to the world that I have not completely surrendered"

        Comment


          #5
          Re: Best Possible Combination

          Hundreds. Could be more.

          Comment


            #6
            Re: Best Possible Combination

            ideas - eliminate impossibilities

            1. if the sum of 2 or more numbers is less than a given number ,then you should only look at numbers in the table that are less than the given number. ( I'm thinking zeroes aren't in this table..)

            2. then I'd look at the largest numbers first to find another number that add to the given number - then start looking for the more than 2 numbers summed..

            3. in looking for the second number, look at only the numbers that are equal to or less than the difference of the 1st number and the given number.
            Al Buchholz
            Bookwood Systems, LTD
            Weekly QReportBuilder Webinars Thursday 1 pm CST

            Occam's Razor - KISS
            Normalize till it hurts - De-normalize till it works.
            Advice offered and questions asked in the spirit of learning how to fish is better than someone giving you a fish.
            When we triage a problem it is much easier to read sample systems than to read a mind.
            "Make it as simple as possible, but not simpler."
            Albert Einstein

            http://www.iadn.com/images/media/iadn_member.png

            Comment


              #7
              Re: Best Possible Combination

              Al:
              Pretty much what I am thinking:
              1-Sorted the table in descending order eliminating values > the user's number
              2-Subtract the top value from the user's
              3-Re-query/sort the table for a number equal the difference.
              4-Log the difference and tag the loop
              5-Re-run
              6-Sort the log for the least difference
              That's my operating concept.

              Comment


                #8
                Re: Best Possible Combination

                so far I have this to build a test table ("nums"), if it helps anyone

                Code:
                 dim vcn as c
                dim vcn1 as C
                 dim vm as n
                
                dim t as p
                t=table.open("nums")
                'Turn on the hourglass cursor.
                hourglass_cursor(.t.)
                for i = 1 to 1000
                	NEWNUM:
                	vm=val(right(alltrim(str(rand()*1000,5)),1))
                	vcn1=left(alltrim(str(rand()*100000000,9)),vm)
                	if len(vcn1)>6
                		GOTO NEWNUM
                	else if val(vcn1)=0
                		GOTO NEWNUM
                	end if
                
                	if len(vcn1)<3
                		vcn="."+padl(vcn1,2,"0")
                	else
                		vcn=substr(vcn1,1,len(vcn1)-2)+"."+right(vcn1,2)
                	end if
                	'msgbox(vcn1,vcn+crlf()+str(vm))
                	
                	t.enter_begin()
                	t.t_val=val(vcn)
                	t.enter_end(.t.)
                next
                
                hourglass_cursor(.f.)
                Mike W
                __________________________
                "I rebel in at least small things to express to the world that I have not completely surrendered"

                Comment


                  #9
                  Re: Best Possible Combination

                  I thought I was missing some algebraic/math concept. Evidently not.
                  With a quick search I found out that this falls under what is know in math as "NP-complete" (nondeterministic polynomial) i.e. a math puzzle that cannot be solved with any logical algorithm, i.e. one in which you cannot guess what the right solution is hence you cannot design one. Therefore, when you try to write a routine to solve it, you could only use brute force. So the best I could do is to reduce the brutality.

                  Comment


                    #10
                    Re: Best Possible Combination

                    Have a look at the Linear Programming discussion for Excel.
                    This is probably what you want;

                    Maximize: cTx
                    Subject to: Ax ≤ b.

                    The theory gets a bit deep but essentially it's all about optimisation.
                    See our Hybrid Option here;
                    https://hybridapps.example-software.com/


                    Apologies to anyone I haven't managed to upset yet.
                    You are held in a queue and I will get to you soon.

                    Comment


                      #11
                      Re: Best Possible Combination

                      Thanks Ted.
                      I have played with Excel Solver in the past, but to my knowledge there is no equivalent in alpha.
                      The odd thing is, 30 yrs ago or so when I started playing with spreadsheets.. there was a program called "What is Best", yes back on those stone ages. I used it for similar purposes. There are tons of optimization software out there, but in alpha you have to design your own scheme. It would be a nice add-on.

                      Comment


                        #12
                        Re: Best Possible Combination

                        I agree, but it's likely to be of limited use I suspect.
                        Is there not a BASIC function in VB you could swipe and use?
                        See our Hybrid Option here;
                        https://hybridapps.example-software.com/


                        Apologies to anyone I haven't managed to upset yet.
                        You are held in a queue and I will get to you soon.

                        Comment


                          #13
                          Re: Best Possible Combination

                          So Gabriel,
                          Have you got it ground out yet. I'm still working on it. Pretty interesting and complex puzzle.
                          Mike W
                          __________________________
                          "I rebel in at least small things to express to the world that I have not completely surrendered"

                          Comment


                            #14
                            Re: Best Possible Combination

                            Ted:
                            I could do this easily in Excel with or without VB. I wanted to do this in alpha. Yes, an add-on would be of limited use, but imagine the benefits of optimization in so many industries/businesses.

                            Mike:
                            I haven't spent a whole lot of time on development, only a concept and stopped in hope of finding that magical formula which so far I haven't found. Linear programming that Ted refers to is about as close as you can get but there is some differences. In LP, most of the time, you are not given a set of values but rather a set of constraints and an objective from which to determine a set of values, i.e resolve the variables based on the given constraints and objective. In that case you find the intersecting point at the feasibility region with one shot at the Bow. However, if you were given a set of values to choose from and you were to apply LP, at this point you will need to do several permutations but by applying LP you reduce the number of these permutations through one of several means developed by several Math geniuses over the years the most common of which is know as "Simplx".

                            There is a similarity though between LP & the scenario at hand: The objective of LP is to reduce the number of permutations which is what I intuitively did in my concept.
                            If you have hundreds, let alone thousands of records, you will be doing calculations of all possible combinations till Kingdom come. My concept First removes the records that cannot possibly work secondly by starting with the larger values first it reduces the number of the combinations and thirdly by testing the difference between the user's value and the combined value and once you get to zero you will quit, no need for any further calculations since you achieved the 2 goals (1-finding the combo closest to the user's 2-with the least number of values) thus reducing the permutations by a fraction which could be a small fraction or a major one. LP in this case is still a brute force in which brute is considerably reduced.

                            Comment


                              #15
                              Re: Best Possible Combination

                              This may also be a situation where the data should be loaded (after filtering) to an array or a collection so that processing can be done in memory instead of reading and re-reading rows from a table.
                              Al Buchholz
                              Bookwood Systems, LTD
                              Weekly QReportBuilder Webinars Thursday 1 pm CST

                              Occam's Razor - KISS
                              Normalize till it hurts - De-normalize till it works.
                              Advice offered and questions asked in the spirit of learning how to fish is better than someone giving you a fish.
                              When we triage a problem it is much easier to read sample systems than to read a mind.
                              "Make it as simple as possible, but not simpler."
                              Albert Einstein

                              http://www.iadn.com/images/media/iadn_member.png

                              Comment

                              Working...
                              X