Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Selecting the best array of values after running through loops

function loops arrays

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 mastermind1

mastermind1

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 15 November 2014 - 01:59 PM

def func(number1,number2,number3):
    
    add_div_1 = (number1 + number2 + number3) / 3
    add_div_2 = (number1 - number2 - number3)/ 3
    add_div_3 = (number1 + number2 - number3) / 3
    add_div_4 = (number1 - number2 + number3) / 3
    
    cond1 = abs(add_div_1/add_div_3)
    cond2 = abs(add_div_2/add_div_4)
    
    opt = cond1*cond2
    return opt

one_num = func(1.5,2.5,3.5)
print one_num
array_n = [10,20,30]

b = 0
bt = 1   # step

while b < 10:
    
    b = b + bt
    
    for i in array_n:
        MAX = 31
        r = rand(3)
        r = abs(2*MAX*(r-0.5))
        
        num1 = array_n[0]
        num2 = array_n[1]
        num3 = array_n[2]
        
        n1,n2,n3 = r
        
        new_num1 = num1 + n1
        new_num2 = num2 + n2
        new_num3 = num3 + n3
        
        newnums = [new_num1,new_num2,new_num3]
        print newnums
        
one_num = func(new_num1,new_num2,new_num3)
        
if one_num < 27.0:
    
    print "The number is lower than 27.0 .","The number is:", one_num
    print "The number values are:", newnums
    
else:
    
    print "The number is larger than 27.0 .","The number is:", one_num
    print "The number values are:", array_n

The code above works with no problems, but does not do the job I want it to do. And, I am not sure how to do that. You will need to run the code in order to understand what I am about to explain.

 

At the end, after you see the thirty arrays of values, you will see either the two statements with "The number is lower..." or "The number is larger...". For either pair of statements, the values are selected from the last array of those thirty arrays. Instead, I want the program to choose the best array that gives me the "one_num" as low as possible from 27. Then, when I run the program again, the program will create all new arrays, and from that it should choose the best array that gives me the "one_num" as low as possible from 27. But, if the "one_num" in the 2nd second run or any in other run after its previous is higher than its previous "one_num", then keep the good value (lower one_num).

 

I am sorry, if that is confusing, but it is hard to explain programming.


Edited by mastermind1, 15 November 2014 - 02:02 PM.


#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 15 November 2014 - 03:19 PM

    r = rand(3)
NameError: name 'rand' is not defined

Compute one_num each time and remember lowest value for given input (as well input)?

 

edit: should be moved to Python forum


Edited by Flying Dutchman, 15 November 2014 - 03:20 PM.

The roots of education are bitter, but the fruit is sweet.


#3 mastermind1

mastermind1

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 15 November 2014 - 03:58 PM

    r = rand(3)
NameError: name 'rand' is not defined

Compute one_num each time and remember lowest value for given input (as well input)?

 

edit: should be moved to Python forum

 

It is not giving me an error. Yes to your question. We have to keep the values in the array that gave us the lowest one_num. If the one_num has improved then, then keep those new values (replace the old values). If not, then keep the the old values. I have tried every possible way I know. The only way to help is to give me the best hints possible.


def func(number1,number2,number3):
    
    add_div_1 = (number1 + number2 + number3) / 3
    add_div_2 = (number1 - number2 - number3)/ 3
    add_div_3 = (number1 + number2 - number3) / 3
    add_div_4 = (number1 - number2 + number3) / 3
    
    cond1 = abs(add_div_1/add_div_3)
    cond2 = abs(add_div_2/add_div_4)
    
    opt = cond1*cond2
    return opt

one_num = func(1.5,2.5,3.5)
print one_num
array_n = [10,20,30]

b = 0
bt = 1   # step

while b < 10:
    
    b = b + bt
    
    for i in array_n:
        MAX = 31
        r = rand(3)
        r = abs(2*MAX*(r-0.5))
        
        num1 = array_n[0]
        num2 = array_n[1]
        num3 = array_n[2]
        
        n1,n2,n3 = r
        
        new_num1 = num1 + n1
        new_num2 = num2 + n2
        new_num3 = num3 + n3
        
        newnums = [new_num1,new_num2,new_num3]
        print newnums
        
one_num = func(new_num1,new_num2,new_num3)
        
if one_num < 27.0:
    
    print "The number is lower than 27.0 .","The number is:", one_num
    print "The number values are:", newnums
    
else:
    
    print "The number is larger than 27.0 .","The number is:", one_num
    print "The number values are:", array_n

The code above works with no problems, but does not do the job I want it to do. And, I am not sure how to do that. You will need to run the code in order to understand what I am about to explain.

 

At the end, after you see the thirty arrays of values, you will see either the two statements with "The number is lower..." or "The number is larger...". For either pair of statements, the values are selected from the last array of those thirty arrays. Instead, I want the program to choose the best array that gives me the "one_num" as low as possible from 27. Then, when I run the program again, the program will create all new arrays, and from that it should choose the best array that gives me the "one_num" as low as possible from 27. But, if the "one_num" in the 2nd second run or any in other run after its previous is higher than its previous "one_num", then keep the good value (lower one_num).

 

We have to keep the values in the array that gave us the lowest one_num. If the one_num has improved then, then keep those new values (replace the old values). If not, then keep the the old values.

 

I am sorry, if that is confusing, but it is hard to explain programming.

 

 


Edited by mastermind1, 15 November 2014 - 03:57 PM.


#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 19 November 2014 - 05:20 AM

Indentation, you're computing one_num once, after the while loop.


The roots of education are bitter, but the fruit is sweet.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download