objective and constraints having a common function - problem based in matlab 2016b
hi, 
in my current optimization problem, both the objective and cosntraint functions are calculated following a time-consuming simulink run. in order to avoid having to run the simulation twice at each iteration (one to compute the objective, one to compute the constraint), i would like to apply the same method as described in https://fr.mathworks.com/help/optim/ug/objective-and-constraints-using-common-function.html. 
however, my work only gives access to matlab 2016b, and the functions used are not available yet : optimvar, fcn2optimexpr. 
is there a way to convert my function "compute\_all" (which computes both the objective and the constraint) to an optimization expression, but in matlab 2016b ? 

thank you for your help, 
thomas
perhaps what you are looking for is in objective and nonlinear constraints in the same function.
alan weiss
matlab mathematical toolbox documentation
You are mixing [expressions](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression?view=netframework-4.7.1) with [lambda expressions](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.lambdaexpression?view=netframework-4.7.1). There are many posts showing how you can combine lambda expressions, but the essential part is to compose expressions from lambda expression [bodies](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.lambdaexpression.body?view=netframework-4.7.1) and rebind the [parameters](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.lambdaexpression.parameters?view=netframework-4.7.1).


The later is usually achieved by a custom [`ExpressionVisitor`](https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expressionvisitor?view=netframework-4.7.1) like this:



```
using System.Linq.Expressions;

public static class ExpressionExtensions
{
    public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target)
    {
        return new ParameterReplacer { Source = source, Target = target }.Visit(expression);
    }

    class ParameterReplacer : ExpressionVisitor
    {
        public ParameterExpression Source;
        public Expression Target;
        protected override Expression VisitParameter(ParameterExpression node)
        {
            return node == Source ? Target : base.VisitParameter(node);
        }
    }
}

```

Now regarding the EF Core combined query filters. 


Using dictionaries and expression lists seems overcomplicated for what are you doing. Since [`IMutableEntityType`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.imutableentitytype?view=efcore-2.1) provides read/write access to the [`QueryFilter`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.imutableentitytype.queryfilter?view=efcore-2.1#Microsoft_EntityFrameworkCore_Metadata_IMutableEntityType_QueryFilter), the same can be achieved with a small set of a custom extension methods.


All they go inside a class like this:



```
using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public static class QueryFilterExtensions
{
}

```

First method:



```
public static void AddQueryFilter(this IMutableEntityType target, LambdaExpression filter)
{
    if (target.QueryFilter == null)
        target.QueryFilter = filter;
    else
    {
        var parameter = target.QueryFilter.Parameters[0];
        var left = target.QueryFilter.Body;
        var right = filter.Body.ReplaceParameter(filter.Parameters[0], parameter);
        var body = Expression.AndAlso(left, right);
        target.QueryFilter = Expression.Lambda(body, parameter);
    }
}

```

This is a non generic method which combines the exiting filter with passed filter using `AndAlso` (C# `&&`) operator and shows the aforementioned lambda expression combining principles.


However it's not so useful directly, like inside your entity type configuration loop (it can, but requires you to build manually the lambda expression instead of letting C# compiler do that). So here comes the second method:



```
public static void AddQueryFilter<T>(this IMutableEntityType target, Expression<Func<T, bool>> filter)
{
    LambdaExpression targetFilter = filter;
    if (target.ClrType != typeof(T))
    {
        var parameter = Expression.Parameter(target.ClrType, "e");
        var body = filter.Body.ReplaceParameter(filter.Parameters[0], parameter);
        targetFilter = Expression.Lambda(body, parameter);
    }
    target.AddQueryFilter(targetFilter);
}

```

It's a generic method - not quite type-safe, but allows you to use a compile time lambda expression and bind it to the actual entity type as follows:



```
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
    if (typeof(IDeleted).IsAssignableFrom(entityType.ClrType))
        entityType.AddQueryFilter<IDeleted>(e => e.DateTimeDeleted == null);
}

```

Looks better, isn't it :)


The last custom extension method is complement to (replacement of) the standard EF Core generic [`HasQueryFilter`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.builders.entitytypebuilder-1.hasqueryfilter?view=efcore-2.1#Microsoft_EntityFrameworkCore_Metadata_Builders_EntityTypeBuilder_1_HasQueryFilter_System_Linq_Expressions_Expression_System_Func__0_System_Boolean___) method:



```
public static EntityTypeBuilder<TEntity> AddQueryFilter<TEntity>(this EntityTypeBuilder<TEntity> target, Expression<Func<TEntity, bool>> filter)
    where TEntity : class
{
    target.Metadata.AddQueryFilter(filter);
    return target;
}

```

and allows you to replace



```
this.AddToDict<DbMyEntity>(t => t.Name == null || t.Name == "Something");

```

with the more convenient



```
modelBuilder.Entity<DbMyEntity>()
    .AddQueryFilter(t => t.Name == null || t.Name == "Something");

```

**Update (EF Core 3.0):** `QueryFilter` property has been replaced with `GetQueryFilter` and `SetQueryFilter` extension methods. applying and factorization, we get, also changing by reversing n and dnote: - try to factorize that term which help in reducing expression. Hello!


 Not only in the German offices, this is pretty common - I did something very similar with Bain myself back in the day-. 


I would strongly recomment you practice it with the Integrated Reasoning part of the GMAT exam.


There are free exams in the internet that you can use for practice (the one of LBS MBA page, Verits prep, as well as some free trials for courses such as the one of The Economist (https://gmat.economist.com/)


The (old) McKinsey PST, BCG Online Test, Bain SOVA test, etc. are all quite similar so leverage resources across all of them.


https://www.myconsultingoffer.org/case-study-interview-prep/bcg-online/


https://www.shl.com/shldirect/en/practice-tests


https://www.psychometricinstitute.co.uk/Free-Aptitude-Tests.asp


Hope it helps!


Cheers,


Clara you can not configure or train the ocr software as end-user. my suggestion: for [number ocr use the ocr engine2](https://ocr.space/ocrapi#ocrengine) - it is usually better with numbers. welcome to mrexcel!

it actually works for me (see below). this appears to be a kind of rounding error. the way excel stores numbers causes some issues when you have decimals. it is a bit rare when comparing a cell to itself, but it can happen. you can try to use sumproduct instead, see column h.

efgh1valuescountifranksumproduct248737.76222336113.11333454287.76111f2:f4f2=countif($e$2:$e$4,">"&e2)+1g2:g4g2=rank.eq(e2,$e$2:$e$4)h2:h4h2=sumproduct(--(round($e$2:$e$4,2)>round(e2,2)))+1 never mind, finally solved it. i called this.engine.runrenderloop while it’s already running, therefore causing the speed to run twice, because it added a loop/callback the 2nd time.

all i need to do for the ‘resume’ part of my function is to always call this.engine.stoprenderloop() before i call t… functions prefixed with “common” are part of our example framework and not part of fmod itself. this code is added to the examples via common.cpp and common\_platform.cpp. developers would usually replace this code with their own application code. May be the function is executed before the document is ready.
Try to use document.ready function like this:



```
$( document ).ready(function() {
    $('#machines').change(function() {
        var $selected = $(this).find(':selected');
        $('#description').html($selected.data('description'));
    }).trigger('change');
});

``` Can you show the calculate function that you've wrote so far? doing integration yields-then finally f(x)=-x cos x + sin xto calculate derivative of the above function f(x) we have to apply formula of derivation of products of two functions-f(x) =-x cos(x)+sin(x)f’(x) =-[x(-sin x)+cos x.1]+cos x ;{by formula d/dx (f.g)=f.g’+g.f’}f’(x) =-(-x sin x) - cos x + cos xf’(x) = x.sin(x)