The original question had to be edited. I would say that part of the optimism embedded in each of the works lies in the authors' respective beliefs in social justice. Consider this in the title of each work. The notion of "civil" action, disobedience in order to connect individual action to the social good is an integral part of Thoreau's belief system. Gandhi's march was "historic" because he recognized its intrinsic transformative nature. The "long walk to freedom" is indicative of how Mandela sees the end of apartheid in his vision. For each thinker, the title of the respective works represents optimism at its core.
The call for sacrifice and pain that each thinker suggests is needed clearly makes the case for an optimistic end. Thoreau's advocates that individuals pay attention to their own moral sense of right and wrong, optimistic in how it sees individuals as of one heart and one temperament in pursuit of that which is intrinsically good. Gandhi opens his work with the idea that he might be arrested and imprisoned. Yet, he faces this willingly because he is optimistic that the way of truth and justice is on his side in his Salt March. Gandhi is fairly open about how he feels that the moral notion of right is something that will guide him and in doing so, he neednt worry about anything else, including being arrested. Mandela suffers greatly on his "long walk to freedom." Yet, Mandela understands that the end of apartheid will happen. The vision that he articulates is positive because he fully understands that Apartheid will come to an end. Through his sacrifices and the path others like him have laide down, Mandela's narrative espouses that the commitment to a higher notion of truth, one that might contradict what the law says or is, invariably leads to success. For each of the thinkers, there is optimism when individuals surrender fear when they hear their internal voice or conscience and embrace that which is right.