0

I haven't found a topic similar so I am asking

I am dealing with optimization of mechanism where I found bunch of variables as length, coordinates and some variables are angles between 0 and 2pi. One of the restrictions in is that the angles must be consecutive angles. I am comparing the angles and penalizing the solution when it generate non-consecutives angles but I am missing when the angles are near 2*pi since a valid solution is for example: 6, 6.1, 0.1, 0.2 [rad].

if( T2_1 == min(T2_1,T2_2,T2_3,T2_4,T2_5,T2_6) and 
   T2_2 == min(T2_2,T2_3,T2_4,T2_5,T2_6) and
   T2_3 == min(T2_3,T2_4,T2_5,T2_6) and
   T2_4 == min(T2_4,T2_5,T2_6) and
   T2_5 == min(T2_5,T2_6) ):
      M1 = 1e3 #Penalty 

I used sorted to sort the angles but I need that the script 'understand' that the angles should be consecutives and that way I need the penalty value to add it in the function I am trying to optimize.

I am obtained results but I am limiting the solutions and the minimization is not working

Any advice would be helpful

Thank you for reading

EDIT, to clarify:

This is a optimization problem of creating a four-bar mechanism and the angles represent different position angles of a bar in a four-bar mechanism. I am interested in the relative position of that bar.

By consecutives angles I mean from lowest to highest, but keeping in mind that if the angles start near 2*pi (4th quadrant) and continue enough to change to the 1st quadrant, that is a valid solution. Example: 6.0, 6.1, 0.1, 0.2, 0.3, 0.5 [rad] are consecutives angles, since I am interested in the relative position and in this case 0.1rad actually mean 0.1+2*PI; 0.2rad actually mean 0.2+2*PI; 0.3rad actually mean 0.3+2*PI and 0.5rad actually mean 0.5+2*PI.

If I analyze the values with the operation above then it receive the penalty, since T2_1=6.0 is not the minimum in those values (it may be for angles but not for decimals). Keep in mind that the values are generated randomly from 0 - 2PI and a value of 0.1rad could mean 0.1rad or 0.1+2*Pi rad as in the previous example.

Other cases:

  1. 0.5, 1.0, 1.5, 2.0, 2.2, 2.5 [rad] - Valid (The function above work with this case)
  2. 0.8, 6.1, 0.1, 0.2, 0.3, 0.5 [rad] - Invalid
  3. 0.5, 1.0, 1.5, 6.0, 2.2, 2.5 [rad] - Invalid
  4. 5.5, 6.0, 6.28, 1.0, 1.2, 1.5 [rad] - Valid

While I was writing the edit, I realized the difference between the consecutives angles must have a maximum; if not, every angle could be a consecutive from the other that give a big jump. So, maybe I need to research about the maximum jump the angle can make. However, I need the script to figure out if any angle Xrad mean Xrad or X+2*Pi rad

I am checking one of the answers that pointed me to work with the difference of the angles since I think it may work.

I am sorry for the complicated question, this is a work in progress and I know I need the investigate more, but I am stuck with the consecutive angles near 2*PI and could not find a solution anywhere.

J Vargas
  • 29
  • 3
  • 2
    It's not clear exactly what you're doing with those angles. What does "consecutive angles" mean? But the answers here may be helpful: https://stackoverflow.com/questions/1878907/the-smallest-difference-between-2-angles – PM 2Ring Nov 03 '17 at 09:24
  • Welcome to StackOverflow. It is very important that your questions be clear and understandable. As was already asked, what is the meaning of "consecutive angles"? Please read and follow the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/help/how-to-ask). – Rory Daulton Nov 03 '17 at 10:55
  • "One of the restrictions in is that the angles must be consecutive angles." If `6, 6.1, 0.1, 0.2` is consecutive (sequence passed though 0 radians), then so is `6, 0.1, 6.1, 0.2` (sequence passed though 0 radians twice). Post needs a more resrtrictive definition of consecutive angles to eliminate any sequence. – chux - Reinstate Monica Nov 03 '17 at 17:07
  • I added some clarification. At this point I am trying to do comparison depending on the quadrant and if first value starts between **3*Pi/2** and **2*PI**, then add **2Pi** to the next ones if appear in the 1st quadrant – J Vargas Nov 04 '17 at 11:01

1 Answers1

1

I got a solution that work fine, although there is maybe a simple alternative to achieve the same

T2 = np.array([T2_1,T2_2,T2_3,T2_4,T2_5,T2_6])
M3 = 0
for i in range(6):
    T2 = np.array([T2_1,T2_2,T2_3,T2_4,T2_5,T2_6])
    if 0 <= T2[i] < 3*np.pi/2 and T2[i] == min(T2[i:]):
        M3=0
    elif (3*np.pi/2 <= T2[i] < 2*np.pi) and  all( T2[i:] == np.clip(T2[i:] , 3*np.pi/2, 2*np.pi) ) and (T2[i]== min(T2[i:])):
        M3=0
    elif 3*np.pi/2 <= T2[i] < 2*np.pi and any( T2[i:] == np.clip(T2[i:] , 0, 0.5*np.pi) ):
        k = i+1
        while k <= 5:
            if 0 <= T2[k] < np.pi/2:
                T2[k] = T2[k] + 2*np.pi
            k=k+1
        if T2[i] == min(T2[i:]):
            M3=0
        else:
            M3=1e4
    else:
        M3=1e4 

With this extensive loop, I condition the direction of rotation of the bar, in this case from lower angles to higher.

In the first 3 quadrant is just checking if the value is the minimun versus the next angles in the sequence (array), if it is good it receive no penalty. When the bar is rotated and the angle is in the 4th quadrant, it compare if that angle is the minimum versus the next angles, as long as all angles are in the 4th quarter. When the bar is at an angle in the 4th quadrant an any of the following angles correspond to the 1st quadrant, it adds 2PI (in the while loop) to that angle and then compare.

As conclusion, it receive penalty when the first values are not minimun (normal integer comparision) and when an angle is rear a complete rotation, it adds 2PI to angles in the 1st quadrant.

At the beginning of the for loop I restart the values for T2 since I changed in the while loop

J Vargas
  • 29
  • 3