For Programmers: Free Programming Magazines  


Home > Archive > Fortran > January 2008 > Re: Interfacing C routine with CVF routine called from VB









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Interfacing C routine with CVF routine called from VB
axtens

2008-01-24, 4:34 am

G'day everyone

Okay, it seems there never was a problem. What WAS the problem was all
the typos that had crept into the C code and had been buried under a
pile of s///g.

Here's the revised code for your entertainment. And yes, in my pursuit
for a solution I turned a subroutine into a function. Yes, it does
work, but i could have left it alone without any loss of
functionality. Do the INTENTs mean anything?

Kind regards,
Bruce.


REAL*8 function KnapsackA43( N, P, W, SOLUTION, MASSLIMIT )
RESULT(OPTIMAL)
!dec$ attributes dllexport, stdcall, alias : "KnapsackA43" ::
KnapsackA43
!dec$ attributes reference :: N, P, W, SOLUTION, MASSLIMIT

INTEGER, INTENT(inout) :: N
REAL*8, INTENT(inout) :: P(N)
REAL*8, INTENT(inout) :: W(N)
REAL*8, INTENT(inout) :: SOLUTION(N)
REAL*8, INTENT(inout) :: MASSLIMIT

interface
real(8) function knapsack_A43 [C, Alias:'_knapsack_A43@24'] (nc, wc,
pc, solutionc, massc) result(optimalc)
! dec$ attributes reference :: nc, wc, pc, solutionc, massc
INTEGER, INTENT(inout) :: nc
REAL*8, INTENT(inout) :: wc(nc)
REAL*8, INTENT(inout) :: pc(nc)
REAL*8, INTENT(inout) :: solutionc(nc)
REAL*8, INTENT(inout) :: massc
end function knapsack_A43
end interface
OPTIMAL = knapsack_A43( N, P, W, SOLUTION, MASSLIMIT)
return
end function KnapsackA43

/*
** knaps2.c
*/
/*
** September 26, 1997
** this program implements Algorithm 4.3
** for solving Problem 4.1 (0,1)-Knapsack
** with simple pruning.
*/
/*
** Compile with:
** gcc knaps2.c -o knaps2
**
** Run with:
** knaps2 fname
** or
** knaps2
*/

#include <stdlib.h>

double __stdcall knapsack_A43( int, double*, double*, double*,
double );

void Knapsack2( int, double );
double DotProd( int, double*, double* );

int igNodes;

int igNumber;
double * dgWeights;
double * dgProfits;
double * dgBestSolution;
double dgCapacity;

double dgOptimal;

double * dgCurrentSolution;

double __stdcall knapsack_A43( int iNumber, double *dWeights, double
*dProfits, double *dBestSolution, double dCapacity )
{

igNodes = 0;

igNumber = iNumber;
dgWeights = dWeights;
dgProfits = dProfits;
dgBestSolution = dBestSolution;
dgCapacity = dCapacity;

dgCurrentSolution = (double *)calloc( igNumber, sizeof( double ) );

Knapsack2( 0, 0.0 );

free(dgCurrentSolution);

return (dgOptimal);
}

double DotProd( int n, double* A,double* B)
{
int i;
double ans;
ans=0.0;
for( i=0; i < n;i++ ) {
ans += ( A[ i ] * B[ i ] );
}
return( ans );
}

void Knapsack2( int ell, double curW )
/*
** Algorithm 4.3
*/
{
int i;
igNodes = igNodes + 1;
if ( ell == igNumber ) {
if ( DotProd( igNumber, dgProfits, dgCurrentSolution ) > dgOptimal )
{
dgOptimal = DotProd( igNumber, dgProfits, dgCurrentSolution );
for( i = 0; i < igNumber; i++ ) {
dgBestSolution[ i ] = dgCurrentSolution[ i ];
}
}
}
else {
if( curW + dgWeights[ ell ] <= dgCapacity ) {
dgCurrentSolution[ ell ] = 1.0;
Knapsack2( ell + 1, curW + dgWeights[ ell ] );
}
dgCurrentSolution[ ell ] = 0.0;
Knapsack2( ell + 1, curW );
}

}


Declare Function KnapsackA43 Lib "debug\boal0.dll" (n As Integer, p As
Double, w As Double, x As Double, mass_limit As Double) As Double
Dim pP(1 To 5) As Double
Dim wW(1 To 5) As Double
Dim xX(1 To 5) As Double

pP(1) = 24#
pP(2) = 13#
pP(3) = 23#
pP(4) = 15#
pP(5) = 16#

wW(1) = 12#
wW(2) = 7#
wW(3) = 11#
wW(4) = 8#
wW(5) = 9#


Debug.Print " "

Debug.Print " "
Debug.Print "Object", "Profit", "Mass", "Profit Density",
"Solution"
Debug.Print " "
For i = 1 To 5
Debug.Print i, pP(i), wW(i), pP(i) / wW(i), xX(i)
Next

profit = KnapsackA43(5, pP(1), wW(1), xX(1), mass_limit)

Debug.Print " "

Debug.Print " "
Debug.Print "Object", "Profit", "Weight", "Profit Density",
"Solution"
Debug.Print " "
For i = 1 To 5
Debug.Print i, pP(i), wW(i), pP(i) / wW(i), xX(i)
Next

Debug.Print " "
Debug.Print " Mass Limit " & mass_limit
Debug.Print " Profit " & profit

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com