The answer is operant conditioning.
Operant conditioning is a process in which an organism goes through a learning
process. It is in which behaviors change in order to deal with the consequences
presented. When the rat already learned of the consequences of pressing the red
button, it already figured out a way as to how it can adapt to the given
controlled stimulus and thus preventing it to hit the button again.
Operant conditioning is mainly a result
of learning from the consequences of the stimulus. It is a trait that has
evolved through time with many organisms.