A Learning-based Algorithm for Optimal MAC Parameters Setting in IEEE 802.15.4 Wireless Sensor Networks