FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browser

Maximizing the area of a polygon using tokens based input

Description
Demonstrates how to solve a nonlinear problem in C#

Further explanation of this example: 'Xpress NonLinear Reference Manual'

Polygon_dnet.zip[download all files]

Source Files





Polygon.cs

using System;
using System.IO;
using Optimizer;
using XSLPdnet;

namespace XSLPExamples
{
  class XSLPExample
  {

    public static void RunExp1()
    {
      /* Example 1 loads a simple problem, and setups the SLP coefficient using a string. */
      XPRS.Init("");
      XSLP sProb = new XSLP();      

      System.Console.WriteLine("####################");
      System.Console.WriteLine("# Polygon example  #");
      System.Console.WriteLine("####################");
      System.Console.WriteLine(sProb.getBanner());
      
      int nRow, nCol, nSide, nElement;
      int iRow;
      char[] RowType;
      double[] RHS, OBJ, Element, Lower, Upper;
      int[] ColStart, RowIndex;
      double Factor;
      int ReturnValue;
      int i, j;      
      string CoefBuffer;
      int BufferPos;
      string[] RowNames, ColNames;

      nSide = 5;

      /* Rows */
      nRow = nSide - 2 + (nSide - 1) * (nSide - 2) / 2 + 1;
      RHS = new double[nRow]; 
      for (i = 0; i < nRow; i++) RHS[i] = 0;

      RowType = new char[nRow+1];
      RowNames = new string[nRow];
      nRow = 0;
      RowType[nRow] = 'E'; /* OBJEQ */
      RowNames[nRow] = "OBJEQ";
      nRow++;      
      for (i = 1; i < nSide - 1; i++)
      {
        RowType[nRow] = 'G'; /* T2T1 .. T4T3 */
        RowNames[nRow] = "T" + (i + 1) + "T" + i; 
        nRow++;
        RHS[i] = 0.001;
      }

      for (i = 1; i < nSide - 1; i++)
      {
        for (j = i + 1; j < nSide; j++)
        {
          RowType[nRow] = 'L';
          RHS[nRow] = 1.0;
          RowNames[nRow] = "V" + i + "V" + j;          
          nRow++;          
        }
      }
      RowType[nRow] = '\0';

      /* Columns */      
      nCol = (nSide - 1) * 2 + 2;
      nElement = 0;
      OBJ = new double[nCol];
      Lower = new double[nCol];
      Upper = new double[nCol];
      for (i = 0; i < nCol; i++)
      {
        OBJ[i] = 0;           /* objective function */
        Lower[i] = 0;         /* lower bound normally zero */
        Upper[i] = XPRS.PLUSINFINITY; /* upper bound infinity */
      }

      ColStart = new int[nRow + 1];
      Element = new double[10*nCol];
      RowIndex = new int[10*nCol];
      ColNames = new string[nCol];

      /* OBJX */
      nCol = 0;      
      ColStart[nCol] = nElement;
      OBJ[nCol] = 1.0;
      ColNames[nCol] = "OBJX";
      Lower[nCol++] = XPRS.MINUSINFINITY; /* free column */
      Element[nElement] = -1.0;
      RowIndex[nElement++] = 0;      

      /* THETA1 - THETA 4 */
      iRow = 0;
      for (i = 1; i < nSide; i++)
      {        
        ColNames[nCol]  = "THETA" + i;
        ColStart[nCol++] = nElement;
        if (i < nSide - 1)
        {
          Element[nElement] = -1;
          RowIndex[nElement++] = iRow + 1;
        }
        if (i > 1)
        {
          Element[nElement] = 1;
          RowIndex[nElement++] = iRow;
        }
        iRow++;
      }

      Upper[nCol - 1] = 3.1415926;

      /* Equals column */      
      ColNames[nCol] = "=";
      ColStart[nCol] = nElement;
      Lower[nCol] = Upper[nCol] = 1.0; /* fixed at 1.0 */
      nCol++;

      /* Remaining columns come later */
      for (i = 1; i < nSide; i++)
      {
        Lower[nCol] = 0.01;   /* lower bound */
        Upper[nCol] = 1;
        ColNames[nCol] = "RHO" + i;
        ColStart[nCol++] = nElement;        
      }
      ColStart[nCol] = nElement;

      sProb.XPRS.LoadLP("Polygon", nCol, nRow, RowType, RHS, null, OBJ, ColStart, null, RowIndex, Element, Lower, Upper);

      sProb.XPRS.AddNames(1, RowNames, 0, nRow - 1);
      sProb.XPRS.AddNames(2, ColNames, 0, nCol - 1);      

      /* Build up nonlinear coefficients */      

      /* Area */      
      double[] coeffFactor = { 0.5 };

      BufferPos = 0;
      CoefBuffer = "";
      for (i = 1; i < nSide - 1; i++)
      {

        if (i > 1)
        {
          CoefBuffer += " + "; 
        }
        CoefBuffer +=  "RHO" + (i+1) + " * RHO" +i +" * SIN ( THETA" + (i+1) + " - THETA" + i + " )";
      }
      sProb.chgCCoef(0, nSide, coeffFactor, CoefBuffer);

      /* Distances */
      coeffFactor[0] = 1.0;      
      for (i = 1; i < nSide - 1; i++)
      {
        for (j = i + 1; j < nSide; j++)
        {
          CoefBuffer = "RHO" + j + " ^ 2 + RHO" + i + " ^ 2 - 2 * RHO" + j + " * RHO" + i + " * COS ( THETA" + j + " - THETA" + i + " )";
          sProb.chgCCoef( iRow, nSide, coeffFactor, CoefBuffer);
          iRow++;
        }
      }

      sProb.writeProb("Polygon_sharp.lp", "l");

      sProb.maxim("");
      sProb.WriteSlxSol("Polygon_sharp", "");

      /* Cleanup */
      sProb.destroyProb();
      XPRS.Free();
    }

    public static void Main(string[] args)
    {
      RunExp1();
      Console.ReadLine();
    }
  }
}


Back to examples browser