How to precisely decide Learning rate and epoch in these type questions..?

I was working on leetcode problem no 1515 https://leetcode.com/problems/best-position-for-a-service-centre/ ,I decided to solve it using gradient descent can somene help me out coz this is predecting the answers but not accurately.How can I set the learning rate and epoch?

class Solution {
public:
    double derx(double x,double y,vector<vector<double>>& positions){
        double ans=0;
        for(int i=0;i<positions.size();i++){
            double xi=positions[i][0];
            double yi=positions[i][1];
            double a=sqrt( (x-xi)*(x-xi)+(y-yi)*(y-yi) );
            if(a==0)a+=1e-5;
            ans+=1.0*(x-xi)/a;
        }
        return ans;
    }
    double dery(double x,double y,vector<vector<double>>& positions){
        double ans=0;
        for(int i=0;i<positions.size();i++){
            double xi=positions[i][0];
            double yi=positions[i][1];
            double a=sqrt((x-xi)*(x-xi)+(y-yi)*(y-yi));
            if(a==0)a+=1e-5;
            ans+=1.0*(y-yi)/a;
        }
        return ans;
    } 
    double calc(vector<vector<double>>& positions,double x,double y){
        double ans=0;
        for(int i=0;i<positions.size();i++){
            double xi=positions[i][0];
            double yi=positions[i][1];
            double a=sqrt((x-xi)*(x-xi)+(y-yi)*(y-yi));
            ans+=a;
        }
        return ans;        
    }
    double getMinDistSum(vector<vector<int>>& v) {
        vector<vector<double>>positions;
        for(int i=0;i<v.size();i++){
            positions.push_back({1.0*v[i][0]/100,1.0*v[i][1]/100});
        }
        double x=0 ,y=0,epoch=1e5,learning_rate=0.5;            
        while(epoch>0){
            epoch=epoch-1;
            //if(derx(x,y,positions)*learning_rate==0||dery(x,y,positions)*learning_rate==0)learning_rate/=2;
            x=x-derx(x,y,positions)*learning_rate;
            y=y-dery(x,y,positions)*learning_rate;
            if(x<0)x=1;
            if(y<0)y=1;
			
        }
        x=x*100;
        y=y*100;
        cout<<x<<" "<<y;
         for(int i=0;i<v.size();i++){
             positions[i][0]*=100;
             positions[i][1]*=100;
         }
        return calc(positions,x,y);
    }
};
Comments (0)