<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: (help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30 in ObjectARX Forum</title>
    <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11917825#M1844</link>
    <description>Thank you very much. Let me learn.</description>
    <pubDate>Mon, 24 Apr 2023 13:43:12 GMT</pubDate>
    <dc:creator>463017170</dc:creator>
    <dc:date>2023-04-24T13:43:12Z</dc:date>
    <item>
      <title>(help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30</title>
      <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11817743#M1839</link>
      <description>&lt;P&gt;Help me！ Where is the problem？&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="cpp"&gt;class Solutionsubsets_targe {
public:
	vector&amp;lt;double&amp;gt; res;
	void dfs(vector&amp;lt;double&amp;gt;&amp;amp; nums,int start,double&amp;amp; track,double target)
	{
		if ( track &amp;lt; target)
		{
			res.push_back(track);
		}

		for(int i=start;i&amp;lt;nums.size();i++)
		{
			if (FindDouble(res, track + nums.at(i) , 1.0E-7) &amp;lt; 0 )
			{
				track += nums.at(i);
			}
			dfs(nums,i+1,track,target);
			track -= nums[i];
		}
		return ;
	}
	vector&amp;lt;double&amp;gt; subsets(vector&amp;lt;double&amp;gt;&amp;amp; nums,double target) {
		double track = 0;
		dfs(nums,0,track,target);
		return res;
	}
};



static int  FindDouble(vector&amp;lt;double&amp;gt; VD, double D, double tol /*= 1.0E-7*/)
{
	for (int i = 0; i &amp;lt; VD.size(); i++)
	{
		if ( fabs(VD.at(i)-D) &amp;lt;tol )
		{
			return 1;
		}
	}
	return -1;
}



static void  MyGroupMyCommand () 
{  

   vector&amp;lt;double&amp;gt; arr;  
   arr.push_back(1.1);arr.push_back(2.1);arr.push_back(3.1);arr.push_back(4.1);;arr.push_back(5.1); 
   arr.push_back(6.1);arr.push_back(7.1);arr.push_back(8.1);arr.push_back(9.1);;arr.push_back(10.1); 

  Solutionsubsets_targe bb;
  vector&amp;lt;double&amp;gt; b= bb.subsets(arr,30);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2023 13:32:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11817743#M1839</guid>
      <dc:creator>463017170</dc:creator>
      <dc:date>2023-03-13T13:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: (help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30</title>
      <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11819141#M1840</link>
      <description>&lt;P&gt;You might have a look at Google’s &amp;nbsp;OR-Tools @ &lt;A href="https://developers.google.com/optimization" target="_blank" rel="noopener"&gt;https://developers.google.com/optimization&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wrapped their traveling salesman solver for autolisp, to find the shortest polyline. Anyway, they have packing solvers, Knapsack, bin-packing etc. &amp;nbsp;You can use my project as a guide to working with their libraries with ARX.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BTW, some of their solvers are multi-threaded, yeah, for that CPU burn.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TSP Discussion is here: &lt;A href="https://www.theswamp.org/index.php?topic=58049.0" target="_blank" rel="noopener"&gt;https://www.theswamp.org/index.php?topic=58049.0&lt;/A&gt;&lt;/P&gt;&lt;P&gt;My solution is here: &lt;A href="https://drive.google.com/file/d/1wwZQ-wIL-6lh6A-QILNyzYU5-TttKeA6/view?usp=share_link" target="_blank" rel="noopener"&gt;https://drive.google.com/file/d/1wwZQ-wIL-6lh6A-QILNyzYU5-TttKeA6/view?usp=share_link&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 00:20:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11819141#M1840</guid>
      <dc:creator>daniel_cadext</dc:creator>
      <dc:date>2023-03-14T00:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: (help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30</title>
      <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11831527#M1841</link>
      <description>&lt;P&gt;Thank you for your help! I cannot open this webpage, can I post the code to the forum?&lt;/P&gt;</description>
      <pubDate>Sun, 19 Mar 2023 02:08:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11831527#M1841</guid>
      <dc:creator>463017170</dc:creator>
      <dc:date>2023-03-19T02:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: (help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30</title>
      <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11832814#M1842</link>
      <description>&lt;P&gt;Which one? You’d need to access OR-Tools docs to see their samples, instructions etc.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2023 02:40:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11832814#M1842</guid>
      <dc:creator>daniel_cadext</dc:creator>
      <dc:date>2023-03-20T02:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: (help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30</title>
      <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11917769#M1843</link>
      <description>&lt;P&gt;First: This is not an ObjectARX question at all. Maybe you get better answers if you post it to a general C++ or algorithm forum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll try to help you anyway, because I like this kind of problems &lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;.&lt;/P&gt;
&lt;P&gt;Your original question was: "Where is the problem?".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Honestly I can't answer this question because I didn't understand which algorithm you tried to implement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I wrote a console application that finds the best solution and also prints out the combination it tried to find it.&lt;/P&gt;
&lt;P&gt;The main part of the code looks like this:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;void findBest()
{
	for (;;) // go forth and back until we can't go back anymore.
	{
		forth();
		if (!back())
			break;
	}
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These classes are involved:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;class ValCount {
	double   _val;		// value i.e. 10.1, 8.1,,,
	unsigned _count;	// how many times this value can be used. (always 1 with the sample data)
	double   _sumAll;   // sum of (_cout * _val) of this and all follwing ValCount entries in the array
};

class Result {
	vector&amp;lt;unsigned&amp;gt;	_counts;
	double				_sum = 0.0;
};

class ValSum {
	const vector&amp;lt;ValCount&amp;gt;&amp;amp; _valArr; // input data, preprocessed: Sorted for descending _val
	const double		    _target; // 30.0
	size_t					_index; // current index
	vector&amp;lt;unsigned&amp;gt;		_counts; // how many times is _valArr[i]._val in the addition
	double					_sum; // current sum
	Result					_best; // current best solution
	void findBest();
	void forth();
	void back();
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output looks like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tbrammer_0-1682341140112.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1205992i4EB338EE01570764/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tbrammer_0-1682341140112.png" alt="tbrammer_0-1682341140112.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Short explanation how forth() and back() work for the first lines:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="tbrammer_1-1682342358916.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1206003i09395409A8ADAAC8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="tbrammer_1-1682342358916.png" alt="tbrammer_1-1682342358916.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One might expect that we need to check 2^N = 1024 combinations if we have N summands.&lt;/P&gt;
&lt;P&gt;But actually we only need to check a small fraction of them.&lt;/P&gt;
&lt;P&gt;The complete code is bellow. I hope the comments are sufficieent to explain how it works. Otherwise feel free to ask..&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;#include &amp;lt;vector&amp;gt;
#include &amp;lt;map&amp;gt;
#include &amp;lt;algorithm&amp;gt;
using namespace std;

void fillVector(vector&amp;lt;double&amp;gt;&amp;amp; arr)
{
	arr.push_back(1.1); arr.push_back(2.1); arr.push_back(3.1); arr.push_back(4.1);; arr.push_back(5.1);
	arr.push_back(6.1); arr.push_back(7.1); arr.push_back(8.1); arr.push_back(9.1);; arr.push_back(10.1);
}

class ValCount
{
public:
	ValCount(const double&amp;amp; v, const unsigned&amp;amp; cnt, const double&amp;amp; sumAll)
		: _val(v), _count(cnt), _sumAll(sumAll)
	{}
	double   _val;
	unsigned _count;
	double   _sumAll;
};

static void print(const vector&amp;lt;unsigned&amp;gt;&amp;amp; counts, const double&amp;amp; sum, const double&amp;amp; target, const char* msg = "")
{
	for (size_t i = 0; i &amp;lt; counts.size(); ++i)
		printf("%d, ", counts[i]);
	printf("  Sum = %lf, Diff = %lf%s\n", sum, target - sum, msg ? msg : "");
}

class Result
{
public:
	vector&amp;lt;unsigned&amp;gt;	_counts;
	double				_sum = 0.0;
public:
	void print(const double&amp;amp; target, const char* msg = "")
	{
		::print(_counts, _sum, target, msg);
	}
};

class ValSum
{
private:
	const vector&amp;lt;ValCount&amp;gt;&amp;amp; _valArr;
	const double		    _target;
	size_t				_index;
	vector&amp;lt;unsigned&amp;gt;	_counts;
	double				_sum;

	Result				_best;

public:
	ValSum(const vector&amp;lt;ValCount&amp;gt;&amp;amp; valArr, double trg)
		: _valArr(valArr), _sum(0.0), _counts(valArr.size(), 0), _target(trg), _index(0)
	{
	}

	void findBest()
	{
		for (;;) // go forth and back until we can't go back anymore.
		{
			forth();
			if (!back())
				break;
		}
	}

	// Here we start at index, increment index and add as many elements as possible to the addition
	// All counts[i] with i&amp;gt;index are 0.
	// We end up with index being the index of the last added element.
	// If the result is better than the best result yet, we choose the new result.
	void forth()
	{
		const size_t arrSize = _counts.size();
		size_t indexNew = _index;
		for (; _index &amp;lt; arrSize; ++_index)
		{
			const ValCount&amp;amp; vc = _valArr[_index];
			unsigned n = (unsigned)((_target - _sum) / vc._val);
			n = min(n, vc._count);
			_counts[_index] = n;
			if (n &amp;gt; 0)
			{
				_sum += vc._val * n;
				indexNew = _index;
			}
		}
		_index = indexNew; //highest modified index.
		if (_sum &amp;gt; _best._sum)
		{
			// found better solution
			_best._counts = _counts;
			_best._sum = _sum;
			_best.print(_target, " new best");
		}
		else
			::print(_counts, _sum, _target, "");
	}

	// Here we start at _index and remove the last added element _counts[_index] first.
	// If the sum of all elements _valArr[i]._val with i&amp;gt;_index is big enough to sum up to target, we go forth with ++_index
	// 
	bool back()
	{
		if (_index == 0)
			return false; // can't go back any further

		for (;; --_index)
		{
			unsigned&amp;amp; cnt = _counts[_index];
			if (cnt &amp;gt; 0)
			{
				if (_index &amp;lt; _counts.size() - 1)
				{
					--cnt;
					_sum -= _valArr[_index]._val;
					if (_sum + _valArr[++_index]._sumAll &amp;gt;= _best._sum) // can we reach target with a lower difference?
						return true; // Yes. Let's go forth()
					// No. Even if we sum up all remaining elements. Go back further.
				}
				else
				{
					// this is the last entry in counts[]. We can't go forth from here
					_sum -= _valArr[_index]._val * cnt;
					cnt = 0;
				}
			}
			if (_index == 0)
				return false; // can't go back any further (note: index is unsigned)
		}
	}
};

static void MyMaxSum()
{
	vector&amp;lt;double&amp;gt; arr;
	fillVector(arr);

	map&amp;lt;double, unsigned&amp;gt; val2count;
	for (const double&amp;amp; val : arr)
		val2count[val]++;

	vector&amp;lt;ValCount&amp;gt; valArr;
	arr.reserve(val2count.size());
	double sumAll = 0.0;
	for (const auto&amp;amp; entry : val2count)
	{
		sumAll += entry.first * entry.second;
		valArr.push_back(ValCount(entry.first, entry.second, sumAll));
	}
	reverse(valArr.begin(), valArr.end());

	ValSum valsum(valArr, 30.0);
	valsum.findBest();
}

int main()
{
	MyMaxSum();
	getchar();
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2023 13:24:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11917769#M1843</guid>
      <dc:creator>tbrammer</dc:creator>
      <dc:date>2023-04-24T13:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: (help)Find the maximum value of the sum of all arrs and the maximum value is less than target=30</title>
      <link>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11917825#M1844</link>
      <description>Thank you very much. Let me learn.</description>
      <pubDate>Mon, 24 Apr 2023 13:43:12 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/objectarx-forum/help-find-the-maximum-value-of-the-sum-of-all-arrs-and-the/m-p/11917825#M1844</guid>
      <dc:creator>463017170</dc:creator>
      <dc:date>2023-04-24T13:43:12Z</dc:date>
    </item>
  </channel>
</rss>

