Closed Thread
Results 1 to 25 of 25

VBA - Tree visualisation. Possibly the hardest algo logic ever to code

  1. #1
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    I've been stuck here for hours trying to come up with a VBA excel logic for this problem. Think of this as a tree problem, in which the number of branches/nodes is determined by the user and inputted at the start.

    For every node, there is 3 possible outcomes, staying flat, increasing by 1% or decreasing by 1%.

    I thought about it and faced the following problems, was wondering if this makes sense or if its even doable:

    1) The row insertion to build a "tree" is computationally expensive, no way around it. While this is fine for a small number of nodes, a bigger tree would just take hours for a click of the macro! ( i intend to use this up to 11/12 nodes, which takes way too long!)
    - Is it possible to build a solution where macro calculations uses the same "tree" once it has been generated once, rather than to regenerate the tree every single click. i.e. to spilt up the processes, i would run a tree with 12 node at the start. And afterwhich the tweaking of the input values simply generates values that shows itself up in the tree, rather than cleaning the sheets and inserting new rows again.

    2) Let me illustrate whats going on here in the best way i can.
    The initial value of 100, which was used for this test, will be replaced by a formula. Lets call this formula, (A*B*Sqrt(C) + 6/X), where A, B, are constants and X is the initial % move input in the sheet which the tree branches are based off. C is the tricky bit - this is assumed to be constant until a manual intervention comes in.

    Lets for a moment assume a simple tree model, with 5 nodes - giving the following structure. 1, 3, 9, 27, 81 ,243 ... 3^n, where n is the number of nodes.
    Running this formula with an initial input of 2%, whereby X = 2%.
    We get a nice small tree with values at this point of time.
    I am trying to build a more dynamic tree here, whereby i can step in for e.g. on the 2nd value of node 3, adjust the input for C and see how the dependant nodes changes. All without affecting the previously assumed C value on nodes 1 & 2, and nodes not directly affected by the forward path of this particular value.

    So in this picture below, only the blue paths should be affected by the new C value input. Is this something possible? I am so stuck...

    Attachment 530711
    Attached Images Attached Images
    Last edited by songhaegyo; 07-29-2017 at 09:08 PM. Reason: updated with new problem set and issues.

  2. #2
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    bump 10 chars

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,614

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    It's not clear for me what you want to use it for. But let's make fist step. Sheet 1:
    A2: starting value, B2: levels, sheet2 output

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Best Regards,

    Kaper

  4. #4
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Thank you for the very prompt help Kaper. Thats a genius method.

    To answer your question on the purpose: I am trying to model a population birth rate in a city where after every year, T+0 to T+n, where n is the number of nodes. So every +1 year, there could either be 101% of the population, 100% or 99% of the population remaining in the city.

    To add to the complexity, the number of people in the city has to be dynamic at any time. So for example we know the path in the 5th year where we get +1% of population every year. so at 5th year we should have (1.01)^5 people. However, since this city is dynamic, there could be people leaving or entering the city, so manual adjustments to the city population has be to catered for.

    E.G. At 5th year, 5000 left the city to another place. So the spreadsheet has to be dynamic enough to adjust the 5th level node from (1.01)^5 to (1.01)^5 - 5000. And the 6th node carries on from there.... branching out again.

    Not sure if i explained this clearly enough. But this seems to be really hard to code with my amateur vba skills. Is this possible?

    Also, the spreadsheet seems to crash when i simulate 10 levels and above
    Last edited by AliGW; 07-28-2017 at 02:34 AM. Reason: Unnecessary quotation removed.

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    I'll check
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    You mention that the spreadsheet crashes.
    What is the error message or does it just say "not responding" ?

  7. #7
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    it just says not responding... the spinning wheel thingy that never ends.

    My suspicion is that it is looping a new row in every time the loop runs and that is computationally expensive as the nodes grow ^ 3.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    This is not a problem it is a 'feature' with the newer versions of Excel.
    I fell into the same 'trap', but... just let it run and it will finish.

    I modified the file Kaper attached and added a statusbar update and you can see it working, I also noticed that if you add a Doevents in the for next loop it generally keeps on going.
    I made it levels 10 and it works

    I place a button on Sheet 1
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    That looks lovely - the status bar update. Very handy indeed.

    However, what about the 2nd part to the question? Is that something doable?
    I am referring to : "To add to the complexity, the number of people in the city has to be dynamic at any time. So for example we know the path in the 5th year where we get +1% of population every year. so at 5th year we should have (1.01)^5 people. However, since this city is dynamic, there could be people leaving or entering the city, so manual adjustments to the city population has be to catered for.

    E.G. At 5th year, 5000 left the city to another place. So the spreadsheet has to be dynamic enough to adjust the 5th level node from (1.01)^5 to (1.01)^5 - 5000. And the 6th node carries on from there.... branching out again."

    Meaning, is there a way to be able to tweak one of the level input at any given time, to manually change the input, say, of a certain point on the 3th level.And to calculate the other 4th, 5th, 6th, etc based on that dynamic manual change... Not sure if this is clear?
    Last edited by AliGW; 07-27-2017 at 07:30 AM. Reason: Unnecessary quotation removed.

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    I just looked at the fact if there was an error, and that is not so.
    About the modifications, well I guess you're the one with the 'statistical' knowledge, I'm just somebody that can code
    I guess you're the best one to modify entry points and use these a reference?

    I you can add some sample input or what you think you need I'll see if I understand it.

  11. #11
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Would having a enterable value in a cell, that could be referenced at the end/start point of a nodes calculation, before continuing be what you had in mind.
    ie
    node1 calculate... end point reference cell empty
    node2 calculate... end point reference cell empty
    node3 calculate... end point reference cell "-5000" to account for population change at that point...or am I mis reading???

    Year one variance "0"
    Year two variance "0"
    Year 3 variance "-5000" etc

    each year changeable

  12. #12
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,614

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    May be a good idea would be to have not values but formulas. Then you could manually change selected node/nodes and all dependant nodes would change accordingly.

    See sample file.

    @Keebellah - Glad to see you joined thread giving some valuable input. I changed a bit the statusbar message.
    Now it is more linear, and do not refer to cells (it's time consuming, and the code is anyway already quite slow, because it uses rows inserting).

    edit: and again @songhaegyo - have you noticed change in post #9 and a comment from moderator:
    Last edited by AliGW; Today at 01:30 PM. Reason: Unnecessary quotation removed.
    Please follow AliGW advice (and our rule 12 of https://www.excelforum.com/forum-rul...rum-rules.html )
    By the way - this part of thread title is also a bad idea: "Possibly the hardest algo logic ever to code" forum search is based mostly on thread titles and while mentioning in a title "tree visualisation" is a good idea, comments on possible complication are not. While it's not written explicite in Point 1 of the rules, it fits the idea behind.
    Attached Files Attached Files
    Last edited by Kaper; 07-27-2017 at 11:14 AM.

  13. #13
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Thanks guys for the valuable input and ideas.

    I thought about it and faced the following problems, was wondering if this makes sense or if its even doable:

    1) The row insertion to build a "tree" is computationally expensive, no way around it. While this is fine for a small number of nodes, a bigger tree would just take hours for a click of the macro! ( i intend to use this up to 11/12 nodes, which takes way too long!)
    - Is it possible to build a solution where macro calculations uses the same "tree" once it has been generated once, rather than to regenerate the tree every single click. i.e. to spilt up the processes, i would run a tree with 12 node at the start. And afterwhich the tweaking of the input values simply generates values that shows itself up in the tree, rather than cleaning the sheets and inserting new rows again.

    2) Good catch on the formula portion @Kaper. Perfectly agree with you. Let me illustrate whats going on here in the best way i can.
    The initial value of 100, which was used for this test, will be replaced by a formula. Lets call this formula, (A*B*Sqrt(C) + 6/X), where A, B, are constants and X is the initial % move input in the sheet which the tree branches are based off. C is the tricky bit - this is assumed to be constant until a manual intervention comes in.

    Lets for a moment assume a simple tree model, with 5 nodes - giving the following structure. 1, 3, 9, 27, 81 ,243 ... 3^n, where n is the number of nodes.
    Running this formula with an initial input of 2%, whereby X = 2%.
    We get a nice small tree with values at this point of time.
    I am trying to build a more dynamic tree here, whereby i can step in for e.g. on the 2nd value of node 3, adjust the input for C and see how the dependant nodes changes. All without affecting the previously assumed C value on nodes 1 & 2, and nodes not directly affected by the forward path of this particular value.

    So in this picture below, only the blue paths should be affected by the new C value input. Is this something possible? I am so stuck...
    Node.JPG

  14. #14
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    I've seen and read your PM but this seems way over my head, let me read it carefully and see if I can come up with something.
    I do have a way that I think will wok to halt the calculations and resume at another moment but am not sure it I can implement here

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,621

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Note to all participants: please keep the entire conversation here within the thread.

    Remember, this is a public forum and all information needs remain public. This thread will be of little use to anyone in the future wishing to use the methods discussed here if crucial bits of information are missing.

    Thank you for your co-operation and understanding.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  16. #16
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    no worries AliGW, PM was simply to tell him that bits of information were uploaded to the post!

  17. #17
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,621

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    You shouldn't really do that - just bump the thread with the information, as you did. Helpers will respond when they are ready, not 'on demand'.

  18. #18
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    True, that is why I answered here an not in the PM

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,621

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Yes, I realise that - thank you.

  20. #20
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    bump any help please?

  21. #21
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,621

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Please be patient. This is not a paid 'service': people help here on a voluntary basis and of their own goodwill, and are located in many different time zones.

  22. #22
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Just what I was about to say too ...

  23. #23
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    This seemed a litte faster :
    Please Login or Register  to view this content.
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  24. #24
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    Quote Originally Posted by karedog View Post
    This seemed a litte faster :
    Please Login or Register  to view this content.

    Wow. Your code seems to parse the data a lot faster. However, do you think that the 2nd part of my question is solvable?

  25. #25
    Registered User
    Join Date
    06-20-2017
    Location
    london
    MS-Off Ver
    2010
    Posts
    38

    Re: VBA - Tree visualisation. Possibly the hardest algo logic ever to code

    To Admin:

    I have decided to upgrade this post to a paid version to get response quickly. Please remove this thread.

    https://www.excelforum.com/showthrea...07#post4710007

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pipe visualisation
    By cooksteve09 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 07-01-2017, 05:03 AM
  2. Data Visualisation
    By KING9 in forum Excel General
    Replies: 0
    Last Post: 12-11-2013, 12:03 PM
  3. using excell for material selection possibly a logic problem
    By pault125 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-10-2013, 12:17 PM
  4. IF/AND Formula Logic Tree
    By quibilty in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-18-2013, 08:43 PM
  5. [SOLVED] Logic Tree to IF/AND Formula Conversion
    By quibilty in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-19-2013, 11:00 AM
  6. Code for email alerts from excel isn't working, wrong code possibly?
    By jessthorogood in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2012, 01:45 AM
  7. Logic Tree, MS help = useless
    By roffler in forum Excel Formulas & Functions
    Replies: 63
    Last Post: 09-06-2005, 07:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1