This work presents a new optimization-based solution to the Inverse Kinematics Problem (IKP) of legged robots, including a modified Walking Pattern Generator that automatically avoids singularity configurations. The approach uses a numeric constrained problem solved with the DE/rand/1/bin algorithm, where the joint vector is calculated to minimize the position errors, while orientation errors are handled as equality constraints and threshold values are used to set the maximum error for each foot along the trajectories. The optimization model generalizes the IKP for robots with any number of legs and any number of poses within the physically consistent trajectories for the Center of Mass and the feet, considering the dynamics of the robot for a stable walking. The case study was a 12-DOF biped robot, and the resulting joint trajectories were validated by a dynamic simulation, using a Gazebo-ROS platform, where the walking was successfully performed without requiring a feedback control for correcting the torso tilt, showing the solution quality.