Dynamic programming for energy control of machine tools in manufacturing