Thank you Ben. I tried similar to that in C++. Only h(x) <= 0 is satisfied
but g(x) <= 0 (InequalityConstraint2) is not satisfied.
Hope you can help.

--
Regards,
Prabhakar Vemavarapu
Cell: (337)212-6277

On Wed, Nov 29, 2017 at 1:24 AM, Ben Bokser <b.bok...@gmail.com> wrote:

> Hi,
>
> Here is an example I wrote in C.
>
> Sincerely,
> *Benjamin Bokser*
> Cell: (917) 376-6326
> Email: bbok...@gmail.com <b.bok...@gmail.com>
> https://www.linkedin.com/in/ben-bokser
>
> On Wed, Nov 29, 2017 at 2:15 AM, P V V <pvv8...@gmail.com> wrote:
>
>> Hi
>> I tried to get in touch with you regarding help with NLOPT. I have an
>> optimization problem with two nonlinear constraints - h(x) <= 0, g(x) <=0.
>> Please let me know if this is possible in NLOPT.
>> Thanks.
>>
>> _______________________________________________
>> NLopt-discuss mailing list
>> NLopt-discuss@ab-initio.mit.edu
>> http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
>>
>>
>
#include "Optimization.h"

igraph_t _complete_Graph;
igraph_t current_Graph;
igraph_t _subgraph;

std::vector<float> band_Radii;
std::vector<std::vector<int>> graphClusters;

std::vector<double> cluster_Zero_Angles;
std::vector<int> vertices_Order;
std::vector<float> vertices_Order_Angles;

std::vector<GenerateGraph::GraphNode> graphVertices;
GenerateGraph *genGraph;
std::vector<GenerateGraph::GraphNode> graph_Vertices;

float node_Radius;

clock_t clock_Beginning, clock_Beginning_T;
clock_t clock_End;
double counter_Time;
int timer;

int tel_counter = 0;
double prev_Tel = 0.0;

double stopValue = 0.5f;
int maxEval = 95000;

typedef struct {
	int n1, n2, clusterNum;
	float R;
} inequality_Constraints_Data;

typedef struct {
	int n1, n2;
	float r1, r2;
} edge_Inequality_Constraints_Data;

using namespace std;

Optimization::Optimization(){
	node_Radius = 1.0f;
}

float max_Edge_Length;


double OptimizationFunction(const std::vector<double> &x, std::vector<double> &grad, void *my_func_data){
	double total_Edge_length = 0.0f;

	igraph_integer_t from, to;

	for (int i = 0; i < igraph_ecount(&current_Graph); i++){
		igraph_edge(&current_Graph, i, &from, &to);

		int r1 = band_Radii.at(graph_Vertices[from].belongsToCluster - 1);
		int r2 = band_Radii.at(graph_Vertices[to].belongsToCluster - 1);
		float el = sqrtf(powl(r1, 2) + powl(r2, 2) - 2 * r1 * r2 * cosf(x[from] - x[to]));

		total_Edge_length += el;// sqrtf(powl(r1, 2) + powl(r2, 2) - 2 * r1 * r2 * cosf(x[from] - x[to]));
	}

	return total_Edge_length;
}

double InEqualityConstraints(const std::vector<double> &x, std::vector<double> &grad, void *data){
	inequality_Constraints_Data *d = (inequality_Constraints_Data*)data;

	int m = d->n1;
	int n = d->n2;
	float r = d->R;

	double theta = abs(x[m] - x[n]);

	return ((3 * node_Radius) - (2 * M_PI * r * (theta / 360.f)));
}

double InequalityConstraint2(const std::vector<double> &x, std::vector<double> &grad, void *data){
	edge_Inequality_Constraints_Data *d = (edge_Inequality_Constraints_Data*)data;

	int m = d->n1;
	int n = d->n2;
	int r1 = d->r1;
	int r2 = d->r2;

	float edge_length = sqrt(powl(r1, 2) + powl(r2, 2) - 2 * r1 * r2 * cosl(x[m] - x[n]));

	return edge_length - max_Edge_Length;
}

void Optimization::SetBandRadii(std::vector < float> radiiVector){
	band_Radii = radiiVector;

	max_Edge_Length = 40;// band_Radii.at(band_Radii.size() - 1) - band_Radii.at(0);
}

void Optimization::OptimizeLayout(igraph_t *graph, std::vector<std::vector<int>> clusters, std::vector<double> &x){
	igraph_copy(&current_Graph, graph);

	std::cout << "Optimizing Layout for graph -\n\tVertices: " << igraph_vcount(&current_Graph) << "\n\tEdges: " << igraph_ecount(&current_Graph) << std::endl;

	graphClusters = clusters;

	GenerateLayout(x);
}

void Optimization::GenerateLayout(std::vector<double> &x){
	int total_Number_of_Variables = igraph_vcount(&current_Graph);

	genGraph = new GenerateGraph();
	graphVertices = genGraph->GetVertices();

	graph_Vertices = graphVertices;

	/*************************
	***** NLOPT Objects *****
	*************************/
	nlopt::opt _opt(nlopt::GN_ISRES, total_Number_of_Variables);

	std::vector<double> lb(total_Number_of_Variables);
	std::vector<double> ub(total_Number_of_Variables);

	for (size_t i = 0; i < lb.size(); i++){
		lb[i] = 0.0f;
		ub[i] = 360.0f;
	}

	lb[0] = ub[0] = 0;
	lb[1] = ub[1] = 15;
	lb[2] = ub[2] = 75;
	lb[3] = ub[3] = 165;
	lb[4] = ub[4] = 310;
	
	_opt.set_lower_bounds(lb);
	_opt.set_upper_bounds(ub);

	_opt.set_min_objective(OptimizationFunction, NULL);

	unsigned int total_Constraints = 0;

	for (int i = 0; i < graphClusters.size(); i++){
		int j = graphClusters.at(i).size();

		total_Constraints += (j * (j - 1)) / 2;
	}

	total_Constraints *= 2;
	std::cout << "TOTAL CONSTRAINTS: " << total_Constraints << std::endl;

	std::vector<inequality_Constraints_Data> inequality_data(total_Constraints);// (igraph_vcount(&current_Graph));

	std::vector<edge_Inequality_Constraints_Data> edge_inequality_data(igraph_ecount(&current_Graph));// (igraph_vcount(&current_Graph));

	int counter = 0;
	igraph_integer_t from, to;

	for (size_t i = 0; i < graphClusters.size(); i++){
		for (size_t j = 0; j < graphClusters.at(i).size(); j++){
			for (size_t k = 0; k < graphClusters.at(i).size(); k++){
				if (k != j){
					//inequality_data[counter] = { graphClusters.at(i).at(j), graphClusters.at(i).at(k), band_Radii.at(i) };
					inequality_data[counter].n1 = graphClusters.at(i).at(j);
					inequality_data[counter].n2 = graphClusters.at(i).at(k);
					inequality_data[counter].clusterNum = i;
					inequality_data[counter].R = band_Radii.at(i);

					_opt.add_inequality_constraint(InEqualityConstraints, &inequality_data[counter], 1e-8);
					counter++;
				}
			}
		}
	}

	counter = 0;

	for (int i = 0; i < edge_inequality_data.size(); i++){
		igraph_edge(&current_Graph, i, &from, &to);

		edge_inequality_data[counter].n1 = from;
		edge_inequality_data[counter].n2 = to;
		edge_inequality_data[counter].r1 = band_Radii.at(graph_Vertices[from].belongsToCluster - 1);
		edge_inequality_data[counter].r2 = band_Radii.at(graph_Vertices[to].belongsToCluster - 1);

		_opt.add_inequality_constraint(InequalityConstraint2, &edge_inequality_data[counter], 1e-8);
		counter++;
	}

	double minf;

	std::cout << "Optimizaing...." << std::endl;

	x.resize(total_Number_of_Variables);
	nlopt::result result;


	try{
		/****************************************
		** Set the stop value for optimization **
		*****************************************/
		//_opt.set_maxeval(50000);
		_opt.set_ftol_rel(0.009999f);

		try{
			clock_Beginning = clock();

			result = _opt.optimize(x, minf);
		}
		catch (std::exception& e){
			std::cout << "EXCEPTION RAISED WHILE OPTIMIZING: " << e.what() << std::endl;
		}

		std::cout << "TOTAL TIME TAKEN: " << double(clock() - clock_Beginning) / CLOCKS_PER_SEC << std::endl;
		cout << "RESULTTT:: " << result << endl;

		cout << "Optimized and minimum found at: " << minf << std::endl;
		std::cout << "GLOBAL MIN: " << global_MIN << std::endl;

		std::cout << "RESULT: " << minf << std::endl;
	}
	catch (std::exception& e){
		std::cout << "Exception Creating Opt object -- " << e.what() << std::endl;
	}
}
_______________________________________________
NLopt-discuss mailing list
NLopt-discuss@ab-initio.mit.edu
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to