A neural network model of when to retrieve and encode episodic memories