VRP result got infeaslible in Gurobi-C# - c#

I try to solve pricing problem using Gurobi in C#. It's already worked on AMPL and LINGO but got infeasible when I try in C#. Maybe I wrong in indexing or something, please help me
this is my code in C#
static void Main()
{
try
{
// Model
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
model.ModelName = "OLRPTW Subproblem";
GRBVar[,] x = new GRBVar[DC1, DC1]; //variable of route
GRBVar[] D = new GRBVar[DC1]; //variable of duration
for (int i = 0; i < DC1; i++)
{
for (int j = 0; j < DC1; j++)
{
x[i, j] = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x" + i + "," + j);
}
}
for (int i = 0; i < DC1; i++)
{
D[i] = model.AddVar(A[i], B[i], 0.0, GRB.CONTINUOUS, "D" + i);
}
for (int i = 0; i < DC1; i++)
{
for (int j = 0; j < DC1; j++)
{
time[i, j] = Distance[i, j] + servicetime[i];
}
}
for (int i = 0; i < DC1; i++)
{
for (int j = 0; j < DC1; j++)
{
ReducedCost[i,j] = Distance[i, j]-DualU[i] + Demand[i]*DualV;
}
}
// Integrate new variables
model.Update();
//objective function
GRBLinExpr sumRC = 0.0;
for (int i = 0; i < DC1; i++)
{
for (int j = 0; j < DC1; j++)
{
sumRC += ReducedCost[i, j] * x[i, j];
}
}
model.SetObjective(sumRC, GRB.MINIMIZE);
//constraint 1
GRBLinExpr sumI = 0.0;
for (int i = 0; i < DC1; i++)
{
int j = 0;
sumI += x[i, j];
}
model.AddConstr(sumI, GRB.EQUAL, 1.0, "C1.1");
GRBLinExpr sumJ = 0.0;
for (int j = 0; j < DC1; j++)
{
int i = 0;
sumI += x[i, j];
}
model.AddConstr(sumJ, GRB.EQUAL,1.0, "C1.2");
//constraint 2
for (int h = 0; h < C; h++)
{
GRBLinExpr sumIH = 0.0;
GRBLinExpr sumHJ = 0.0;
for (int i = 0; i < DC1; i++)
{
sumIH += x[i, h];
}
for (int j = 0; j < DC1; j++)
{
sumHJ += x[h, j];
}
model.AddConstr(sumIH - sumHJ, GRB.EQUAL, 0.0, "C2");
}
//constraint 3
GRBLinExpr sumCap = 0.0;
for (int i = 0; i < DC1; i++)
{
for (int j = 0; j < DC1; j++)
{
sumCap += Demand[i] * x[i, j];
}
}
model.AddConstr(sumCap, GRB.LESS_EQUAL, Capacity, "Cap");
//constraint 4
for (int i = 0; i < C; i++)
{
for (int j = 0; j < C; j++)
if(i!=j)
{
GRBLinExpr Time = D[i] + time[i, j] + D[j];
GRBLinExpr M = 10000 * (1 - x[i, j]);
model.AddConstr(Time <= M, "Time" + i + j);
}
}
for (int i = 0; i < DC1; i++)
{
model.AddConstr(x[i, i], GRB.EQUAL, 0,"C*");
}
// Solve
model.Optimize();

Related

C# Neural Network Always Outputting the Same Thing

I am writing a neural network based on this guide (https://learn.microsoft.com/en-us/archive/msdn-magazine/2012/october/test-run-neural-network-back-propagation-for-programmers), and no matter what the inputs, desired outputs, or layer sizes are, it always outputs one:
using System;
namespace Neural_Network_Test
{
internal class Program
{
private int numInput;
private int numHidden;
private int numOutput;
private float[,] inputNodes;
private float[,] weightsIH;
private float[,] weightsHO;
private float[,] outputsIH;
private float[,] outputsHO;
private float[,] sumIH;
private float[,] sumHO;
private float[,] biasIH;
private float[,] biasHO;
private float[,] gradientsO;
private float[,] gradientsH;
private float[,] prevWeightsDeltaIH;
private float[,] prevBiasesDeltaIH;
private float[,] prevWeightsDeltaHO;
private float[,] prevBiasesDeltaHO;
private float[,] rawIH;
private float[,] desiredOutputs;
private float eta = 0.9f;
private float momentum = 0.4f;
private int iteration = 0;
private static void Main(string[] args)
{
Program program = new Program();
program.numInput = 3;
program.numHidden = 4;
program.numOutput = 2;
program.desiredOutputs = new float[program.numOutput, 1];
for (int i = 0; i < program.numOutput; i++)
{
Console.Write("Output " + (i + 1).ToString() + ": ");
program.desiredOutputs[i, 0] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Enter Iterations: ");
int numIterations = 0;
numIterations = Convert.ToInt32(Console.ReadLine());
program.SetMisc();
program.SetNodes();
program.SetWeights();
for (int i = 0; i < numIterations; i++)
{
program.ComputeOutputs();
program.BackPropagation();
program.AddDeltas();
Console.WriteLine("Outputs: ");
for (int j = 0; j < program.numOutput; j++)
{
Console.WriteLine("Output " + (j + 1).ToString() + ": " + program.outputsHO[j, 0].ToString());
}
float cost = 0f;
for (int w = 0; w < program.numOutput; w++)
{
cost += 0.5f * (float)Math.Pow(program.desiredOutputs[w, 0] - program.outputsHO[w, 0], 2);
}
Console.WriteLine("Cost: " + cost.ToString());
program.iteration++;
}
}
private void SetNodes()
{
Random rnd = new Random();
for (int i = 0; i < numInput; i++)
{
inputNodes[i, 0] = (float)rnd.NextDouble();
}
}
private void SetWeights()
{
Random rnd = new Random();
weightsIH = new float[numInput, numHidden];
for(int i = 0; i < numInput; i++)
{
for(int j = 0; j < numHidden; j++)
{
if (rnd.NextDouble() > 0.5)
{
weightsIH[i, j] = (float)rnd.NextDouble();
}
else
{
weightsIH[i, j] = (float)rnd.NextDouble() * -1;
}
weightsIH[i, j] = 1f;
}
}
weightsHO = new float[numHidden, numOutput];
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
if (rnd.NextDouble() > 0.5)
{
weightsHO[i, j] = (float)rnd.NextDouble();
}
else
{
weightsHO[i, j] = (float)rnd.NextDouble() * -1;
}
weightsHO[i, j] = 1f;
}
}
for(int i = 0; i < numHidden; i++)
{
biasIH[i, 0] = rnd.Next(20) - 10;
}
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] = rnd.Next(20) - 10;
}
for(int i = 0; i < numHidden; i++)
{
biasIH[i, 0] = 0;
}
for(int i = 0; i < numOutput; i++)
{
biasHO[i, 0] = 0;
}
weightsIH = new float[numInput, numHidden];
weightsHO = new float[numHidden, numOutput];
float value = 0.1f;
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
weightsIH[i, j] = (float)Math.Round(value, 2);
value += 0.1f;
}
}
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
weightsHO[i, j] = value;
value += 0.1f;
}
}
}
private void SetMisc()
{
sumIH = new float[numHidden, 1];
biasIH = new float[numHidden, 1];
sumHO = new float[numOutput, 1];
biasHO = new float[numOutput, 1];
gradientsO = new float[numOutput, 1];
gradientsH = new float[numHidden, 1];
prevWeightsDeltaIH = new float[numInput, numHidden];
prevWeightsDeltaHO = new float[numHidden, numOutput];
prevBiasesDeltaIH = new float[numHidden, 1];
prevBiasesDeltaHO = new float[numOutput, 1];
outputsIH = new float[numHidden, 1];
outputsHO = new float[numOutput, 1];
rawIH = new float[numHidden, 1];
inputNodes = new float[numHidden, 1];
}
private void ComputeOutputs()
{
//Hidden
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numInput; j++)
{
sumIH[i, 0] += inputNodes[j, 0] * weightsIH[j, i]; //
}
sumIH[i, 0] += biasIH[i, 0];
rawIH[i, 0] = sumIH[i, 0];
outputsIH[i, 0] = SigmoidFunction(sumIH[i, 0]);
}
//Output
for (int i = 0; i < numOutput; i++)
{
for (int j = 0; j < numHidden; j++)
{
sumHO[i, 0] += outputsIH[j, 0] * weightsHO[j, i];
}
sumHO[i, 0] += biasHO[i, 0];
outputsHO[i, 0] = HyperTanFunction(sumHO[i, 0]);
}
}
private void BackPropagation()
{
//Outputs
for (int i = 0; i < numOutput; i++)
{
gradientsO[i, 0] = (desiredOutputs[i, 0] - outputsHO[i, 0]) * (1 - outputsHO[i, 0]) * (1 + outputsHO[i, 0]);
}
//Hidden
for (int i = 0; i < numHidden; i++)
{
float outputComp = 0;
for (int j = 0; j < numOutput; j++)
{
outputComp += gradientsO[j, 0] * weightsHO[i, j];
}
gradientsH[i, 0] = (outputsIH[i, 0]) * (1 - outputsIH[i, 0]) * outputComp;
}
//Delta IH
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
prevWeightsDeltaIH[i, j] = eta * gradientsH[j, 0] * inputNodes[i, 0];
}
}
//Delta HO
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
}
}
//Bias IH
for (int i = 0; i < numHidden; i++)
{
biasIH[i, 0] = eta * gradientsH[i, 0] * 1;
}
//Bias HO
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] = eta * gradientsO[i, 0] * 1;
}
}
private void AddDeltas()
{
if (iteration > 0)
{
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
weightsIH[i, j] += prevWeightsDeltaIH[i, j] + (prevWeightsDeltaIH[i, j] * momentum);
}
}
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
weightsHO[i, j] += prevWeightsDeltaHO[i, j] + (prevWeightsDeltaHO[i, j] * momentum);
}
}
for (int i = 0; i < numHidden; i++)
{
biasIH[i, 0] += prevBiasesDeltaIH[i, 0] + (prevBiasesDeltaIH[i, 0] * momentum);
}
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] += prevBiasesDeltaHO[i, 0] + (prevBiasesDeltaHO[i, 0] * momentum);
}
}
else
{
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
weightsIH[i, j] += prevWeightsDeltaIH[i, j];
}
}
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
weightsHO[i, j] += prevWeightsDeltaHO[i, j];
}
}
for (int i = 0; i < numHidden; i++)
{
biasIH[i, 0] += prevBiasesDeltaIH[i, 0];
}
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] += prevBiasesDeltaHO[i, 0];
}
}
}
private static float SigmoidFunction(float x)
{
if (x < -45.0f) return 0.0f;
else if (x > 45.0) return 1.0f;
else return (float)(1.0f / (1.0f + Math.Exp(-x)));
}
private static float HyperTanFunction(float x)
{
if (x < -10.0) return -1.0f;
else if (x > 10.0) return 1.0f;
else return (float)(Math.Tanh(x));
}
}
}
I have tried changing the values of the input layer, the desired values, layer sizes, checked the backpropagation code, and many other things, but it always outputs one.

How can i improve this code to find rectangles and not only squares

I have this code and I work finding squares of "0" in 2d arrays.
How can I make find rectangles and not only squares?
I'm having some difficulty doing this.
// Set first column of S[,]
for (i = 0; i < R; i++)
{
S[i, 0] = M[i, 0];
}
// Set first row of S[][]
for (j = 0; j < C; j++)
{
S[0, j] = M[0, j];
}
// Construct other entries of S[,]
for (i = 1; i < R; i++)
{
for (j = 1; j < C; j++)
{
if (M[i, j] == 0)
S[i, j] = Math.Min(S[i, j - 1], Math.Min(S[i - 1, j], S[i - 1, j - 1])) + 1;
}
}
// Find the maximum entry, and indexes of maximum entry in S[,]
max_of_s = S[0, 0];
max_i = 0;
max_j = 0;
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
{
if (max_of_s < S[i, j])
{
max_of_s = S[i, j];
max_i = i;
max_j = j;
}
}
}
Console.WriteLine("Maximum size sub-matrix is: ");
for (i = max_i; i > max_i - max_of_s; i--)
{
for (j = max_j; j > max_j - max_of_s; j--)
{
M[i, j] = 2;
Console.Write(M[i, j] + " ");
}
Console.WriteLine();
}
Find any size rectangles and not only squares.

complex valued neural network back propogation

i am working oncomplex valued neural networks, rvnn ok but when convert it to cvnn error is not decreases, and i cant figure out where is mistake, my code is here
public bool Train(List<Complex> input, List<Complex> output)
{
if ((input.Count != this.Layers[0].Neurons.Count) || (output.Count != this.Layers[this.Layers.Count - 1].Neurons.Count)) return false;
Run(input);
error = 0;
for(int i = 0; i < this.Layers[this.Layers.Count - 1].Neurons.Count; i++)
{
Neuron neuron = this.Layers[this.Layers.Count - 1].Neurons[i];
neuron.Delta = dSigmoid(neuron.Value) * (output[i] - neuron.Value);
error += (output[i] - neuron.Value).Magnitude;
for(int j = this.Layers.Count - 2; j > 2; j--)
{
for(int k = 0; k < this.Layers[j].Neurons.Count; k++)
{
Neuron n = this.Layers[j].Neurons[k];
n.Delta = dSigmoid(n.Value)*
this.Layers[j + 1].Neurons[i].Dendrites[k].Weight *
this.Layers[j + 1].Neurons[i].Delta;
}
}
}
for(int i = this.Layers.Count - 1; i > 1; i--)
{
for(int j=0; j < this.Layers[i].Neurons.Count; j++)
{
Neuron n = this.Layers[i].Neurons[j];
n.Bias = n.Bias + (this.LearningRate * n.Delta);
for (int k = 0; k < n.Dendrites.Count; k++)
n.Dendrites[k].Weight = n.Dendrites[k].Weight + (this.LearningRate * this.Layers[i - 1].Neurons[k].Value * n.Delta);
}
}
return true;
}

Change 2d array bubble sort to counting sort

The following code sorts rows by the first element using bubble method.
I can't change it to counting sort.
public void SortStack(double[,] n)
{
for (int i = 0; i < n.GetLength(0) - 1; i++)
{
for (int j = i; j < n.GetLength(0); j++)
{
if (n[i, 0] > n[j, 0])
{
for (int k = 0; k < n.GetLength(1); k++)
{
var temp = n[i, k];
n[i, k] = n[j, k];
n[j, k] = temp;
}
}
}
}
}
Please help.
As you do bubble sort based on first element of each row. you should do counting sort like that too. so you just need to count first item of each row.
private static int[,] CountingSort2D(int[,] arr)
{
// find the max number by first item of each row
int max = arr[0, 0];
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] > max) max = arr[i, 0];
}
// we want to get count of first items of each row. thus we need 1d array.
int[] counts = new int[max + 1];
// do the counting. again on first index of each row
for (int i = 0; i < arr.GetLength(0); i++)
{
counts[arr[i, 0]]++;
}
for (int i = 1; i < counts.Length; i++)
{
counts[i] += counts[i - 1];
}
// result is sorted array
int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
counts[arr[i, 0]]--;
// now we need to copy columns too. thus we need another loop
for (int j = 0; j < arr.GetLength(1); j++)
{
result[counts[arr[i, 0]], j] = arr[i, j];
}
}
return result;
}
Here is the test.
static void Main()
{
Random rand = new Random();
int[,] arr = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = rand.Next(0, 100);
}
}
int[,] newarr = CountingSort2D(arr);
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
for (int i = 0; i < newarr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < newarr.GetLength(1); j++)
{
Console.Write(newarr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
}
Example Output:
{ 49,66,8 },{ 33,39,64 },{ 65,52,76 } // Original array
{ 33,39,64 },{ 49,66,8 },{ 65,52,76 } // Sorted array

How to improve nested loop in C#?

I have a function to calculate the costMatrix like this code below.
public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
// For D(n,1)
for (int i = 0; i < firstSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
}
}
// For D(1,m)
for (int i = 0; i < secondSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
}
}
// For D(n,m)
for (int i = 1; i < firstSeq.Count; i++)
{
for (int j = 1; j < secondSeq.Count; j++)
{
double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
}
}
return costMatrix;
}
For the 3rd loop (n,m), how could i improve its performance if the "count" of each Sequence is 1 million points.

Categories