Software Product Lines (SPLs) capture commonalities and variability of product families, typically represented by means of feature models. The selection of a set of suitable features when a software product is configured is typically made by exploring the space of tread-offs along different attributes of interest, for instance cost and value. In this paper, we present an approach for optimal product configuration by exploiting feature models and grammar guided genetic programming. In particular, we propose a novel encoding of candidate solutions, based on grammar representation of feature models, which ensures that relations imposed in the feature model are respected by the candidate solutions.