How to use a custom objective function? The following is what I'm trying to accomplish:

I have a charity contact data set. Each contact has features such as sex, age and so on, which we define as X. 

Now we are doing a solicitation campaign: we send mails to certain contacts, with a cost of 5 dollars per person, and they can respond by making a donation of however much money they wish. For the training set, we have the persons that have received the mails, and Y_amount is the amount they donated (0 if they didn't do so). People who didn't receive the mails couldn't make unsolicited donations. 

Our objective is to maximize the amount raised by the campaign after deducting the cost. However, here comes the tricky part: how could I write a loss function out of this problem and implement it within python? 

My original thought was to train a neural network that gives the confidence of generating net revenues from each person Y_confidence on X using the following loss function (the money we could raise if I follow the rule):


Y_decision = 1 if Y_confidence > 0.5
Y_decision = 0 if Y_confidence &lt; 0.5
Loss = -SUM(Y_decision * (Y_amount - 5))


Do you know how to implement it or do you have a better idea of solving this problem?
You can break the problem into two separate problems:


How much is a person likely to donate? That is a regression problem.
How to maximize the amount raised by the campaign after deducting the cost? That is contraine optimization problem. 


Each separate problem can be solved with existing tools without creating a custom loss function.