A public referendum on legalising cannabis for personal use will be held by the 2020 election as part of the agreement between the Green and Labour parties.